Skip to content
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

#1412 Wait for async close tasks before close completes #1435

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -17,13 +17,13 @@

public class AsyncHttpClientState {

private final AtomicBoolean closed;
private final AtomicBoolean closeTriggered;

public AsyncHttpClientState(AtomicBoolean closed) {
this.closed = closed;
public AsyncHttpClientState(AtomicBoolean closeTriggered) {
this.closeTriggered = closeTriggered;
}

public boolean isClosed() {
return closed.get();
public boolean isCloseTriggered() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's wrong with the old name?

return closeTriggered.get();
}
}
Expand Up @@ -19,8 +19,10 @@
import static org.asynchttpclient.util.Assertions.assertNotNull;
import io.netty.channel.EventLoopGroup;
import io.netty.util.HashedWheelTimer;
import io.netty.util.ThreadDeathWatcher;
import io.netty.util.Timer;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Predicate;

Expand All @@ -42,6 +44,7 @@ public class DefaultAsyncHttpClient implements AsyncHttpClient {

private final static Logger LOGGER = LoggerFactory.getLogger(DefaultAsyncHttpClient.class);
private final AsyncHttpClientConfig config;
private final AtomicBoolean closeTriggered = new AtomicBoolean(false);
private final AtomicBoolean closed = new AtomicBoolean(false);
private final ChannelManager channelManager;
private final ConnectionSemaphore connectionSemaphore;
Expand Down Expand Up @@ -87,7 +90,7 @@ public DefaultAsyncHttpClient(AsyncHttpClientConfig config) {

channelManager = new ChannelManager(config, nettyTimer);
connectionSemaphore = new ConnectionSemaphore(config);
requestSender = new NettyRequestSender(config, channelManager, connectionSemaphore, nettyTimer, new AsyncHttpClientState(closed));
requestSender = new NettyRequestSender(config, channelManager, connectionSemaphore, nettyTimer, new AsyncHttpClientState(closeTriggered));
channelManager.configureBootstraps(requestSender);
}

Expand All @@ -99,7 +102,7 @@ private Timer newNettyTimer() {

@Override
public void close() {
if (closed.compareAndSet(false, true)) {
if (closeTriggered.compareAndSet(false, true)) {
try {
channelManager.close();
} catch (Throwable t) {
Expand All @@ -112,6 +115,15 @@ public void close() {
LOGGER.warn("Unexpected error on HashedWheelTimer close", t);
}
}

//see https://github.com/netty/netty/issues/2084#issuecomment-44822314
try {
ThreadDeathWatcher.awaitInactivity(config.getShutdownTimeout(), TimeUnit.MILLISECONDS);
} catch(InterruptedException t) {
// Ignore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-assert interrupted status

}

closed.compareAndSet(false, true);
}
}

Expand Down
Expand Up @@ -43,6 +43,7 @@
import java.net.InetSocketAddress;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
Expand Down Expand Up @@ -99,11 +100,15 @@ public class ChannelManager {
private final ChannelPool channelPool;
private final ChannelGroup openChannels;

private final CountDownLatch closeLatch;

private AsyncHttpClientHandler wsHandler;

public ChannelManager(final AsyncHttpClientConfig config, Timer nettyTimer) {

this.config = config;

closeLatch = new CountDownLatch(2);

this.sslEngineFactory = config.getSslEngineFactory() != null ? config.getSslEngineFactory() : new DefaultSslEngineFactory();
try {
Expand Down Expand Up @@ -300,16 +305,30 @@ public boolean removeAll(Channel connection) {
}

private void doClose() {
openChannels.close();
openChannels.close().addListener(future -> closeLatch.countDown());
channelPool.destroy();

//see https://github.com/netty/netty/issues/2084#issuecomment-44822314
try {
GlobalEventExecutor.INSTANCE.awaitInactivity(config.getShutdownTimeout(), TimeUnit.MILLISECONDS);
} catch(InterruptedException t) {
// Ignore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-assert interrupted status

} finally {
closeLatch.countDown();
}
}

public void close() {
if (allowReleaseEventLoopGroup) {
eventLoopGroup.shutdownGracefully(config.getShutdownQuietPeriod(), config.getShutdownTimeout(), TimeUnit.MILLISECONDS)//
.addListener(future -> doClose());
} else {
} else
doClose();

try {
closeLatch.await(config.getShutdownTimeout(), TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
// Ignore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-assert interrupted status

}
}

Expand Down
Expand Up @@ -72,7 +72,7 @@ public void connect(final Bootstrap bootstrap, final NettyConnectListener<?> con
try {
connect0(bootstrap, connectListener, remoteAddress);
} catch (RejectedExecutionException e) {
if (clientState.isClosed()) {
if (clientState.isCloseTriggered()) {
LOGGER.info("Connect crash but engine is shutting down");
} else {
connectListener.onFailure(null, e);
Expand Down
Expand Up @@ -597,7 +597,7 @@ public void replayRequest(final NettyResponseFuture<?> future, FilterContext fc,
}

public boolean isClosed() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename method too.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done and attached another commit to the pull request!

return clientState.isClosed();
return clientState.isCloseTriggered();
}

public void drainChannelAndExecuteNextRequest(final Channel channel, final NettyResponseFuture<?> future, Request nextRequest) {
Expand Down