Skip to content

Shutdown executors while stopping the server #4819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLServerSocket;
Expand All @@ -44,12 +46,15 @@
import io.helidon.nima.webserver.http.DirectHandlers;
import io.helidon.nima.webserver.spi.ServerConnectionProvider;

import static java.lang.System.Logger.Level.ERROR;
import static java.lang.System.Logger.Level.INFO;
import static java.lang.System.Logger.Level.TRACE;

class ServerListener {
private static final System.Logger LOGGER = System.getLogger(ServerListener.class.getName());

private static final long EXECUTOR_SHUTDOWN_MILLIS = 500L;

private final ConnectionProviders connectionProviders;
private final String socketName;
private final ListenerConfiguration listenerConfig;
Expand Down Expand Up @@ -122,13 +127,15 @@ void stop() {
if (!running) {
return;
}
// TODO for graceful shutdown, we should wait until existing channels close
running = false;
try {
// Attempt to wait until existing channels finish execution
shutdownExecutor(readerExecutor);
shutdownExecutor(sharedExecutor);

serverSocket.close();
} catch (IOException e) {
// todo handle this
e.printStackTrace();
LOGGER.log(INFO, "Exception thrown on socket close", e);
}
serverThread.interrupt();
closeFuture.join();
Expand Down Expand Up @@ -245,6 +252,8 @@ private void listen() {
simpleHandlers);

readerExecutor.submit(handler);
} catch (RejectedExecutionException e) {
LOGGER.log(ERROR, "Executor rejected handler for new connection");
} catch (Exception e) {
// we may get an SSL handshake errors, which should only fail one socket, not the listener
LOGGER.log(TRACE, "Failed to handle accepted socket", e);
Expand All @@ -268,4 +277,22 @@ private void listen() {
closeFuture.complete(null);
}

/**
* Shutdown an executor by waiting for a period of time.
*
* @param executor executor to shut down
*/
static void shutdownExecutor(ExecutorService executor) {
try {
boolean terminate = executor.awaitTermination(EXECUTOR_SHUTDOWN_MILLIS, TimeUnit.MILLISECONDS);
if (!terminate) {
List<Runnable> running = executor.shutdownNow();
if (!running.isEmpty()) {
LOGGER.log(INFO, running.size() + " channel tasks did not terminate gracefully");
}
}
} catch (InterruptedException e) {
LOGGER.log(INFO, "InterruptedException caught while shutting down channel tasks");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.nima.webserver;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.is;

class ExecutorShutdownTest {
@Test
void testShutdown() {
try (ExecutorService executor = Executors.newThreadPerTaskExecutor(Thread.ofVirtual()
.allowSetThreadLocals(true)
.inheritInheritableThreadLocals(false)
.factory())) {
ServerListener.shutdownExecutor(executor);
assertThat(executor.isShutdown(), is(true));
}
}
}