Skip to content

Commit b876d4e

Browse files
rzikmCopilot
andauthored
Defer WinHTTP HTTP/2 loopback connection closure until server disposal (#134181)
<!-- --> WinHTTP can discard buffered response data when the loopback server sends FIN before the client consumes it, causing error 12030. This affects more than cookie tests: the same failure signature also appeared in `ClientCertificateTest.UseClientCertOnHttp2_OSSupportsIt_Success`. Synchronizing individual tests leaves other paths exposed. This change moves lifetime management into the shared HTTP/2 loopback infrastructure. In WinHTTP tests, connection disposal sends GOAWAY but retains the transport until the enclosing server is disposed, normally after both client and server callbacks complete. Retired connections do not prevent subsequent connections from being accepted. Automatic GOAWAY respects earlier last-stream boundaries, and cleanup tolerates the server closing a connection during asynchronous disposal. The behavior is enabled by default only for WinHTTP tests. Explicit shutdown methods remain immediate, and tests requiring the original disposal behavior can set `DeferConnectionClose = false`. The HTTP/2 cookie tests quarantined under #126867 are re-enabled; no cookie-specific synchronization or product code changes are needed. ### Validation - WinHTTP functional suites: 1,749 tests on .NET 11 and 667 on .NET Framework, with zero failures and 41/28 existing skips respectively. - WinHTTP unit tests: 173 passed. - SocketsHttpHandler HTTP/2 protocol and cookie tests: 212 passed. - Ten repetitions of the affected cookie, certificate, and lifetime tests: 800 passes, no failures or skips. - Regression tests verify that responses remain readable after connection disposal and that physical closure during deferred disposal completes safely. Both regressions were checked against the corresponding unfixed behavior. These verify the lifetime contract, not a deterministic reproduction of native error 12030. Fixes #133779 Fixes #130458 Fixes #126867 ... and countless others that boil down to "Error 12030 calling WINHTTP_CALLBACK_STATUS_REQUEST_ERROR, 'The connection with the server was terminated abnormally'." > [!NOTE] > This pull request description and changes were generated with GitHub Copilot. Co-authored-by: Copilot App <[email protected]>
1 parent 185491d commit b876d4e

4 files changed

Lines changed: 213 additions & 12 deletions

File tree

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

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public class Http2LoopbackConnection : GenericLoopbackConnection
2828
private readonly TimeSpan _timeout;
2929
private int _lastStreamId;
3030
private bool _expectClientDisconnect;
31+
private bool _closeDeferred;
32+
private int _lastRequestStreamId;
33+
private int _lastGoAwayStreamId = int.MaxValue;
34+
private int _closed;
3135
private readonly SemaphoreSlim? _readLock;
3236
private readonly SemaphoreSlim? _writeLock;
3337

@@ -36,6 +40,8 @@ public class Http2LoopbackConnection : GenericLoopbackConnection
3640
public bool IsInvalid => _connectionSocket == null;
3741
public Stream Stream => _connectionStream;
3842
public Task<bool> SettingAckWaiter => _ignoredSettingsAckPromise?.Task;
43+
internal bool DeferClose { get; set; }
44+
internal bool IsCloseDeferred => _closeDeferred;
3945

4046
private Http2LoopbackConnection(SocketWrapper socket, Stream stream, TimeSpan timeout, Http2Options httpOptions)
4147
{
@@ -179,9 +185,10 @@ public async Task SendConnectionPrefaceAsync()
179185

180186
public async Task WriteFrameAsync(Frame frame, CancellationToken cancellationToken = default)
181187
{
188+
Stream stream = _connectionStream ?? throw new ObjectDisposedException(nameof(Http2LoopbackConnection));
182189
byte[] writeBuffer = new byte[Frame.FrameHeaderLength + frame.Length];
183190
frame.WriteTo(writeBuffer);
184-
await _connectionStream.WriteAsync(writeBuffer, 0, writeBuffer.Length, cancellationToken).ConfigureAwait(false);
191+
await stream.WriteAsync(writeBuffer, 0, writeBuffer.Length, cancellationToken).ConfigureAwait(false);
185192
}
186193

187194
public async Task WriteFramesAsync(Frame[] frames, CancellationToken cancellationToken = default)
@@ -247,6 +254,10 @@ public async Task<Frame> ReadFrameAsync(CancellationToken cancellationToken)
247254
}
248255

249256
Frame header = Frame.ReadFrom(headerBytes);
257+
if (header.Type == FrameType.Headers)
258+
{
259+
_lastRequestStreamId = Math.Max(_lastRequestStreamId, header.StreamId);
260+
}
250261

251262
// Read the data segment of the frame, if it is present.
252263
byte[] data = new byte[header.Length];
@@ -799,6 +810,7 @@ public async Task<SettingsFrame> ReadSettingsAsync()
799810

800811
public async Task SendGoAway(int lastStreamId, ProtocolErrors errorCode = ProtocolErrors.NO_ERROR)
801812
{
813+
_lastGoAwayStreamId = Math.Min(_lastGoAwayStreamId, lastStreamId);
802814
GoAwayFrame frame = new GoAwayFrame(lastStreamId, (int)errorCode, new byte[] { }, 0);
803815
await WriteFrameAsync(frame).ConfigureAwait(false);
804816
}
@@ -932,13 +944,59 @@ public async Task SendResponseBodyAsync(int streamId, ReadOnlyMemory<byte> respo
932944

933945
public override async ValueTask DisposeAsync()
934946
{
947+
if (_closeDeferred)
948+
{
949+
return;
950+
}
951+
935952
// Might have been already shutdown manually via WaitForConnectionShutdownAsync which nulls the _connectionStream.
936953
if (_connectionStream != null)
937954
{
955+
if (DeferClose)
956+
{
957+
_closeDeferred = true;
958+
try
959+
{
960+
await SendGoAway(Math.Min(_lastRequestStreamId, _lastGoAwayStreamId)).ConfigureAwait(false);
961+
}
962+
catch (ObjectDisposedException) when (Volatile.Read(ref _closed) != 0)
963+
{
964+
// The server can close connections while a failing test is still unwinding.
965+
}
966+
catch (IOException)
967+
{
968+
// The client may already have closed the connection.
969+
}
970+
catch (SocketException)
971+
{
972+
// The client may already have closed the connection.
973+
}
974+
return;
975+
}
976+
938977
await ShutdownIgnoringErrorsAsync(_lastStreamId);
939978
}
940979
}
941980

981+
internal void Close()
982+
{
983+
if (Interlocked.Exchange(ref _closed, 1) != 0)
984+
{
985+
return;
986+
}
987+
988+
try
989+
{
990+
_connectionStream?.Dispose();
991+
}
992+
finally
993+
{
994+
_connectionSocket?.Dispose();
995+
_connectionStream = null;
996+
_connectionSocket = null;
997+
}
998+
}
999+
9421000
//
9431001
// GenericLoopbackServer implementation
9441002
//
@@ -1047,7 +1105,14 @@ public override async Task<HttpRequestData> HandleRequestAsync(HttpStatusCode st
10471105
await SendResponseBodyAsync(streamId, Encoding.ASCII.GetBytes(content)).ConfigureAwait(false);
10481106
}
10491107

1050-
await WaitForConnectionShutdownAsync().ConfigureAwait(false);
1108+
if (DeferClose)
1109+
{
1110+
await DisposeAsync().ConfigureAwait(false);
1111+
}
1112+
else
1113+
{
1114+
await WaitForConnectionShutdownAsync().ConfigureAwait(false);
1115+
}
10511116

10521117
return requestData;
10531118
}

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

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,36 @@ public Task<Http2LoopbackConnection> AcceptConnectionAsync()
8585

8686
public async Task<Http2LoopbackConnection> AcceptConnectionAsync(TimeSpan? timeout)
8787
{
88-
RemoveInvalidConnections();
89-
90-
if (!AllowMultipleConnections && _connections.Count != 0)
88+
Socket listenSocket;
89+
lock (_connections)
9190
{
92-
throw new InvalidOperationException("Connection already established. Set `AllowMultipleConnections = true` to bypass.");
91+
listenSocket = _listenSocket ?? throw new ObjectDisposedException(nameof(Http2LoopbackServer));
92+
RemoveInvalidConnections();
93+
94+
if (!AllowMultipleConnections && _connections.Exists(c => !c.IsCloseDeferred))
95+
{
96+
throw new InvalidOperationException("Connection already established. Set `AllowMultipleConnections = true` to bypass.");
97+
}
9398
}
9499

95-
Socket connectionSocket = await _listenSocket.AcceptAsync().ConfigureAwait(false);
100+
Socket connectionSocket = await listenSocket.AcceptAsync().ConfigureAwait(false);
96101

97102
var stream = new NetworkStream(connectionSocket, ownsSocket: true);
98103
var wrapper = new SocketWrapper(connectionSocket);
99104
Http2LoopbackConnection connection =
100105
timeout != null ? await Http2LoopbackConnection.CreateAsync(wrapper, stream, _options, timeout.Value).ConfigureAwait(false) :
101106
await Http2LoopbackConnection.CreateAsync(wrapper, stream, _options).ConfigureAwait(false);
102-
_connections.Add(connection);
107+
lock (_connections)
108+
{
109+
if (_listenSocket is null)
110+
{
111+
connection.Close();
112+
throw new ObjectDisposedException(nameof(Http2LoopbackServer));
113+
}
114+
115+
connection.DeferClose = _options.DeferConnectionClose;
116+
_connections.Add(connection);
117+
}
103118

104119
return connection;
105120
}
@@ -135,10 +150,22 @@ public async Task<Http2LoopbackConnection> EstablishConnectionAsync(TimeSpan? ti
135150

136151
public override void Dispose()
137152
{
138-
if (_listenSocket != null)
153+
lock (_connections)
139154
{
140-
_listenSocket.Dispose();
141-
_listenSocket = null;
155+
if (_listenSocket != null)
156+
{
157+
_listenSocket.Dispose();
158+
_listenSocket = null;
159+
}
160+
161+
if (_options.DeferConnectionClose)
162+
{
163+
foreach (Http2LoopbackConnection connection in _connections)
164+
{
165+
connection.Close();
166+
}
167+
_connections.Clear();
168+
}
142169
}
143170
}
144171

@@ -187,6 +214,16 @@ public class Http2Options : GenericLoopbackOptions
187214
public bool EnableTransparentPingResponse { get; set; } = true;
188215
public bool EnsureThreadSafeIO { get; set; }
189216

217+
// Transfer disposed connections to the server until its scope ends, after the client has consumed
218+
// its responses. WinHTTP can discard buffered data when the server sends FIN too early.
219+
// Tests requiring immediate closure can opt out or use explicit connection shutdown methods.
220+
public bool DeferConnectionClose { get; set; } =
221+
#if WINHTTPHANDLER_TEST
222+
true;
223+
#else
224+
false;
225+
#endif
226+
190227
public Http2Options()
191228
{
192229
SslProtocols = SslProtocols.Tls12;

‎src/libraries/System.Net.Http.WinHttpHandler/tests/FunctionalTests/PlatformHandlerTest.cs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ public PlatformHandler_HttpClientHandler_Authentication_Test(ITestOutputHelper o
181181
}
182182

183183
#if NET
184-
[ActiveIssue("https://github.com/dotnet/runtime/issues/126867", typeof(PlatformDetection), nameof(PlatformDetection.IsWindows), nameof(PlatformDetection.IsX64Process))]
185184
[ConditionalClass(typeof(PlatformDetection), nameof(PlatformDetection.IsWindows10Version1607OrGreater))]
186185
public sealed class PlatformHandlerTest_Cookies_Http2 : HttpClientHandlerTest_Cookies
187186
{

‎src/libraries/System.Net.Http.WinHttpHandler/tests/FunctionalTests/WinHttpHandlerTest.cs‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using System.Linq;
77
using System.Net.Http.Functional.Tests;
8+
using System.Net.Sockets;
89
using System.Net.Test.Common;
910
using System.Text;
1011
using System.Threading;
@@ -34,6 +35,105 @@ public WinHttpHandlerTest(ITestOutputHelper output)
3435
_output = output;
3536
}
3637

38+
#if !NETFRAMEWORK
39+
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsWindows10Version1607OrGreater))]
40+
[InlineData(false)]
41+
[InlineData(true)]
42+
public async Task GetAsync_Http2LoopbackConnectionDisposed_ResponseRemainsReadable(bool handleRequest)
43+
{
44+
const string Content = "Response read after the server finishes sending";
45+
var serverFinished = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
46+
Http2LoopbackConnection connection = null;
47+
48+
await Http2LoopbackServer.CreateClientAndServerAsync(async address =>
49+
{
50+
using var client = new HttpClient(new WinHttpHandler
51+
{
52+
ServerCertificateValidationCallback = TestHelper.AllowAllCertificates
53+
});
54+
using var request = new HttpRequestMessage(HttpMethod.Get, address) { Version = HttpVersion20.Value };
55+
using HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
56+
await serverFinished.Task.WaitAsync(TestHelper.PassingTestTimeout);
57+
58+
Assert.False(connection.IsInvalid);
59+
Assert.Equal(Content, await response.Content.ReadAsStringAsync());
60+
},
61+
async server =>
62+
{
63+
try
64+
{
65+
connection = await server.EstablishConnectionAsync();
66+
await using (connection)
67+
{
68+
if (handleRequest)
69+
{
70+
await connection.HandleRequestAsync(content: Content);
71+
}
72+
else
73+
{
74+
int streamId = await connection.ReadRequestHeaderAsync();
75+
await connection.SendResponseHeadersAsync(streamId, endStream: false);
76+
await connection.SendResponseBodyAsync(streamId, Encoding.ASCII.GetBytes(Content));
77+
}
78+
}
79+
80+
serverFinished.SetResult(true);
81+
}
82+
catch (Exception e)
83+
{
84+
serverFinished.TrySetException(e);
85+
throw;
86+
}
87+
});
88+
89+
Assert.True(connection.IsInvalid);
90+
}
91+
92+
[Fact]
93+
public async Task Http2LoopbackConnection_CloseDuringDeferredDispose_Completes()
94+
{
95+
var writeStarted = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
96+
var finishWrite = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
97+
bool closed = false;
98+
using var socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
99+
using var stream = new DelegateStream(
100+
canReadFunc: () => true,
101+
canWriteFunc: () => true,
102+
readAsyncFunc: (buffer, offset, count, token) =>
103+
{
104+
byte[] preface = Encoding.ASCII.GetBytes(Http2LoopbackConnection.Http2Prefix);
105+
Assert.Equal(preface.Length, count);
106+
preface.CopyTo(buffer, offset);
107+
return Task.FromResult(preface.Length);
108+
},
109+
writeAsyncFunc: async (buffer, offset, count, token) =>
110+
{
111+
writeStarted.TrySetResult(true);
112+
await finishWrite.Task.WaitAsync(TestHelper.PassingTestTimeout);
113+
Assert.True(closed);
114+
throw new ObjectDisposedException(nameof(DelegateStream));
115+
},
116+
disposeFunc: _ => closed = true);
117+
118+
Http2LoopbackConnection connection = await Http2LoopbackConnection.CreateAsync(
119+
new SocketWrapper(socket), stream, new Http2Options { UseSsl = false });
120+
connection.DeferClose = true;
121+
Task disposeTask = connection.DisposeAsync().AsTask();
122+
try
123+
{
124+
await writeStarted.Task.WaitAsync(TestHelper.PassingTestTimeout);
125+
connection.Close();
126+
}
127+
finally
128+
{
129+
finishWrite.TrySetResult(true);
130+
}
131+
132+
await disposeTask.WaitAsync(TestHelper.PassingTestTimeout);
133+
Assert.True(connection.IsInvalid);
134+
}
135+
#endif
136+
37137
[OuterLoop]
38138
[Fact]
39139
public void SendAsync_SimpleGet_Success()

0 commit comments

Comments
 (0)