diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectHelper.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectHelper.cs index 7f88b005bfdfd6..288022837e8321 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectHelper.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectHelper.cs @@ -136,7 +136,12 @@ public static ValueTask EstablishSslConnectionAsync(SslClientAuthenti Func localFromHttpClientHandler = mapper.FromHttpClientHandler; HttpRequestMessage localRequest = request; sslOptions.RemoteCertificateValidationCallback = (object sender, X509Certificate? certificate, X509Chain? chain, SslPolicyErrors sslPolicyErrors) => - localFromHttpClientHandler(localRequest, certificate as X509Certificate2, chain, sslPolicyErrors); + { + Debug.Assert(localRequest != null); + bool result = localFromHttpClientHandler(localRequest, certificate as X509Certificate2, chain, sslPolicyErrors); + localRequest = null!; // ensure the SslOptions and this callback don't keep the first HttpRequestMessage alive indefinitely + return result; + }; } // Create the SslStream, authenticate, and return it.