Skip to content

Commit

Permalink
Fix socket connecting synchronously (Azure#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
varunpuranik authored and Seabiscuit committed Aug 9, 2018
1 parent 884b68e commit 23e1c24
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/DotNetty.Transport/Channels/Sockets/AbstractSocketChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,27 +334,26 @@ 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)
{
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();
}

}
}

Expand Down
4 changes: 4 additions & 0 deletions src/DotNetty.Transport/Channels/Sockets/TcpSocketChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 23e1c24

Please sign in to comment.