* 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
Co-authored-by: ManickaP <mapichov@microsoft.com>
}
// 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;
{
try
{
- _cleaningTimer!.Change(timeout, timeout);
+ _cleaningTimer!.Change(timeout, Timeout.InfiniteTimeSpan);
_timerIsRunning = timeout != Timeout.InfiniteTimeSpan;
}
catch (ObjectDisposedException)
}
}
- // 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