From 6e12567353fb8a4dacc927f795acec72b3796ad7 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Fri, 8 Jan 2021 14:52:44 +0100 Subject: [PATCH] SocketAsyncContext.Unix: fix double processing of AsyncOperations (#45683) * SocketAsyncContext.Unix: fix double processing of AsyncOperations When a socket event occurs, the pending operations gets triggered to continue their work by calling the Process method. The changes in https://github.com/dotnet/runtime/pull/37974 cause Process to be called twice on the same AsyncOperation. When Process is called, the operation can complete, and the AsyncOperation instance may be reused for a different operation. * Remove processAsyncEvents --- .../System/Net/Sockets/SocketAsyncContext.Unix.cs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncContext.Unix.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncContext.Unix.cs index 4abf6ff75c8127..343c9c9126b4ec 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncContext.Unix.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncContext.Unix.cs @@ -844,7 +844,7 @@ public bool StartAsyncOperation(SocketAsyncContext context, TOperation operation } } - public AsyncOperation? ProcessSyncEventOrGetAsyncEvent(SocketAsyncContext context, bool skipAsyncEvents = false, bool processAsyncEvents = true) + public AsyncOperation? ProcessSyncEventOrGetAsyncEvent(SocketAsyncContext context, bool skipAsyncEvents = false) { AsyncOperation op; using (Lock()) @@ -865,7 +865,6 @@ public bool StartAsyncOperation(SocketAsyncContext context, TOperation operation Debug.Assert(_isNextOperationSynchronous == (op.Event != null)); if (skipAsyncEvents && !_isNextOperationSynchronous) { - Debug.Assert(!processAsyncEvents); // Return the operation to indicate that the async operation was not processed, without making // any state changes because async operations are being skipped return op; @@ -903,11 +902,6 @@ public bool StartAsyncOperation(SocketAsyncContext context, TOperation operation { // Async operation. The caller will figure out how to process the IO. Debug.Assert(!skipAsyncEvents); - if (processAsyncEvents) - { - op.Process(); - return null; - } return op; } } @@ -2079,12 +2073,14 @@ public void HandleEventsInline(Interop.Sys.SocketEvents events) if ((events & Interop.Sys.SocketEvents.Read) != 0) { - _receiveQueue.ProcessSyncEventOrGetAsyncEvent(this, processAsyncEvents: true); + AsyncOperation? receiveOperation = _receiveQueue.ProcessSyncEventOrGetAsyncEvent(this); + receiveOperation?.Process(); } if ((events & Interop.Sys.SocketEvents.Write) != 0) { - _sendQueue.ProcessSyncEventOrGetAsyncEvent(this, processAsyncEvents: true); + AsyncOperation? sendOperation = _sendQueue.ProcessSyncEventOrGetAsyncEvent(this); + sendOperation?.Process(); } }