未验证 提交 50ac454d 编写于 作者: G Geoff Kizer 提交者: GitHub

fix issue with duplicate completion in MultipleConnectAsync (#43112)

Co-authored-by: NGeoffrey Kizer <geoffrek@windows.microsoft.com>
上级 c533b605
......@@ -151,7 +151,7 @@ private bool DoDnsCallback(IAsyncResult result, bool sync)
}
else if (!pending)
{
return DoConnectCallback(_internalArgs!);
return DoConnectCallback(_internalArgs!, sync);
}
else
{
......@@ -161,13 +161,13 @@ private bool DoDnsCallback(IAsyncResult result, bool sync)
private void InternalConnectCallback(object? sender, SocketAsyncEventArgs args)
{
DoConnectCallback(args);
DoConnectCallback(args, false);
}
// Callback which fires when an internal connection attempt completes.
// If it failed and there are more addresses to try, do it.
// Returns true if the operation is pending, false if it completed synchronously.
private bool DoConnectCallback(SocketAsyncEventArgs args)
private bool DoConnectCallback(SocketAsyncEventArgs args, bool sync)
{
Exception? exception = null;
......@@ -243,16 +243,14 @@ private bool DoConnectCallback(SocketAsyncEventArgs args)
}
}
if (exception == null)
if (exception != null)
{
Succeed();
return Fail(sync, exception);
}
else
{
AsyncFail(exception);
return Succeed(sync);
}
return false;
}
// Called to initiate a connection attempt to the next address in the list.
......@@ -287,11 +285,21 @@ private bool DoConnectCallback(SocketAsyncEventArgs args)
protected abstract void OnSucceed();
private void Succeed()
private bool Succeed(bool sync)
{
OnSucceed();
_userArgs!.FinishWrapperConnectSuccess(_internalArgs!.ConnectSocket, _internalArgs.BytesTransferred, _internalArgs.SocketFlags);
if (sync)
{
_userArgs!.FinishWrapperConnectSyncSuccess(_internalArgs!.ConnectSocket, _internalArgs.BytesTransferred, _internalArgs.SocketFlags);
}
else
{
_userArgs!.FinishWrapperConnectAsyncSuccess(_internalArgs!.ConnectSocket, _internalArgs.BytesTransferred, _internalArgs.SocketFlags);
}
_internalArgs.Dispose();
return !sync;
}
protected abstract void OnFail(bool abortive);
......
......@@ -676,7 +676,7 @@ internal void FinishConnectByNameAsyncFailure(Exception exception, int bytesTran
}
}
internal void FinishWrapperConnectSuccess(Socket? connectSocket, int bytesTransferred, SocketFlags flags)
internal void FinishWrapperConnectSyncSuccess(Socket? connectSocket, int bytesTransferred, SocketFlags flags)
{
SetResults(SocketError.Success, bytesTransferred, flags);
_currentSocket = connectSocket;
......@@ -685,8 +685,15 @@ internal void FinishWrapperConnectSuccess(Socket? connectSocket, int bytesTransf
if (SocketsTelemetry.Log.IsEnabled()) LogBytesTransferEvents(connectSocket?.SocketType, SocketAsyncOperation.Connect, bytesTransferred);
// Complete the operation and raise the event.
ExecutionContext? context = _context; // store context before it's cleared as part of completing the operation
Complete();
}
internal void FinishWrapperConnectAsyncSuccess(Socket? connectSocket, int bytesTransferred, SocketFlags flags)
{
ExecutionContext? context = _context; // store context before it's cleared as part of completing the operation
FinishWrapperConnectSyncSuccess(connectSocket, bytesTransferred, flags);
if (context == null)
{
OnCompletedInternal();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册