Skip to content

Commit

Permalink
[HTTP] Scavange fix (#61530)
Browse files Browse the repository at this point in the history
* Guard pool scavenging callback from parallel execution when it takes longer than the timer interval gets triggered

* Dispose connection from the pool in a serate task to not to block the caller (scavenge timer callback)

* feedback
  • Loading branch information
ManickaP committed Nov 22, 2021
1 parent f1edeed commit c2b7638
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1979,7 +1979,12 @@ public bool CleanCacheAndDisposeIfUnused()
}

// Dispose the stale connections outside the pool lock, to avoid holding the lock too long.
toDispose?.ForEach(c => c.Dispose());
// Dispose them asynchronously to not to block the caller on closing the SslStream or NetworkStream.
if (toDispose is not null)
{
Task.Factory.StartNew(static s => ((List<HttpConnectionBase>)s!).ForEach(c => c.Dispose()), toDispose,
CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
}

// Pool is active. Should not be removed.
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ private void SetCleaningTimer(TimeSpan timeout)
{
try
{
_cleaningTimer!.Change(timeout, timeout);
_cleaningTimer!.Change(timeout, Timeout.InfiniteTimeSpan);
_timerIsRunning = timeout != Timeout.InfiniteTimeSpan;
}
catch (ObjectDisposedException)
Expand Down Expand Up @@ -492,13 +492,10 @@ private void RemoveStalePools()
}
}

// Stop running the timer if we don't have any pools to clean up.
// Restart the timer if we have any pools to clean up.
lock (SyncObj)
{
if (_pools.IsEmpty)
{
SetCleaningTimer(Timeout.InfiniteTimeSpan);
}
SetCleaningTimer(!_pools.IsEmpty ? _cleanPoolTimeout : Timeout.InfiniteTimeSpan);
}

// NOTE: There is a possible race condition with regards to a pool getting cleaned up at the same
Expand Down

0 comments on commit c2b7638

Please sign in to comment.