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 6320ac5d40b9e..ef7a0a0ce60f5 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 @@ -266,7 +266,7 @@ public bool TryCancel() // we can't pool the object, as ProcessQueue may still have a reference to it, due to // using a pattern whereby it takes the lock to grab an item, but then releases the lock // to do further processing on the item that's still in the list. - ThreadPool.QueueUserWorkItem(o => ((AsyncOperation)o).InvokeCallback(allowPooling: false), this); + ThreadPool.UnsafeQueueUserWorkItem(o => ((AsyncOperation)o).InvokeCallback(allowPooling: false), this); } Trace("Exit"); @@ -837,7 +837,7 @@ public void HandleEvent(SocketAsyncContext context) // We just transitioned from Waiting to Processing. // Spawn a work item to do the actual processing. - ThreadPool.QueueUserWorkItem(s_processingCallback, context); + ThreadPool.UnsafeQueueUserWorkItem(s_processingCallback, context); } // Called on the threadpool when data may be available. @@ -968,7 +968,7 @@ public void ProcessQueue(SocketAsyncContext context) Debug.Assert(_state == QueueState.Processing); // Spawn a new work item to continue processing the queue. - ThreadPool.QueueUserWorkItem(s_processingCallback, context); + ThreadPool.UnsafeQueueUserWorkItem(s_processingCallback, context); } // At this point, the operation has completed and it's no longer diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEngine.Unix.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEngine.Unix.cs index 39b503551e286..b270ff7c77902 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEngine.Unix.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEngine.Unix.cs @@ -286,11 +286,21 @@ private SocketAsyncEngine() // // Start the event loop on its own thread. // - Task.Factory.StartNew( - EventLoop, - CancellationToken.None, - TaskCreationOptions.LongRunning, - TaskScheduler.Default); + bool suppressFlow = !ExecutionContext.IsFlowSuppressed(); + try + { + if (suppressFlow) ExecutionContext.SuppressFlow(); + Task.Factory.StartNew( + s => ((SocketAsyncEngine)s).EventLoop(), + this, + CancellationToken.None, + TaskCreationOptions.LongRunning, + TaskScheduler.Default); + } + finally + { + if (suppressFlow) ExecutionContext.RestoreFlow(); + } } catch {