Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit d425f86

Browse files
benaadamsstephentoub
authored andcommitted
Don't capture AsyncLocals into HttpConnectionPools timer (#26068)
* Don't capture AsyncLocals into HttpConnectionPools timer * Harden EC flow suppression
1 parent d7dd6a4 commit d425f86

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/System.Net.Http/src/System/Net/Http/Managed/HttpConnectionPools.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,25 @@ public HttpConnectionPools(int maxConnectionsPerServer)
3939
_maxConnectionsPerServer = maxConnectionsPerServer;
4040
_pools = new ConcurrentDictionary<HttpConnectionKey, HttpConnectionPool>();
4141
// Start out with the timer not running, since we have no pools.
42-
_cleaningTimer = new Timer(s => ((HttpConnectionPools)s).RemoveStalePools(), this, Timeout.Infinite, Timeout.Infinite);
42+
43+
// Don't capture the current ExecutionContext and its AsyncLocals onto the timer causing them to live forever
44+
bool restoreFlow = false;
45+
try
46+
{
47+
if (!ExecutionContext.IsFlowSuppressed())
48+
{
49+
ExecutionContext.SuppressFlow();
50+
restoreFlow = true;
51+
}
52+
53+
_cleaningTimer = new Timer(s => ((HttpConnectionPools)s).RemoveStalePools(), this, Timeout.Infinite, Timeout.Infinite);
54+
}
55+
finally
56+
{
57+
// Restore the current ExecutionContext
58+
if (restoreFlow)
59+
ExecutionContext.RestoreFlow();
60+
}
4361
}
4462

4563
/// <summary>Gets a pool for the specified endpoint, adding one if none existed.</summary>

0 commit comments

Comments
 (0)