From 01e3d29673dbfce2b3520635a50812f4b83e26d7 Mon Sep 17 00:00:00 2001 From: Roman Konecny Date: Thu, 4 Jul 2024 11:13:12 +0200 Subject: [PATCH 1/3] Fix cancelability of DNS queries --- .../src/System/Net/Sockets/Socket.Tasks.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs index 3ba24e90cf101..606aec0829fdd 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs @@ -97,7 +97,7 @@ public ValueTask ConnectAsync(EndPoint remoteEP, CancellationToken cancellationT saea.RemoteEndPoint = remoteEP; - ValueTask connectTask = saea.ConnectAsync(this); + ValueTask connectTask = saea.ConnectAsync(this, saeaCancelable: cancellationToken.CanBeCanceled); if (connectTask.IsCompleted || !cancellationToken.CanBeCanceled) { // Avoid async invocation overhead @@ -1210,11 +1210,11 @@ public ValueTask SendToAsync(Socket socket, CancellationToken cancellationT ValueTask.FromException(CreateException(error)); } - public ValueTask ConnectAsync(Socket socket) + public ValueTask ConnectAsync(Socket socket, bool saeaCancelable) { try { - if (socket.ConnectAsync(this, userSocket: true, saeaCancelable: false)) + if (socket.ConnectAsync(this, userSocket: true, saeaCancelable: saeaCancelable)) { return new ValueTask(this, _mrvtsc.Version); } From 5e253671edaade5449df9dbd54e5d35329bf4fd1 Mon Sep 17 00:00:00 2001 From: Roman Konecny Date: Tue, 9 Jul 2024 19:34:02 +0200 Subject: [PATCH 2/3] Properly cascade cancelation during connect by name --- .../src/System/Net/Sockets/SocketAsyncEventArgs.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs index 78dd22e5eda7b..1d5f1cd1b8daf 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs @@ -428,6 +428,13 @@ internal void SetResults(Exception exception, int bytesTransferred, SocketFlags { _socketError = socketException.SocketErrorCode; } + else if (exception is OperationCanceledException) + { + // Preserver information about the cancellation + // when it is canceled at non Socket operation. + // It will be later used to throw the right exception. + _socketError = SocketError.OperationAborted; + } else { _socketError = SocketError.SocketError; From c0c819819c423c84efa86ea0d35d5924983fb14a Mon Sep 17 00:00:00 2001 From: Roman Konecny Date: Wed, 10 Jul 2024 16:13:37 +0200 Subject: [PATCH 3/3] Typo --- .../src/System/Net/Sockets/SocketAsyncEventArgs.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs index 1d5f1cd1b8daf..29e9aa094555b 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs @@ -430,9 +430,8 @@ internal void SetResults(Exception exception, int bytesTransferred, SocketFlags } else if (exception is OperationCanceledException) { - // Preserver information about the cancellation - // when it is canceled at non Socket operation. - // It will be later used to throw the right exception. + // Preserve information about the cancellation when it is canceled at non Socket operation. + // It is used to throw the right exception later in the stack. _socketError = SocketError.OperationAborted; } else