diff --git a/src/libraries/System.Net.Http.WinHttpHandler/src/System/Net/Http/WinHttpHandler.cs b/src/libraries/System.Net.Http.WinHttpHandler/src/System/Net/Http/WinHttpHandler.cs index 732f3482483225..2b9a7375d9963c 100644 --- a/src/libraries/System.Net.Http.WinHttpHandler/src/System/Net/Http/WinHttpHandler.cs +++ b/src/libraries/System.Net.Http.WinHttpHandler/src/System/Net/Http/WinHttpHandler.cs @@ -90,7 +90,7 @@ private Func< private int _maxResponseDrainSize = HttpHandlerDefaults.DefaultMaxResponseDrainSize; private IDictionary? _properties; // Only create dictionary when required. private volatile bool _operationStarted; - private volatile bool _disposed; + private volatile int _disposed; private SafeWinHttpHandle? _sessionHandle; private readonly WinHttpAuthHelper _authHelper = new WinHttpAuthHelper(); @@ -539,13 +539,11 @@ public bool EnableMultipleHttp2Connections protected override void Dispose(bool disposing) { - if (!_disposed) + if (Interlocked.CompareExchange(ref _disposed, 1, 0) == 0) { - _disposed = true; - if (disposing && _sessionHandle != null) { - SafeWinHttpHandle.DisposeAndClearHandle(ref _sessionHandle); + _sessionHandle.Dispose(); } } @@ -1033,7 +1031,7 @@ private async Task StartRequestAsync(WinHttpRequestState state) } finally { - SafeWinHttpHandle.DisposeAndClearHandle(ref connectHandle); + connectHandle?.Dispose(); try { @@ -1620,7 +1618,7 @@ private void SetOperationStarted() private void CheckDisposed() { - if (_disposed) + if (_disposed == 1) { throw new ObjectDisposedException(GetType().FullName); }