diff --git a/src/DotNetty.Transport/Channels/Sockets/AbstractSocketChannel.cs b/src/DotNetty.Transport/Channels/Sockets/AbstractSocketChannel.cs index fb5e0de6a..ffc590f77 100644 --- a/src/DotNetty.Transport/Channels/Sockets/AbstractSocketChannel.cs +++ b/src/DotNetty.Transport/Channels/Sockets/AbstractSocketChannel.cs @@ -334,16 +334,6 @@ public sealed override Task ConnectAsync(EndPoint remoteAddress, EndPoint localA void FulfillConnectPromise(bool wasActive) { - TaskCompletionSource promise = this.Channel.connectPromise; - if (promise == null) - { - // Closed via cancellation and the promise has been notified already. - return; - } - - // trySuccess() will return false if a user cancelled the connection attempt. - bool promiseSet = promise.TryComplete(); - // Regardless if the connection attempt was cancelled, channelActive() event should be triggered, // because what happened is what happened. if (!wasActive && this.channel.Active) @@ -351,10 +341,19 @@ void FulfillConnectPromise(bool wasActive) this.channel.Pipeline.FireChannelActive(); } - // If a user cancelled the connection attempt, close the channel, which is followed by channelInactive(). - if (!promiseSet) + TaskCompletionSource promise = this.Channel.connectPromise; + // If promise is null, then it the channel was Closed via cancellation and the promise has been notified already. + if (promise != null) { - this.CloseSafe(); + // trySuccess() will return false if a user cancelled the connection attempt. + bool promiseSet = promise.TryComplete(); + + // If a user cancelled the connection attempt, close the channel, which is followed by channelInactive(). + if (!promiseSet) + { + this.CloseSafe(); + } + } } diff --git a/src/DotNetty.Transport/Channels/Sockets/TcpSocketChannel.cs b/src/DotNetty.Transport/Channels/Sockets/TcpSocketChannel.cs index 900fed57e..a31d8490b 100644 --- a/src/DotNetty.Transport/Channels/Sockets/TcpSocketChannel.cs +++ b/src/DotNetty.Transport/Channels/Sockets/TcpSocketChannel.cs @@ -129,6 +129,10 @@ protected override bool DoConnect(EndPoint remoteAddress, EndPoint localAddress) RemoteEndPoint = remoteAddress }; bool connected = !this.Socket.ConnectAsync(eventPayload); + if(connected) + { + this.DoFinishConnect(eventPayload); + } success = true; return connected; }