Skip to content

Track HTTP/2 pools when connection timeout is zero - #136656

Open
RutulPatel8 wants to merge 3 commits into
dotnet:mainfrom
RutulPatel8:fix/http2-zero-lifetime-dispose-135997
Open

RutulPatel8 wants to merge 3 commits into
dotnet:mainfrom
RutulPatel8:fix/http2-zero-lifetime-dispose-135997

Conversation

@RutulPatel8

@RutulPatel8 RutulPatel8 commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

With a zero pooled connection lifetime or idle timeout, HttpConnectionPoolManager omits its cleanup timer and does not track pools. A newly created HTTP/2 connection can still reach the pool's return path and remain cached there, so disposing the handler misses it.

Keep the existing no-storage optimization. When an HTTP/2 connection returns to an untracked pool, do not cache it. Defer its shutdown until all streams already assigned to it have finished, including the first request that may not have started sending when the connection is returned.

Add an HTTP/2 regression test for zero lifetime and zero idle timeout. It completes a request, disposes the handler, and waits for a client-initiated disconnect.

Fixes #135997.

Validation

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 29, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 4 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @karelz, @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

settings._keepAlivePingDelay == Timeout.InfiniteTimeSpan &&
// HTTP/2 and HTTP/3 may pool a new connection before its first request completes.
// Keep their pools tracked so handler disposal can close those connections.
settings._maxHttpVersion.Major == 1;

@MihaZupan MihaZupan Oct 29, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would effectively kill this optimization in practice.

A better alternative approach may be to check for this edge case as part of returning an H2 connection to the pool.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, you are right: gating the optimization on the maximum HTTP version disables it for most default handlers. I pushed 8d455703d2c, which restores the original manager condition and handles the untracked-pool case in ReturnHttp2Connection. The connection is removed from pooling, and its shutdown waits for streams already assigned to it so the first request can still send. Both the library and functional test projects build with zero warnings/errors; CI is running for the new commit. @MihaZupan, could you take another look when it finishes?

{
Debug.Assert(_poolManager.AvoidStoringConnections);
if (NetEventSource.Log.IsEnabled()) connection.Trace("Disposing HTTP2 connection when its assigned streams complete. Pool is untracked.");
connection.DisposeWhenIdle();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this different from calling the existing Shutdown? Why do we need to track the extra _disposeWhenIdle?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shutdown() sets _shutdown immediately. A waiter can already have been given a stream reservation on the newly created connection; its request checks _shutdown before opening the stream and would then retry on another connection. _disposeWhenIdle blocks new reservations while letting those already assigned finish, and calls Shutdown() after the last reservation is released. I also updated the waiter assertion in 87b80e7 to account for that intermediate state. Please let me know if you see a simpler way to retain the assigned request.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Net.Http community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Disposing SocketsHttpHandler leaks HTTP/2 connections (socket, SslStream, native TLS state) when PooledConnectionLifetime is TimeSpan.Zero

2 participants