From 43b5d81660045be81910074f3a4d6c0b96b26305 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Thu, 7 May 2020 20:02:08 -0400 Subject: [PATCH] Translate all ObjectDisposedExceptions to SocketExceptions in MultipleConnectAsync.AttemptConnection (#35965) 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/System/Net/Sockets/MultipleConnectAsync.cs | 40 +++++----------------- 1 file changed, 8 insertions(+), 32 deletions(-) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/MultipleConnectAsync.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/MultipleConnectAsync.cs index e301f6d..c201d24 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/MultipleConnectAsync.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/MultipleConnectAsync.cs @@ -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(); -- 2.7.4