Skip to content

Commit

Permalink
Fix closure in HttpConnectionPoolManager.StartMonitoringNetworkChanges (
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Apr 15, 2021
1 parent e254b56 commit f958f2b
Showing 1 changed file with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,20 @@ public void StartMonitoringNetworkChanges()

// Monitor network changes to invalidate Alt-Svc headers.
// A weak reference is used to avoid NetworkChange.NetworkAddressChanged keeping a non-disposed connection pool alive.
var poolsRef = new WeakReference<ConcurrentDictionary<HttpConnectionKey, HttpConnectionPool>>(_pools);
NetworkAddressChangedEventHandler networkChangedDelegate = delegate
{
if (poolsRef.TryGetTarget(out ConcurrentDictionary<HttpConnectionKey, HttpConnectionPool>? pools))
NetworkAddressChangedEventHandler networkChangedDelegate;
{ // scope to avoid closure if _networkChangeCleanup != null
var poolsRef = new WeakReference<ConcurrentDictionary<HttpConnectionKey, HttpConnectionPool>>(_pools);
networkChangedDelegate = delegate
{
foreach (HttpConnectionPool pool in pools.Values)
if (poolsRef.TryGetTarget(out ConcurrentDictionary<HttpConnectionKey, HttpConnectionPool>? pools))
{
pool.OnNetworkChanged();
foreach (HttpConnectionPool pool in pools.Values)
{
pool.OnNetworkChanged();
}
}
}
};
};
}

var cleanup = new NetworkChangeCleanup(networkChangedDelegate);

Expand Down

0 comments on commit f958f2b

Please sign in to comment.