diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPoolManager.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPoolManager.cs index 27181c7b5b8a95..8424d1b26cebe2 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPoolManager.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPoolManager.cs @@ -69,10 +69,13 @@ public HttpConnectionPoolManager(HttpConnectionSettings settings) // However, we can only do such optimizations if we're not also tracking // connections per server, as we use data in the associated data structures // to do that tracking. + // Additionally, we should not avoid storing connections if keep-alive ping is configured, + // as the heartbeat timer is needed for ping functionality. bool avoidStoringConnections = settings._maxConnectionsPerServer == int.MaxValue && (settings._pooledConnectionIdleTimeout == TimeSpan.Zero || - settings._pooledConnectionLifetime == TimeSpan.Zero); + settings._pooledConnectionLifetime == TimeSpan.Zero) && + settings._keepAlivePingDelay == Timeout.InfiniteTimeSpan; // Start out with the timer not running, since we have no pools. // When it does run, run it with a frequency based on the idle timeout. diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/SocketsHttpHandlerTest.Http2KeepAlivePing.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/SocketsHttpHandlerTest.Http2KeepAlivePing.cs index 0f1c1b8d58c326..e2af99c6de45fa 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/SocketsHttpHandlerTest.Http2KeepAlivePing.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/SocketsHttpHandlerTest.Http2KeepAlivePing.cs @@ -95,9 +95,10 @@ await Http2LoopbackServer.CreateClientAndServerAsync(async uri => [OuterLoop("Runs long")] [Theory] - [InlineData(HttpKeepAlivePingPolicy.Always)] - [InlineData(HttpKeepAlivePingPolicy.WithActiveRequests)] - public async Task KeepAliveConfigured_KeepAlivePingsAreSentAccordingToPolicy(HttpKeepAlivePingPolicy policy) + [InlineData(HttpKeepAlivePingPolicy.Always, -1)] + [InlineData(HttpKeepAlivePingPolicy.WithActiveRequests, -1)] + [InlineData(HttpKeepAlivePingPolicy.WithActiveRequests, 0)] + public async Task KeepAliveConfigured_KeepAlivePingsAreSentAccordingToPolicy(HttpKeepAlivePingPolicy policy, int connectionLifetimeMilliseconds) { await Http2LoopbackServer.CreateClientAndServerAsync(async uri => { @@ -105,6 +106,7 @@ await Http2LoopbackServer.CreateClientAndServerAsync(async uri => handler.KeepAlivePingTimeout = TimeSpan.FromSeconds(10); handler.KeepAlivePingPolicy = policy; handler.KeepAlivePingDelay = TimeSpan.FromSeconds(1); + handler.PooledConnectionLifetime = TimeSpan.FromMilliseconds(connectionLifetimeMilliseconds); using HttpClient client = new HttpClient(handler); client.DefaultRequestVersion = HttpVersion.Version20;