diff --git a/src/System.Net.Http/src/System/Net/Http/Managed/HttpConnectionPools.cs b/src/System.Net.Http/src/System/Net/Http/Managed/HttpConnectionPools.cs index 2650b7bab385..d1d043b5432b 100644 --- a/src/System.Net.Http/src/System/Net/Http/Managed/HttpConnectionPools.cs +++ b/src/System.Net.Http/src/System/Net/Http/Managed/HttpConnectionPools.cs @@ -39,7 +39,25 @@ public HttpConnectionPools(int maxConnectionsPerServer) _maxConnectionsPerServer = maxConnectionsPerServer; _pools = new ConcurrentDictionary(); // Start out with the timer not running, since we have no pools. - _cleaningTimer = new Timer(s => ((HttpConnectionPools)s).RemoveStalePools(), this, Timeout.Infinite, Timeout.Infinite); + + // Don't capture the current ExecutionContext and its AsyncLocals onto the timer causing them to live forever + bool restoreFlow = false; + try + { + if (!ExecutionContext.IsFlowSuppressed()) + { + ExecutionContext.SuppressFlow(); + restoreFlow = true; + } + + _cleaningTimer = new Timer(s => ((HttpConnectionPools)s).RemoveStalePools(), this, Timeout.Infinite, Timeout.Infinite); + } + finally + { + // Restore the current ExecutionContext + if (restoreFlow) + ExecutionContext.RestoreFlow(); + } } /// Gets a pool for the specified endpoint, adding one if none existed.