Skip to content

Commit 3c5f74a

Browse files
Copilotstephentoub
andauthored
Fix HTTP/2 pings not working with zero connection lifetime (#115735)
* Fix HTTP/2 pings with zero connection lifetime Co-authored-by: stephentoub <[email protected]> * Extend existing test instead of adding new ones --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: stephentoub <[email protected]> Co-authored-by: Stephen Toub <[email protected]>
1 parent 2dbdff1 commit 3c5f74a

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPoolManager.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,13 @@ public HttpConnectionPoolManager(HttpConnectionSettings settings)
6969
// However, we can only do such optimizations if we're not also tracking
7070
// connections per server, as we use data in the associated data structures
7171
// to do that tracking.
72+
// Additionally, we should not avoid storing connections if keep-alive ping is configured,
73+
// as the heartbeat timer is needed for ping functionality.
7274
bool avoidStoringConnections =
7375
settings._maxConnectionsPerServer == int.MaxValue &&
7476
(settings._pooledConnectionIdleTimeout == TimeSpan.Zero ||
75-
settings._pooledConnectionLifetime == TimeSpan.Zero);
77+
settings._pooledConnectionLifetime == TimeSpan.Zero) &&
78+
settings._keepAlivePingDelay == Timeout.InfiniteTimeSpan;
7679

7780
// Start out with the timer not running, since we have no pools.
7881
// When it does run, run it with a frequency based on the idle timeout.

src/libraries/System.Net.Http/tests/FunctionalTests/SocketsHttpHandlerTest.Http2KeepAlivePing.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,18 @@ await Http2LoopbackServer.CreateClientAndServerAsync(async uri =>
9595

9696
[OuterLoop("Runs long")]
9797
[Theory]
98-
[InlineData(HttpKeepAlivePingPolicy.Always)]
99-
[InlineData(HttpKeepAlivePingPolicy.WithActiveRequests)]
100-
public async Task KeepAliveConfigured_KeepAlivePingsAreSentAccordingToPolicy(HttpKeepAlivePingPolicy policy)
98+
[InlineData(HttpKeepAlivePingPolicy.Always, -1)]
99+
[InlineData(HttpKeepAlivePingPolicy.WithActiveRequests, -1)]
100+
[InlineData(HttpKeepAlivePingPolicy.WithActiveRequests, 0)]
101+
public async Task KeepAliveConfigured_KeepAlivePingsAreSentAccordingToPolicy(HttpKeepAlivePingPolicy policy, int connectionLifetimeMilliseconds)
101102
{
102103
await Http2LoopbackServer.CreateClientAndServerAsync(async uri =>
103104
{
104105
SocketsHttpHandler handler = CreateSocketsHttpHandler(allowAllCertificates: true);
105106
handler.KeepAlivePingTimeout = TimeSpan.FromSeconds(10);
106107
handler.KeepAlivePingPolicy = policy;
107108
handler.KeepAlivePingDelay = TimeSpan.FromSeconds(1);
109+
handler.PooledConnectionLifetime = TimeSpan.FromMilliseconds(connectionLifetimeMilliseconds);
108110

109111
using HttpClient client = new HttpClient(handler);
110112
client.DefaultRequestVersion = HttpVersion.Version20;

0 commit comments

Comments
 (0)