Translate all ObjectDisposedExceptions to SocketExceptions in MultipleConnectAsync...
authorStephen Toub <stoub@microsoft.com>
Fri, 8 May 2020 00:02:08 +0000 (20:02 -0400)
committerGitHub <noreply@github.com>
Fri, 8 May 2020 00:02:08 +0000 (20:02 -0400)
If ObjectDisposedExceptions occurred at unexpected times, we'd return that ObjectDisposedException rather than a SocketException for OperationAborted.  Higher-levels would then treat that arbitrary exception as a generic SocketError. So net net, this means that consuming code would get a SocketException for the more generic SocketError.SocketError rather than the more specific SocketError.OperationAborted.

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

index e301f6d..c201d24 100644 (file)
@@ -230,55 +230,31 @@ namespace System.Net.Sockets
         {
             try
             {
-                Socket? attemptSocket;
-                IPAddress? attemptAddress = GetNextAddress(out attemptSocket);
-
+                IPAddress? attemptAddress = GetNextAddress(out Socket? attemptSocket);
                 if (attemptAddress == null)
                 {
                     return new SocketException((int)SocketError.NoData);
                 }
+                Debug.Assert(attemptSocket != null);
 
-                _internalArgs!.RemoteEndPoint = new IPEndPoint(attemptAddress, _endPoint!.Port);
-
-                return AttemptConnection(attemptSocket!, _internalArgs);
-            }
-            catch (Exception e)
-            {
-                if (e is ObjectDisposedException)
-                {
-                    NetEventSource.Fail(this, "unexpected ObjectDisposedException");
-                }
-                return e;
-            }
-        }
-
-        private Exception? AttemptConnection(Socket attemptSocket, SocketAsyncEventArgs args)
-        {
-            try
-            {
-                if (attemptSocket == null)
-                {
-                    NetEventSource.Fail(null, "attemptSocket is null!");
-                }
-
-                bool pending = attemptSocket.ConnectAsync(args);
-                if (!pending)
+                SocketAsyncEventArgs args = _internalArgs!;
+                args.RemoteEndPoint = new IPEndPoint(attemptAddress, _endPoint!.Port);
+                if (!attemptSocket.ConnectAsync(args))
                 {
                     InternalConnectCallback(null, args);
                 }
+
+                return null;
             }
             catch (ObjectDisposedException)
             {
-                // This can happen if the user closes the socket, and is equivalent to a call
-                // to CancelConnectAsync
+                // This can happen if the user closes the socket and is equivalent to a call to CancelConnectAsync.
                 return new SocketException((int)SocketError.OperationAborted);
             }
             catch (Exception e)
             {
                 return e;
             }
-
-            return null;
         }
 
         protected abstract void OnSucceed();