diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs index 5224adcfa36a6..862ac35498719 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs @@ -288,11 +288,6 @@ public bool TryReserveStream() if (_streamsInUse < _maxConcurrentStreams) { - if (_streamsInUse == 0) - { - MarkConnectionAsNotIdle(); - } - _streamsInUse++; return true; } @@ -324,8 +319,6 @@ public void ReleaseStream() if (_streamsInUse == 0) { - MarkConnectionAsIdle(); - if (_shutdown) { FinalTeardown(); @@ -1606,6 +1599,11 @@ private void AddStream(Http2Stream http2Stream) ThrowRetry(SR.net_http_request_aborted); } + if (_httpStreams.Count == 0) + { + MarkConnectionAsNotIdle(); + } + // Now that we're holding the lock, configure the stream. The lock must be held while // assigning the stream ID to ensure only one stream gets an ID, and it must be held // across setting the initial window size (available credit) and storing the stream into @@ -2072,6 +2070,11 @@ private void RemoveStream(Http2Stream http2Stream) Debug.Fail($"Stream {http2Stream.StreamId} not found in dictionary during RemoveStream???"); return; } + + if (_httpStreams.Count == 0) + { + MarkConnectionAsIdle(); + } } ReleaseStream(); diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/MetricsTest.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/MetricsTest.cs index 759f24b8f74de..161b1e793b78b 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/MetricsTest.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/MetricsTest.cs @@ -719,9 +719,9 @@ await LoopbackServerFactory.CreateClientAndServerAsync(async uri => Action check2 = connectionNoLongerIdle; Action check3 = connectionIsActive; - if (UseVersion.Major > 1) + if (UseVersion.Major > 2) { - // With HTTP/2 and HTTP/3, the idle state change is emitted before RequestsQueueDuration. + // With HTTP/3, the idle state change is emitted before RequestsQueueDuration. check1 = connectionNoLongerIdle; check2 = connectionIsActive; check3 = requestsQueueDuration;