make CancelConnectAsync thread safe (#42935)
authorTomas Weinfurt <tweinfurt@yahoo.com>
Thu, 1 Oct 2020 05:57:04 +0000 (22:57 -0700)
committerGitHub <noreply@github.com>
Thu, 1 Oct 2020 05:57:04 +0000 (22:57 -0700)
* make CancelConnectAsync thread safe

* replace var with MultipleConnectAsync

src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs

index 2a40157..c6e77b3 100644 (file)
@@ -589,20 +589,16 @@ namespace System.Net.Sockets
         {
             if (_operating == InProgress && _completedOperation == SocketAsyncOperation.Connect)
             {
-                if (_multipleConnect != null)
+                MultipleConnectAsync? multipleConnect = _multipleConnect;
+                if (multipleConnect != null)
                 {
                     // If a multiple connect is in progress, abort it.
-                    _multipleConnect.Cancel();
+                    multipleConnect.Cancel();
                 }
                 else
                 {
                     // Otherwise we're doing a normal ConnectAsync - cancel it by closing the socket.
-                    // _currentSocket will only be null if _multipleConnect was set, so we don't have to check.
-                    if (_currentSocket == null)
-                    {
-                        NetEventSource.Fail(this, "CurrentSocket and MultipleConnect both null!");
-                    }
-                    _currentSocket.Dispose();
+                    _currentSocket?.Dispose();
                 }
             }
         }