Skip to content

Commit 6cd25fb

Browse files
authored
Release the stream once the response is sent (#90659)
1 parent 8e3376c commit 6cd25fb

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

src/libraries/Common/tests/System/Net/Http/Http3LoopbackConnection.cs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,6 @@ public static int GetRequestId(QuicStream stream)
108108
return checked((int)stream.Id + 1);
109109
}
110110

111-
public Http3LoopbackStream GetOpenRequest(int requestId = 0)
112-
{
113-
return requestId == 0 ? _currentStream : _openStreams[requestId - 1];
114-
}
115-
116111
public override Task InitializeConnectionAsync()
117112
{
118113
throw new NotImplementedException();
@@ -195,6 +190,17 @@ public async Task EstablishControlStreamAsync(SettingsEntry[] settingsEntries)
195190
await _outboundControlStream.SendSettingsFrameAsync(settingsEntries);
196191
}
197192

193+
public async Task DisposeCurrentStream()
194+
{
195+
Assert.NotNull(_currentStream);
196+
Assert.True(_currentStreamId >= 0);
197+
198+
await _currentStream.DisposeAsync().ConfigureAwait(false);
199+
_openStreams.Remove((int)_currentStreamId);
200+
_currentStream = null;
201+
_currentStreamId = -4;
202+
}
203+
198204
public override async Task<byte[]> ReadRequestBodyAsync()
199205
{
200206
return await _currentStream.ReadRequestBodyAsync().ConfigureAwait(false);
@@ -206,24 +212,32 @@ public override async Task<HttpRequestData> ReadRequestDataAsync(bool readBody =
206212
return await stream.ReadRequestDataAsync(readBody).ConfigureAwait(false);
207213
}
208214

209-
public override Task SendResponseAsync(HttpStatusCode statusCode = HttpStatusCode.OK, IList<HttpHeaderData> headers = null, string content = "", bool isFinal = true)
215+
public override async Task SendResponseAsync(HttpStatusCode statusCode = HttpStatusCode.OK, IList<HttpHeaderData> headers = null, string content = "", bool isFinal = true)
210216
{
211-
return GetOpenRequest().SendResponseAsync(statusCode, headers, content, isFinal);
217+
await _currentStream.SendResponseAsync(statusCode, headers, content, isFinal);
218+
if (isFinal)
219+
{
220+
await DisposeCurrentStream().ConfigureAwait(false);
221+
}
212222
}
213223

214-
public override Task SendResponseBodyAsync(byte[] content, bool isFinal = true)
224+
public override async Task SendResponseBodyAsync(byte[] content, bool isFinal = true)
215225
{
216-
return GetOpenRequest().SendResponseBodyAsync(content, isFinal);
226+
await _currentStream.SendResponseBodyAsync(content, isFinal);
227+
if (isFinal)
228+
{
229+
await DisposeCurrentStream().ConfigureAwait(false);
230+
}
217231
}
218232

219233
public override Task SendResponseHeadersAsync(HttpStatusCode statusCode = HttpStatusCode.OK, IList<HttpHeaderData> headers = null)
220234
{
221-
return GetOpenRequest().SendResponseHeadersAsync(statusCode, headers);
235+
return _currentStream.SendResponseHeadersAsync(statusCode, headers);
222236
}
223237

224238
public override Task SendPartialResponseHeadersAsync(HttpStatusCode statusCode = HttpStatusCode.OK, IList<HttpHeaderData> headers = null)
225239
{
226-
return GetOpenRequest().SendPartialResponseHeadersAsync(statusCode, headers);
240+
return _currentStream.SendPartialResponseHeadersAsync(statusCode, headers);
227241
}
228242

229243
public override async Task<HttpRequestData> HandleRequestAsync(HttpStatusCode statusCode = HttpStatusCode.OK, IList<HttpHeaderData> headers = null, string content = "")
@@ -310,7 +324,7 @@ public async Task WaitForClientDisconnectAsync(bool refuseNewRequests = true)
310324

311325
public override async Task WaitForCancellationAsync(bool ignoreIncomingData = true)
312326
{
313-
await GetOpenRequest().WaitForCancellationAsync(ignoreIncomingData).ConfigureAwait(false);
327+
await _currentStream.WaitForCancellationAsync(ignoreIncomingData).ConfigureAwait(false);
314328
}
315329

316330
public override Task WaitForCloseAsync(CancellationToken cancellationToken)

0 commit comments

Comments
 (0)