Skip to content

Commit e67df79

Browse files
varunpuraniknayato
authored andcommitted
Fix socket connecting synchronously (#413)
1 parent f5a7f3b commit e67df79

2 files changed

Lines changed: 16 additions & 13 deletions

File tree

src/DotNetty.Transport/Channels/Sockets/AbstractSocketChannel.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -318,27 +318,26 @@ public sealed override Task ConnectAsync(EndPoint remoteAddress, EndPoint localA
318318

319319
void FulfillConnectPromise(bool wasActive)
320320
{
321-
TaskCompletionSource promise = this.Channel.connectPromise;
322-
if (promise == null)
323-
{
324-
// Closed via cancellation and the promise has been notified already.
325-
return;
326-
}
327-
328-
// trySuccess() will return false if a user cancelled the connection attempt.
329-
bool promiseSet = promise.TryComplete();
330-
331321
// Regardless if the connection attempt was cancelled, channelActive() event should be triggered,
332322
// because what happened is what happened.
333323
if (!wasActive && this.channel.Active)
334324
{
335325
this.channel.Pipeline.FireChannelActive();
336326
}
337327

338-
// If a user cancelled the connection attempt, close the channel, which is followed by channelInactive().
339-
if (!promiseSet)
328+
TaskCompletionSource promise = this.Channel.connectPromise;
329+
// If promise is null, then it the channel was Closed via cancellation and the promise has been notified already.
330+
if (promise != null)
340331
{
341-
this.CloseSafe();
332+
// trySuccess() will return false if a user cancelled the connection attempt.
333+
bool promiseSet = promise.TryComplete();
334+
335+
// If a user cancelled the connection attempt, close the channel, which is followed by channelInactive().
336+
if (!promiseSet)
337+
{
338+
this.CloseSafe();
339+
}
340+
342341
}
343342
}
344343

src/DotNetty.Transport/Channels/Sockets/TcpSocketChannel.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ protected override bool DoConnect(EndPoint remoteAddress, EndPoint localAddress)
126126
var eventPayload = new SocketChannelAsyncOperation(this, false);
127127
eventPayload.RemoteEndPoint = remoteAddress;
128128
bool connected = !this.Socket.ConnectAsync(eventPayload);
129+
if(connected)
130+
{
131+
this.DoFinishConnect(eventPayload);
132+
}
129133
success = true;
130134
return connected;
131135
}

0 commit comments

Comments
 (0)