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

tcp-netty-internal: fix race in TcpConnector #3069

Merged
merged 2 commits into from
Sep 27, 2024
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 @@ -32,7 +32,10 @@
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import io.netty.channel.ConnectTimeoutException;
import io.netty.channel.EventLoop;
import io.netty.resolver.AbstractAddressResolver;
Expand Down Expand Up @@ -112,24 +115,6 @@ protected void handleSubscribe(final Subscriber<? super C> subscriber) {
Future<?> connectFuture = connect0(localAddress, resolvedRemoteAddress, config, autoRead,
executionContext, connectHandler);
connectHandler.connectFuture(connectFuture);
connectFuture.addListener(f -> {
Throwable cause = f.cause();
if (cause != null) {
if (cause instanceof ConnectTimeoutException) {
String msg = resolvedRemoteAddress instanceof FileDescriptorSocketAddress ?
"Failed to register: " + resolvedRemoteAddress :
"Failed to connect: " + resolvedRemoteAddress + " (localAddress: " +
localAddress + ")";
cause = new io.servicetalk.client.api.ConnectTimeoutException(msg, cause);
} else if (cause instanceof ConnectException) {
cause = new RetryableConnectException((ConnectException) cause);
}
if (f instanceof ChannelFuture) {
assignConnectionError(((ChannelFuture) f).channel(), cause);
}
connectHandler.connectFailed(cause);
}
});
} catch (Throwable t) {
connectHandler.unexpectedFailure(t);
}
Expand All @@ -140,13 +125,44 @@ protected void handleSubscribe(final Subscriber<? super C> subscriber) {
private static Future<?> connect0(@Nullable SocketAddress localAddress, Object resolvedRemoteAddress,
ReadOnlyTcpClientConfig config, boolean autoRead,
ExecutionContext<?> executionContext,
Consumer<? super Channel> subscriber) {
ConnectHandler<?> connectHandler) {
// Create the handler here and ensure in connectWithBootstrap / initFileDescriptorBasedChannel it is added
// to the ChannelPipeline after registration is complete as otherwise we may miss channelActive events.
ChannelHandler handler = new io.netty.channel.ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel channel) {
subscriber.accept(channel);
// We need to intercept the `connect` call in the pipeline and add our listener because right after the
// pipeline finishes its `connect(..)` sequence the netty Bootstrap will add the `CLOSE_ON_FAILURE`
// listener to the connect future. That will close the channel and complete the close future before we
// can call the `ChannelCloseUtils.assignConnectionError` helpers and can cause us to miss the reason
// for the channel closing.
channel.pipeline().addLast(new ChannelOutboundHandlerAdapter() {
@Override
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress,
bryce-anderson marked this conversation as resolved.
Show resolved Hide resolved
SocketAddress localAddress, ChannelPromise promise) throws Exception {
ctx.pipeline().remove(this);
promise.addListener(f -> {
Throwable cause = f.cause();
if (cause != null) {
if (cause instanceof ConnectTimeoutException) {
String msg = resolvedRemoteAddress instanceof FileDescriptorSocketAddress ?
"Failed to register: " + resolvedRemoteAddress :
"Failed to connect: " + resolvedRemoteAddress + " (localAddress: " +
localAddress + ")";
cause = new io.servicetalk.client.api.ConnectTimeoutException(msg, cause);
} else if (cause instanceof ConnectException) {
cause = new RetryableConnectException((ConnectException) cause);
}
if (f instanceof ChannelFuture) {
assignConnectionError(((ChannelFuture) f).channel(), cause);
}
connectHandler.connectFailed(cause);
}
});
super.connect(ctx, remoteAddress, localAddress, promise);
}
});
connectHandler.accept(channel);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ void setUp() throws Exception {
client = createClient();
}

// Visible for overriding.
private TcpClient createClient() {
return new TcpClient(getTcpClientConfig(), getClientTransportObserver());
}
Expand Down
Loading