Skip to content

Commit 247f6a7

Browse files
rzikmCopilot
andauthored
Avoid early HTTP/2 teardown in WinHTTP Latin1 header test (#133955)
## Summary Keep the HTTP/2 loopback connection alive until the Latin1 header test's buffered `HttpClient.SendAsync` and client disposal finish. The previous helper starts server-side send shutdown immediately after writing the final response, potentially racing pending WinHTTP native I/O. The new path is limited to **WinHTTP HTTP/2 in this one header-encoding theory**. It preserves request-body consumption, GOAWAY-before-response ordering, the empty 200/END_STREAM response, and all raw Latin1/Huffman header assertions. Client completion is signaled in `finally`; server failure cancels the request **before** asynchronous connection disposal. HTTP/1 and Sockets retain the existing server helper. There is no production change, retry, native-error suppression, timeout increase, test disabling, or change to global loopback teardown defaults. ## Evidence and limits The supplied [CoreCLR build 1594333](https://dev.azure.com/dnceng-public/public/_build/results?buildId=1594333), revision `6ed2ba318dcaa936da60f7d6bc9eb30cad54fc0c`, failed the Content/0x7f case with native 12030 during response buffering: [same-run console](https://helixr18s23ayyejvk1x8qcc.blob.core.windows.net/dotnet-runtime-refs-heads-main-a2037c901dc94f1d88/System.Net.Http.WinHttpHandler.Functional.Tests/1/console.55c334a6.log?helixlogtype=result). - Directly invoking the **unchanged archived CI test method** reproduced `API_QUERY_DATA_AVAILABLE / WINHTTP_CONNECTION_ERROR(12030)` at call 686. - A controlled reconstruction using those archived assemblies compared immediate shutdown with waiting for buffered client completion: **1 native 12030 in 10,000 ungated calls; 0 in 10,000 gated calls**. That controlled failure was at **API_WRITE_DATA**, not API_QUERY_DATA_AVAILABLE. Observer timestamps show server END_STREAM and send shutdown before the request-error callback. - The different native API phases and sparse failure count matter: this supports removing the test's unnecessary teardown race, but does **not** prove the internal native cause of the original CI failure or elimination of all WinHTTP 12030s. A separate bounded header-callback-delay experiment passed both arms and was inconclusive. This is a test-lifetime correction for header encoding, not a change to how WinHTTP handles arbitrary server disconnects. Cookie/client-certificate 12030s and badssl 12152/12002 diagnostics remain separate. ## Local validation Windows 11 build 26100, x64 CoreCLR Release with locally built Debug libraries/tests; actual runtime 12.0.0, test TFM net11.0-windows. Reused the successful clr+libs baseline through a subst path. | Final-source validation | Result | |---|---| | WinHTTP full functional suite, CoreCLR | 1,631 passed, 41 existing skips, zero failures | | WinHTTP full functional suite, .NET Framework net481 | 639 passed, 28 existing skips, zero failures | | Actual fixed Content/0x7f method, filtered native tracing enabled | 10,000/10,000 passed | | Shared Sockets Latin1 theory, unmodified HEAD and final source | Both 70 passed / same two HTTP/1 host-software-aborted failures | The Sockets async failure is tracked by #132004; its sync counterpart had the same signature. Both were reproduced with the unmodified HEAD test compiled via an artifact-only source override. Neither failure enters the new WinHTTP HTTP/2 branch, and neither was suppressed. Artifact-only failure injection into the retained method also exercised success, client assertion failure, server failure before/after the response, cancellation with an established connection, client timeout, and completion-gate timeout. All seven preserved their expected outcomes and completed both cleanup paths, including awaited server disposal, within 0.7 seconds. The final uninstrumented source was then explicitly rebuilt and the full suites and 10,000-call replay run again. Final WinHTTP build: zero warnings/errors. No x86, NativeAOT, older-Windows, or non-Windows execution is claimed. Framework coverage compiles the shared change but does not execute the HTTP/2 gate. Existing graceful-disposal/factory timeout limitations are not globally hardened by this patch. Resolves #130458 > [!NOTE] > This change and description were developed with GitHub Copilot; validation results were collected locally. Co-authored-by: Copilot App <[email protected]>
1 parent 0b942f4 commit 247f6a7

1 file changed

Lines changed: 63 additions & 22 deletions

File tree

‎src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs‎

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2248,39 +2248,80 @@ public async Task SendAsync_RequestWithLatin1HeaderValue_Succeeds(char safeChar,
22482248
return; // SocketsHttpHandler doesn't support Latin-1 characters in headers without setting header encoding.
22492249
}
22502250
var headerValue = $"HeaderValue{safeChar}WithSafeChar";
2251+
var clientFinished = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
2252+
using var cancellation = new CancellationTokenSource();
22512253
await LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
22522254
{
2253-
var handler = CreateHttpClientHandler();
2254-
using (var client = new HttpClient(handler))
2255+
try
22552256
{
2256-
var request = new HttpRequestMessage(HttpMethod.Get, uri);
2257-
request.Version = UseVersion;
2258-
switch (headerType)
2257+
var handler = CreateHttpClientHandler();
2258+
using (var client = new HttpClient(handler))
22592259
{
2260-
case HeaderType.Request:
2261-
request.Headers.Add("Custom-Header", headerValue);
2262-
break;
2263-
case HeaderType.Content:
2264-
request.Content = new StringContent("test content");
2265-
request.Content.Headers.Add("Custom-Content-Header", headerValue);
2266-
break;
2267-
case HeaderType.Cookie:
2260+
using var request = new HttpRequestMessage(HttpMethod.Get, uri);
2261+
request.Version = UseVersion;
2262+
switch (headerType)
2263+
{
2264+
case HeaderType.Request:
2265+
request.Headers.Add("Custom-Header", headerValue);
2266+
break;
2267+
case HeaderType.Content:
2268+
request.Content = new StringContent("test content");
2269+
request.Content.Headers.Add("Custom-Content-Header", headerValue);
2270+
break;
2271+
case HeaderType.Cookie:
22682272
#if WINHTTPHANDLER_TEST
2269-
handler.CookieUsePolicy = CookieUsePolicy.UseSpecifiedCookieContainer;
2273+
handler.CookieUsePolicy = CookieUsePolicy.UseSpecifiedCookieContainer;
22702274
#endif
2271-
handler.CookieContainer = new CookieContainer();
2272-
handler.CookieContainer.Add(uri, new Cookie("CustomCookie", headerValue));
2273-
break;
2274-
}
2275+
handler.CookieContainer = new CookieContainer();
2276+
handler.CookieContainer.Add(uri, new Cookie("CustomCookie", headerValue));
2277+
break;
2278+
}
22752279

2276-
using (HttpResponseMessage response = await client.SendAsync(request))
2277-
{
2278-
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
2280+
using (HttpResponseMessage response = await client.SendAsync(request, cancellation.Token))
2281+
{
2282+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
2283+
}
22792284
}
22802285
}
2286+
finally
2287+
{
2288+
clientFinished.SetResult(true);
2289+
}
22812290
}, async server =>
22822291
{
2283-
var data = await server.AcceptConnectionSendResponseAndCloseAsync();
2292+
HttpRequestData data;
2293+
if (IsWinHttpHandler && UseVersion.Major == 2)
2294+
{
2295+
GenericLoopbackConnection genericConnection = null;
2296+
try
2297+
{
2298+
genericConnection = await server.EstablishGenericConnectionAsync();
2299+
var connection = (Http2LoopbackConnection)genericConnection;
2300+
(int streamId, data) = await connection.ReadAndParseRequestHeaderAsync();
2301+
await connection.SendGoAway(streamId);
2302+
await connection.SendResponseHeadersAsync(streamId, endStream: true);
2303+
2304+
// WinHTTP can fail pending native I/O if the server closes before the buffered send completes.
2305+
await clientFinished.Task.WaitAsync(TestHelper.PassingTestTimeout);
2306+
await connection.WaitForConnectionShutdownAsync();
2307+
}
2308+
catch
2309+
{
2310+
cancellation.Cancel();
2311+
throw;
2312+
}
2313+
finally
2314+
{
2315+
if (genericConnection is not null)
2316+
{
2317+
await genericConnection.DisposeAsync();
2318+
}
2319+
}
2320+
}
2321+
else
2322+
{
2323+
data = await server.AcceptConnectionSendResponseAndCloseAsync();
2324+
}
22842325
switch (headerType)
22852326
{
22862327
case HeaderType.Request:

0 commit comments

Comments
 (0)