diff --git a/src/protocols/ioxide.http2/Http2BodyReader.cs b/src/protocols/ioxide.http2/Http2BodyReader.cs index bc84bd78..a8f77dad 100644 --- a/src/protocols/ioxide.http2/Http2BodyReader.cs +++ b/src/protocols/ioxide.http2/Http2BodyReader.cs @@ -23,6 +23,14 @@ namespace ioxide.http2; /// /// The window is the stream's own. The connection's is opened to its maximum and kept there, so a /// handler that never reads holds back only its own upload, never the other streams'. +/// +/// A handler that answers without reading its body to the end leaves the rest to the connection +/// once it returns: while its response still waits on the client's credit, what arrives is credited +/// and dropped, and once the response has ended a request still arriving is reset with NO_ERROR. A +/// client that reads its response only once its upload has ended, as .NET's HttpClient does +/// with a content that is not duplex, can then finish both. A handler that instead waits on +/// for that credit, its body unread, waits until such a +/// client gives up. /// /// /// Single consumer, on any thread: a read made off the reactor is served on it. Each @@ -88,12 +96,18 @@ public ValueTask> ReadAsync() // Body bytes straight off a DATA frame. The span points into the connection's accumulator and // dies when the parser moves on, so it has to be copied. Never completes a parked reader - // inline: the wake is deferred to FireIfReady, after the parser unwinds. - internal void Push(ReadOnlySpan data) + // inline: the wake is deferred to FireIfReady, after the parser unwinds. False once the body has + // ended: its handler is done with it, and the caller credits bytes nobody will read. + internal bool Push(ReadOnlySpan data) { + if (_ended) + { + return false; + } + if (data.IsEmpty) { - return; + return true; } byte[] buffer = ArrayPool.Shared.Rent(data.Length); @@ -104,6 +118,7 @@ internal void Push(ReadOnlySpan data) { _owner.NoteBodyWake(this); } + return true; } /// End of body: END_STREAM, or a cut one once has said so. Idempotent. @@ -175,11 +190,12 @@ internal void Abort(string? reason = null) } // The handler is done with the request, or never got it: recycle everything, the chunk it held - // included, and complete a read it left parked. + // included, and complete a read it left parked. Ended, so DATA still on its way is credited as it + // arrives while the stream stays open for a response waiting on credit. internal void Drop() { ReleaseHandedOut(); - DropQueued(); + _owner.CreditBody(_streamId, DropQueued()); // Drained first, so the wake below reports end-of-body rather than handing out a chunk // whose stream is already gone. @@ -187,13 +203,16 @@ internal void Drop() FireIfReady(); } - // Owed no credit: the connection was charged for these on arrival, and their stream is done. - private void DropQueued() + // The connection was charged for these on arrival; only a stream still open is owed them, which CreditBody checks. + private int DropQueued() { + int dropped = 0; while (_chunks.TryDequeue(out (byte[] Buffer, int Length) chunk)) { + dropped += chunk.Length; ArrayPool.Shared.Return(chunk.Buffer); } + return dropped; } private void ReleaseHandedOut() diff --git a/src/protocols/ioxide.http2/Http2Connection.Frames.cs b/src/protocols/ioxide.http2/Http2Connection.Frames.cs index a49d89e9..878ea6c7 100644 --- a/src/protocols/ioxide.http2/Http2Connection.Frames.cs +++ b/src/protocols/ioxide.http2/Http2Connection.Frames.cs @@ -404,13 +404,7 @@ private void HandleData(in FrameHeader header, ReadOnlySpan payload) if (!_streams.TryGetValue(header.StreamId, out PendingRequest? pending)) { - // A buffered answer parked on credit still has its upload credited, so a client that credits the - // answer only once its upload ends can finish both. Any other stream's window went with it. - if (payload.Length > 0 && !ended && _responseWindows.ContainsKey(header.StreamId)) - { - WriteWindowUpdate(header.StreamId, payload.Length); - } - return; + return; // closed, so owed no credit (RFC 9113 5.1): a request whose answer waits on credit stays in _streams } // Past its window is a stream error (6.9), and its response stops with it; an empty frame may still end it (6.9.1). @@ -433,9 +427,14 @@ private void HandleData(in FrameHeader header, ReadOnlySpan payload) return; } + // Consumed now: a buffered body, one its handler is done with, or a streamed one's padding, its body credited as read. + int credit = payload.Length; if (pending.BodyReader is { } reader) { - reader.Push(body); + if (reader.Push(body)) + { + credit -= body.Length; + } } else { @@ -447,9 +446,8 @@ private void HandleData(in FrameHeader header, ReadOnlySpan payload) pending.AppendBody(body); } - // Consumed now: a buffered body, or a streamed one's padding, its body credited as read; an ended request is owed none. - int credit = pending.BodyReader is null ? payload.Length : payload.Length - body.Length; - if (credit > 0 && !ended) + // An ended request is owed none. + if (credit > 0 && !ended && !pending.RequestEnded) { CreditStream(pending, credit); } diff --git a/src/protocols/ioxide.http2/Http2Connection.Streamed.cs b/src/protocols/ioxide.http2/Http2Connection.Streamed.cs index 4167da9d..d1b6dd27 100644 --- a/src/protocols/ioxide.http2/Http2Connection.Streamed.cs +++ b/src/protocols/ioxide.http2/Http2Connection.Streamed.cs @@ -98,10 +98,17 @@ private async Task ServeStreamedAsync(Func body = response.Body; // a null response throws here, with the window still there for the 500 // Gone once the peer has reset the stream, and nothing may follow a reset (RFC 9113 5.1). if (!_responseWindows.Remove(streamId, out int window)) { - return; + return null; } WriteHeaders(streamId, response, endStream: body.Length == 0); - if (body.Length > 0) - { - WriteData(streamId, body.Span, window); - } + return body.Length > 0 ? WriteData(streamId, body.Span, window) : null; } private void WriteHeaders(int streamId, Http2Response response, bool endStream) @@ -426,7 +424,7 @@ private void WriteHeaders(int streamId, Http2Response response, bool endStream) } } - private void WriteData(int streamId, ReadOnlySpan body, int window) + private Http2ResponseWriter? WriteData(int streamId, ReadOnlySpan body, int window) { int offset = 0; @@ -441,8 +439,7 @@ private void WriteData(int streamId, ReadOnlySpan body, int window) { // Out of credit: the rest waits for a WINDOW_UPDATE the way a streamed response does. _responseWindows[streamId] = window; - ParkRest(streamId, body[offset..]); - return; + return ParkRest(streamId, body[offset..]); } bool last = offset + chunk >= body.Length; @@ -457,35 +454,17 @@ private void WriteData(int streamId, ReadOnlySpan body, int window) window -= chunk; offset += chunk; } + return null; } // Copied into a writer, because the body is the handler's again once it has returned. - private void ParkRest(int streamId, ReadOnlySpan rest) + private Http2ResponseWriter ParkRest(int streamId, ReadOnlySpan rest) { Http2ResponseWriter writer = RentWriter(streamId); writer.ContinueBody(); rest.CopyTo(writer.GetSpan(rest.Length)); writer.Advance(rest.Length); - - _ = CompleteParkedAsync(writer); - } - - private async Task CompleteParkedAsync(Http2ResponseWriter writer) - { - try - { - await writer.CompleteAsync(); - } - catch - { - // FlushAsync has already marked the connection broken; the read loop sees it. - } - finally - { - _creditWaiters.Remove(writer.StreamId); - _responseWindows.Remove(writer.StreamId); - _writerPool.Push(writer); - } + return writer; } // Three ASCII digits: HTTP/2 has no reason phrase and every valid status is 100..599. diff --git a/src/protocols/ioxide.http2/Http2Connection.cs b/src/protocols/ioxide.http2/Http2Connection.cs index 1a2a338d..7bf8fb63 100644 --- a/src/protocols/ioxide.http2/Http2Connection.cs +++ b/src/protocols/ioxide.http2/Http2Connection.cs @@ -1,5 +1,6 @@ using System.Buffers; using System.IO.Pipelines; +using System.Runtime.CompilerServices; namespace ioxide.http2; @@ -331,13 +332,14 @@ private void DispatchBuffered(Func> handl // there is no Task to allocate. if (inFlight.IsCompletedSuccessfully) { + Http2ResponseWriter? parked = null; try { - WriteResponse(pending.StreamId, inFlight.Result); + parked = WriteResponse(pending.StreamId, inFlight.Result); } finally { - RetireStream(pending); + Retire(pending, parked); } continue; } @@ -358,11 +360,12 @@ private void DispatchBuffered(Func> handl /// private async Task CompleteBufferedAsync(ValueTask inFlight, Http2Request request, PendingRequest pending) { + Http2ResponseWriter? parked = null; try { Http2Response response = await inFlight; await new ReactorSwitch(_reactor); // back on the reactor, wherever the handler finished - WriteResponse(pending.StreamId, response); + parked = WriteResponse(pending.StreamId, response); } catch (Exception exception) { @@ -373,12 +376,12 @@ private async Task CompleteBufferedAsync(ValueTask inFlight, Http if (!IsBroken) { - WriteResponse(pending.StreamId, new Http2Response { Status = 500 }); + WriteResponse(pending.StreamId, new Http2Response { Status = 500 }); // no body, so nothing parks } } finally { - RetireStream(pending); + Retire(pending, parked); await MaybeFlushAsync(); } } @@ -418,7 +421,7 @@ private void RetireStream(PendingRequest pending) // Every reset takes the stream out of _streams; short of one, a streamed writer has staged END_STREAM. bool live = _streams.Remove(pending.StreamId) && !IsBroken; - // A buffered rest parked on credit keeps its window: its END_STREAM is still to go. + // A response parked on credit keeps its window: its END_STREAM is still to go. bool reset = live && !pending.RequestEnded && !_responseWindows.ContainsKey(pending.StreamId); if (reset) @@ -435,6 +438,68 @@ private void RetireStream(PendingRequest pending) } } + // A handler is done with its stream: retired now, unless the rest of its response waits on credit in a writer. + private void Retire(PendingRequest pending, Http2ResponseWriter? parked) + { + if (parked is null) + { + RetireStream(pending); + } + else + { + RetireAfterEnd(parked, pending); + } + } + + /// + /// A handler is done, and the end of its response waits on the peer's credit. Its stream stays open behind it: + /// a request still arriving is drained meanwhile, credited and dropped, since a client that reads its response + /// only once its upload has ended credits nothing before then. The stream is retired once the end is out, and + /// reset with NO_ERROR after it if the request is still arriving then (RFC 9113 8.1). + /// + [MethodImpl(MethodImplOptions.NoInlining)] + private void RetireAfterEnd(Http2ResponseWriter writer, PendingRequest pending) + { + PendingRequest? open = null; + if (!IsBroken && _streams.ContainsKey(pending.StreamId)) + { + pending.BodyReader?.Drop(); // credits what the handler left unread, and DATA still to come as it lands + open = pending; + } + else + { + RetireStream(pending); // reset, broken, or a whole body that left _streams once it arrived: no stream to hold open + } + + _ = FinishParkedAsync(writer, open); + } + + private async Task FinishParkedAsync(Http2ResponseWriter writer, PendingRequest? open) + { + try + { + await writer.CompleteAsync(); // a buffered rest starts here; a streamed handler's writer has completed already + if (writer.ParkedEnd is { } end) + { + await end; + } + } + catch + { + // FlushAsync has already marked the connection broken; the read loop sees it. + } + finally + { + _creditWaiters.Remove(writer.StreamId); + _responseWindows.Remove(writer.StreamId); + if (open is not null) + { + RetireStream(open); + } + _writerPool.Push(writer); + } + } + // Kept unless the pool is full or a peer's header lines grew its lists; only the reactor rents. private void Recycle(PendingRequest pending) { @@ -445,7 +510,7 @@ private void Recycle(PendingRequest pending) } } - // Ended before its request did: without a body reader it was never dispatched; with one, its handler retires it. + // Ended before its request did: without a body reader it was never dispatched; with one, its handler or the response it left on credit retires it. private void Discard(PendingRequest pending) { if (pending.BodyReader is null) diff --git a/src/protocols/ioxide.http2/Http2ResponseWriter.cs b/src/protocols/ioxide.http2/Http2ResponseWriter.cs index 3ebd5c49..fbd0d77f 100644 --- a/src/protocols/ioxide.http2/Http2ResponseWriter.cs +++ b/src/protocols/ioxide.http2/Http2ResponseWriter.cs @@ -55,9 +55,15 @@ internal Http2ResponseWriter(Http2Connection connection, int streamId) /// The stream this response belongs to. public int StreamId => _streamId; - /// True once the response has ended: its body finished and END_STREAM sent, or its stream reset by . + /// + /// True once the response has ended: has returned, its END_STREAM sent or left to + /// the connection to send as the peer's credit allows, or has reset its stream. + /// public bool IsCompleted => _completed; + /// The rest of a completed response and its END_STREAM, still waiting on the peer's credit; the stream is retired behind it. + internal Task? ParkedEnd { get; private set; } + /// /// Send the response headers. Exactly once, before any body byte - HTTP/2 puts HEADERS ahead /// of DATA and there is no correcting it later. @@ -160,8 +166,8 @@ public void Advance(int count) /// growing a queue behind it. /// /// - /// once the peer has reset the stream or the connection is - /// gone, as a TCP pipe writer reports a closed peer: nothing written from then on reaches anyone. + /// once the response has ended, the peer has reset the stream or the + /// connection is gone, as a TCP pipe writer reports a closed peer: nothing written from then on reaches anyone. /// /// Each call has to complete before the next one is made. public ValueTask FlushAsync() => FlushCore(endStream: false); @@ -169,6 +175,10 @@ public void Advance(int count) /// /// Send what is left and mark END_STREAM. A handler that returns without calling this gets it /// called for it - the peer is owed an end either way. + /// + /// It does not wait for the peer's credit, as does: what the peer's windows cannot + /// take yet is left to the connection, which sends it, END_STREAM last, as credit arrives. A client that reads + /// its response only once its upload has ended gives that credit only then, and the handler is not held until it does. /// public async ValueTask CompleteAsync() { @@ -185,10 +195,10 @@ public async ValueTask CompleteAsync() await FlushCore(endStream: true); _completed = true; - if (_staging.Length > 0) + // A parked end sends from the staging still, and gives it back once it is out. + if (ParkedEnd is null) { - ArrayPool.Shared.Return(_staging); - _staging = []; + ReleaseStaging(); } } @@ -204,8 +214,8 @@ public async ValueTask CompleteAsync() /// no longer wanted rather than one that failed. /// /// - /// Once the response has completed, the peer has reset the stream or the connection is gone, it - /// sends nothing. Either way the response has ended: is true, + /// Once the response has completed, its end left to the connection included, the peer has reset the stream or + /// the connection is gone, it sends nothing. Either way the response has ended: is true, /// reports the peer gone, and nothing more is sent when the handler /// returns. The request body ends with the stream: a read on it, parked or still to come, raises /// as for a body cut off. Before the headers it resets the stream too; a handler that wants a @@ -270,7 +280,7 @@ internal ValueTask FailAsync() private ValueTask FlushCore(bool endStream) { var onReactor = new ReactorSwitch(_connection.Reactor); - if (!_headersSent || !onReactor.IsCompleted) + if (_completed || !_headersSent || !onReactor.IsCompleted) // completed: a parked end still sends from the staging { return FlushCoreAsync(endStream); } @@ -323,11 +333,36 @@ private ValueTask Send(bool endStream) return SendStaged(endStream, ref sent) switch { Progress.Sent => Finish(endStream, sent), + Progress.Stalled when endStream => Park(sent), Progress.Stalled => SendAfterCreditAsync(endStream, sent), _ => new ValueTask(PeerGone), }; } + // The end out of credit goes on without its caller, which a client crediting only after its upload would hold for good. + [MethodImpl(MethodImplOptions.NoInlining)] + private ValueTask Park(int sent) + { + ParkedEnd = EndAfterCreditAsync(sent); + return default; + } + + private async Task EndAfterCreditAsync(int sent) + { + try + { + await SendAfterCreditAsync(endStream: true, sent); + } + catch + { + // FlushAsync has already marked the connection broken; the read loop sees it. + } + finally + { + ReleaseStaging(); + } + } + // Everything staged from `sent` on, as far as credit allows. private Progress SendStaged(bool endStream, ref int sent) { @@ -452,6 +487,15 @@ private void EnsureStaging(int sizeHint) _staging = grown; } + private void ReleaseStaging() + { + if (_staging.Length > 0) + { + ArrayPool.Shared.Return(_staging); + _staging = []; + } + } + /// Carry on a buffered response whose headers are already out, for the body its windows could not take. internal void ContinueBody() => _headersSent = true; @@ -464,5 +508,6 @@ internal void Reset(int streamId) _headersSent = false; _completed = false; _unsent = null; + ParkedEnd = null; } } diff --git a/tests/Ioxide.Tests.E2E/Protocols/EarlyAnswerTests.cs b/tests/Ioxide.Tests.E2E/Protocols/EarlyAnswerTests.cs index 66b904de..cff9777b 100644 --- a/tests/Ioxide.Tests.E2E/Protocols/EarlyAnswerTests.cs +++ b/tests/Ioxide.Tests.E2E/Protocols/EarlyAnswerTests.cs @@ -16,10 +16,13 @@ internal static class EarlyAnswerTests // A control's upload, read whole by its handler. private const long ReadUpload = 8L << 20; - // Measured stops are 64 KiB on h2, the connection window, and 192 KiB on h3; these leave room for a slow box. + // Measured stops are up to 320 KiB on h2, the stream window and what the client had buffered, and 192 KiB on h3; these leave room for a slow box. private const long H2Stopped = 512L << 10; private const long H3Stopped = 1L << 20; + // A duplex upload is credited while the answer waits on the client's credit, so it stops later: measured up to 2.5 MiB. + private const long H2DrainedStopped = 32L << 20; + // What the raw client offers to upload after the request's headers: past it, nothing stopped the upload. private const long RawBudget = 4L << 20; @@ -28,9 +31,69 @@ internal static class EarlyAnswerTests public static void Register(Runner runner) { RegisterHttpClient(runner); + RegisterPastWindow(runner); RegisterRawClient(runner); } + // Past the client's 64 KiB stream window, which this client credits only once its upload ends: the unread upload has to be credited first. + private static void RegisterPastWindow(Runner runner) + { + byte[] answer = Pattern(256 * 1024); + + runner.Test("h2 early answer: a streamed 413 past the client's window, its handler returning CompleteAsync, arrives whole, and the upload ends", () => + { + int port = StartH2(connection => connection.RunAsync((_, _, writer) => AnswerAsync(writer, answer))); + using HttpClient client = H2Client(); + var upload = new CountedUpload(Upload); + AssertAnsweredAndEnded(PostAsync(client, $"http://127.0.0.1:{port}/", upload), upload, answer); + }); + + runner.Test("h2 early answer: a streamed 413 past the client's window, left to the stack to complete, arrives whole, and the upload ends", () => + { + int port = StartH2(connection => connection.RunAsync((_, _, writer) => + { + writer.WriteHeaders(new Http2Response { Status = 413 }); + writer.Write(answer); + return default; + })); + using HttpClient client = H2Client(); + var upload = new CountedUpload(Upload); + AssertAnsweredAndEnded(PostAsync(client, $"http://127.0.0.1:{port}/", upload), upload, answer); + }); + + runner.Test("h2 early answer: a buffered 413 past the client's window, returned once the upload has stalled, arrives whole, and the upload ends", () => + { + var stalled = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + int port = StartH2(connection => connection.RunAsync(async (_, _) => + { + await stalled.Task; + return new Http2Response { Status = 413, Body = answer }; + })); + using HttpClient client = H2Client(); + var upload = new CountedUpload(Upload); + Task<(HttpStatusCode Status, byte[] Body)> answered = PostAsync(client, $"http://127.0.0.1:{port}/", upload); + + WaitStalled(upload); + Assert.True(upload.Sent < Upload, $"precondition: the upload was meant to stall on the window its unread body was given, not run to {upload.Sent} bytes"); + stalled.SetResult(); + AssertAnsweredAndEnded(answered, upload, answer); + }); + + foreach (bool streamed in new[] { false, true }) + { + bool stream = streamed; + runner.Test($"h2 early answer: a {(stream ? "streamed" : "buffered")} 413 past the client's window to a duplex upload arrives whole, and the upload stops", () => + { + // This client credits the answer as it uploads; once the answer has ended, the reset after it stops the upload. + int port = StartH2(connection => stream + ? connection.RunAsync((_, _, writer) => AnswerAsync(writer, answer)) + : connection.RunAsync((_, _) => new ValueTask(new Http2Response { Status = 413, Body = answer }))); + using HttpClient client = H2Client(); + AssertAnsweredAndStopped(client, $"http://127.0.0.1:{port}/", answer, H2DrainedStopped, duplex: true); + }); + } + } + private static void RegisterHttpClient(Runner runner) { // Inside the client's 64 KiB stream window: this client credits an answer only once its upload ends, and the reset follows the END_STREAM. @@ -191,20 +254,56 @@ private static void RegisterRawClient(Runner runner) }); } - private static void AssertAnsweredAndStopped(HttpClient client, string url, byte[] answer, long stopped) + private static void AssertAnsweredAndStopped(HttpClient client, string url, byte[] answer, long stopped, bool duplex = false) { - var upload = new CountedUpload(Upload); + HttpContent upload = duplex ? new DuplexUpload(Upload) : new CountedUpload(Upload); Task<(HttpStatusCode Status, byte[] Body)> answered = PostAsync(client, url, upload); Assert.True(answered.Wait(100_000), "the answer never arrived"); Assert.Equal(HttpStatusCode.RequestEntityTooLarge, answered.Result.Status); AssertBody(answer, answered.Result.Body); - long sent = upload.Ended.Result; + long sent = ((ICountedUpload)upload).Ended.Result; Assert.True(sent < stopped, $"the client uploaded {sent} of {Upload} bytes to a handler that read none of them: nothing asked it to stop"); } + // A client that reads the answer only once its upload has ended gets the answer whole, its upload taken to the end. + private static void AssertAnsweredAndEnded(Task<(HttpStatusCode Status, byte[] Body)> answered, CountedUpload upload, byte[] answer) + { + try + { + Assert.True(answered.Wait(100_000), $"the answer never arrived; the upload is at {upload.Sent} of {Upload} bytes"); + } + catch (AggregateException exception) + { + throw new Exception($"the answer never arrived ({exception.GetBaseException().Message}); the upload is at {upload.Sent} of {Upload} bytes"); + } + Assert.Equal(HttpStatusCode.RequestEntityTooLarge, answered.Result.Status); + AssertBody(answer, answered.Result.Body); + Assert.Equal(Upload, upload.Ended.Result); + } + + // Until what the client takes from its source stops growing: an unread upload filled the window it was given. + private static void WaitStalled(CountedUpload upload) + { + long last = -1, since = Environment.TickCount64, deadline = since + 30_000; + while (Environment.TickCount64 < deadline) + { + long now = upload.Sent; + if (now != last) + { + (last, since) = (now, Environment.TickCount64); + } + else if (now > 0 && Environment.TickCount64 - since > 500) + { + return; + } + Thread.Sleep(20); + } + throw new Exception($"the upload never stalled: it went on to {upload.Sent} bytes"); + } + private static void AssertReadWhole(HttpClient client, string url) { var upload = new CountedUpload(ReadUpload); @@ -217,17 +316,18 @@ private static void AssertReadWhole(HttpClient client, string url) } // The answer, once the upload has ended as well: at its last byte, or where the client gave up on the rest. - private static async Task<(HttpStatusCode Status, byte[] Body)> PostAsync(HttpClient client, string url, CountedUpload upload) + private static async Task<(HttpStatusCode Status, byte[] Body)> PostAsync(HttpClient client, string url, HttpContent upload) { + var counted = (ICountedUpload)upload; try { using HttpResponseMessage response = await client.PostAsync(url, upload); byte[] body = await response.Content.ReadAsByteArrayAsync(); // Held open until then: disposing it would have the client cancel the upload itself. - if (await Task.WhenAny(upload.Ended, Task.Delay(30_000)) != upload.Ended) + if (await Task.WhenAny(counted.Ended, Task.Delay(30_000)) != counted.Ended) { - throw new Exception($"the upload neither finished nor stopped, at {upload.Sent} bytes"); + throw new Exception($"the upload neither finished nor stopped, at {counted.Sent} bytes"); } return (response.StatusCode, body); } @@ -372,8 +472,15 @@ private static byte[] Pattern(int length) return bytes; } - // A StreamContent, so not duplex: over HTTP/3 the client finishes its upload before it hands over the answer. - private sealed class CountedUpload : StreamContent + private interface ICountedUpload + { + long Sent { get; } + + Task Ended { get; } + } + + // A StreamContent, so not duplex: the client finishes its upload before it hands over the answer, and over HTTP/2 credits the answer only then. + private sealed class CountedUpload : StreamContent, ICountedUpload { private readonly Source _source; private readonly TaskCompletionSource _ended = new(TaskCreationOptions.RunContinuationsAsynchronously); @@ -406,6 +513,41 @@ protected override async Task SerializeToStreamAsync(Stream stream, TransportCon } } + // An HttpContent of its own, which SocketsHttpHandler sends duplex: the answer is handed over, and credited, while the upload goes on. + private sealed class DuplexUpload(long length) : HttpContent, ICountedUpload + { + private long _sent; + private readonly TaskCompletionSource _ended = new(TaskCreationOptions.RunContinuationsAsynchronously); + + public long Sent => Interlocked.Read(ref _sent); + + public Task Ended => _ended.Task; + + protected override async Task SerializeToStreamAsync(Stream stream, TransportContext? context) + { + byte[] chunk = new byte[64 * 1024]; + try + { + while (Sent < length) + { + int count = (int)Math.Min(chunk.Length, length - Sent); + await stream.WriteAsync(chunk.AsMemory(0, count)); + Interlocked.Add(ref _sent, count); + } + } + finally + { + _ended.TrySetResult(Sent); + } + } + + protected override bool TryComputeLength(out long size) + { + size = length; + return true; + } + } + // Zeros, as many as the upload is long, counted as they are taken. private sealed class Source(long size) : Stream { diff --git a/tests/Ioxide.Tests.Unit/Http2EarlyAnswerParkedTests.cs b/tests/Ioxide.Tests.Unit/Http2EarlyAnswerParkedTests.cs new file mode 100644 index 00000000..08664e97 --- /dev/null +++ b/tests/Ioxide.Tests.Unit/Http2EarlyAnswerParkedTests.cs @@ -0,0 +1,228 @@ +using System.Buffers; +using System.Runtime.CompilerServices; +using ioxide.http2; +using static Ioxide.Tests.Http2EarlyAnswerTests; +using static Ioxide.Tests.Http2FlowControlTests; + +namespace Ioxide.Tests; + +/// +/// An early answer past the client's stream window, to an upload its handler did not read: a client that credits the +/// answer only once its upload has ended needs the upload credited first. The handler is not held on that credit, what +/// it left unread is credited and dropped once it returns, and RST_STREAM(NO_ERROR) follows the answer's END_STREAM if +/// the upload is still arriving then (RFC 9113 8.1). +/// +internal static class Http2EarlyAnswerParkedTests +{ + // The client's stream window, as it is until its SETTINGS say otherwise; the answer is twice that, so it parks. + private const int ClientWindow = 65535; + private const int AnswerLength = 2 * ClientWindow; + + [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_writerPool")] + private static extern ref Stack WriterPool(Http2Connection connection); + + public static void Register(Runner runner) + { + RegisterDrain(runner); + RegisterReset(runner); + } + + private static void RegisterDrain(Runner runner) + { + runner.Test("h2 early answer: a buffered answer past the client's window, returned once the upload filled its stream window, credits what it left unread", () => + { + var stalled = new TaskCompletionSource(); + + using var client = new StrictClient(); + Task run = client.Connection.RunAsync(async (_, _) => + { + await stalled.Task; + return new Http2Response { Status = 413, Body = new byte[AnswerLength] }; + }); + + client.ReleaseFlush(); + client.Feed([.. Open(), .. Post(1), .. Fill(1, StreamWindow)]); + Assert.Equal(0, Credit(client.Drain(), 1)); // unread, so not credited yet + + stalled.SetResult(); // answered outside the pass, the upload stalled + List frames = client.Drain(); + Assert.Equal(ClientWindow, Frame.Body(frames, 1).Length); // one client window of the answer, then parked + int credited = Credit(frames, 1); + Assert.True(credited == StreamWindow, + $"the handler returned with {StreamWindow} bytes unread and {credited} came back on its stream: a client that credits the answer only once its upload ends has no window left to end it"); + + AssertUploadEndsThenAnswerEnds(client, frames); + client.Close(run); + }); + + runner.Test("h2 early answer: CompleteAsync past the client's window returns without its credit, and the unread upload is credited once the handler returns", () => + { + bool returned = false; + + using var client = new StrictClient(); + Task run = client.Connection.RunAsync(async (_, _, writer) => + { + writer.WriteHeaders(new Http2Response { Status = 413 }); + writer.Write(new byte[AnswerLength]); + await writer.CompleteAsync(); + returned = writer.IsCompleted; + }); + + client.ReleaseFlush(); + client.Feed([.. Open(), .. Post(1), .. Fill(1, StreamWindow)]); + List frames = client.Drain(); + Assert.True(returned, "CompleteAsync held the handler on credit that a client reading its answer only once its upload ends gives only then"); + Assert.Equal(ClientWindow, Frame.Body(frames, 1).Length); + int credited = Credit(frames, 1); + Assert.True(credited == StreamWindow, + $"the handler returned with {StreamWindow} bytes unread and {credited} came back on its stream"); + + AssertUploadEndsThenAnswerEnds(client, frames); + client.Close(run); + }); + + runner.Test("h2 early answer: a streamed answer past the client's window that its handler leaves the stack to complete credits the unread upload", () => + { + using var client = new StrictClient(); + Task run = client.Connection.RunAsync((_, _, writer) => + { + writer.WriteHeaders(new Http2Response { Status = 413 }); + writer.Write(new byte[AnswerLength]); + return default; + }); + + client.ReleaseFlush(); + client.Feed([.. Open(), .. Post(1), .. Fill(1, StreamWindow)]); + List frames = client.Drain(); + Assert.Equal(ClientWindow, Frame.Body(frames, 1).Length); + int credited = Credit(frames, 1); + Assert.True(credited == StreamWindow, + $"the handler returned with {StreamWindow} bytes unread and {credited} came back on its stream"); + + AssertUploadEndsThenAnswerEnds(client, frames); + client.Close(run); + }); + + runner.Test("h2 early answer: a handler that reads its body after CompleteAsync parked its answer gets all of it", () => + { + // Only a handler that has returned is done with its body: nothing is drained from under one still reading. + var read = new Tally(); + + using var client = new StrictClient(); + Task run = client.Connection.RunAsync(async (_, body, writer) => + { + writer.WriteHeaders(new Http2Response { Status = 413 }); + writer.Write(new byte[AnswerLength]); + await writer.CompleteAsync(); + await ReadAll(body, read); + }); + + client.ReleaseFlush(); + client.Feed([.. Open(), .. Post(1), .. Fill(1, StreamWindow)]); + List frames = client.Drain(); + Assert.True(read.Bytes == StreamWindow, $"the handler read {read.Bytes} of the {StreamWindow} bytes sent with its request, its answer parked"); + Assert.Equal(StreamWindow, Credit(frames, 1)); // credited as read, once + + client.Feed(Data(1, 0, endStream: true)); // the handler reads to the end and returns + client.Feed([.. WindowUpdate(0, 1 << 20), .. WindowUpdate(1, 1 << 20)]); + frames.AddRange(client.Drain()); + Assert.Equal(AnswerLength, Frame.Body(frames, 1).Length); + Assert.True(!frames.Any(f => f.Type == RstStreamFrame), "a stream whose request had ended was reset"); + + client.Close(run); + }); + + runner.Test("h2 early answer: a FlushAsync after CompleteAsync parked the answer reports it ended, and the answer goes out once", () => + { + // The parked end sends from the writer's staging: a flush that took it up again would wait on the same credit, and send it twice. + string? flushed = null; + + using var client = new StrictClient(); + Task run = client.Connection.RunAsync(async (_, _, writer) => + { + writer.WriteHeaders(new Http2Response { Status = 413 }); + writer.Write(new byte[AnswerLength]); + await writer.CompleteAsync(); + flushed = (await writer.FlushAsync()).IsCompleted ? "ended" : "not ended"; + }); + + client.ReleaseFlush(); + client.Feed([.. Open(), .. Post(1), .. Data(1, 1000, endStream: false)]); + List frames = client.Drain(); + Assert.True(flushed == "ended", $"a flush after the response had completed returned {flushed ?? "nothing, still waiting on credit"}"); + + client.Feed([.. WindowUpdate(0, 1 << 20), .. WindowUpdate(1, 1 << 20)]); + frames.AddRange(client.Drain()); + Assert.Equal(AnswerLength, Frame.Body(frames, 1).Length); + Assert.Equal(1, frames.Count(f => f.StreamId == 1 && f.EndStream)); + + client.Close(run); + }); + + runner.Test("h2 early answer: a streamed answer parked on credit lets go of its writer when the client closes the connection", () => + { + using var client = new StrictClient(); + Task run = client.Connection.RunAsync((_, _, writer) => + { + writer.WriteHeaders(new Http2Response { Status = 413 }); + writer.Write(new byte[AnswerLength]); + return writer.CompleteAsync(); + }); + + client.ReleaseFlush(); + client.Feed([.. Open(), .. Post(1), .. Data(1, 1000, endStream: false)]); + Assert.Equal(ClientWindow, Frame.Body(client.Drain(), 1).Length); + Assert.True(WriterPool(client.Connection).Count == 0, "precondition: the answer was meant to be parked, its writer out"); + + client.Close(run); + Assert.True(WriterPool(client.Connection).Count == 1, "the connection ended with the parked answer still holding its writer"); + }); + } + + private static void RegisterReset(Runner runner) + { + foreach (bool streamed in new[] { false, true }) + { + bool stream = streamed; + runner.Test($"h2 early answer: a {(stream ? "streamed" : "buffered")} answer parked on credit, its END_STREAM out with the upload still arriving, is followed by RST_STREAM(NO_ERROR)", () => + { + // A client that reads while it uploads credits the answer; once it has ended, the upload is asked to stop. + string answer = new('x', AnswerLength); + + using var client = new StrictClient(); + Task run = stream + ? client.Connection.RunAsync((_, _, writer) => Answer(writer, answer)) + : client.Connection.RunAsync((_, _) => new ValueTask(Http2Response.Text(answer, 413))); + + client.ReleaseFlush(); + client.Feed([.. Open(), .. Post(1), .. Data(1, 1000, endStream: false)]); + List frames = client.Drain(); + Assert.Equal(ClientWindow, Frame.Body(frames, 1).Length); + + client.Feed([.. Data(1, 16384, endStream: false), .. WindowUpdate(0, 1 << 20), .. WindowUpdate(1, 1 << 20)]); + frames.AddRange(client.Drain()); + AssertAnsweredThenStopped(frames, streamId: 1, 413, answer); + Assert.Equal(1000 + 16384, Credit(frames, 1)); // drained while the answer waited + + client.Close(run); + }); + } + } + + // The upload goes on with the credit the drain gave it, then ends; credited, the answer then ends whole, and no reset follows. + private static void AssertUploadEndsThenAnswerEnds(StrictClient client, List frames) + { + client.Feed(Data(1, 16384, endStream: false)); + int credited = Credit(client.Drain(), 1); + Assert.True(credited == 16384, $"16384 more bytes of the unread upload arrived and {credited} were credited"); + + client.Feed([.. Data(1, 16384, endStream: true), .. WindowUpdate(0, 1 << 20), .. WindowUpdate(1, 1 << 20)]); + List after = client.Drain(); + frames.AddRange(after); + Assert.Equal(0, Credit(after, 1)); // an ended request is owed none + + Assert.Equal(AnswerLength, Frame.Body(frames, 1).Length); + Assert.True(frames.Last(f => f.StreamId == 1).EndStream, "the answer never ended"); + Assert.True(!frames.Any(f => f.Type == RstStreamFrame), "a stream whose request had ended was reset"); + } +} diff --git a/tests/Ioxide.Tests.Unit/Http2FlowControlTests.cs b/tests/Ioxide.Tests.Unit/Http2FlowControlTests.cs index 7737476c..d19826a4 100644 --- a/tests/Ioxide.Tests.Unit/Http2FlowControlTests.cs +++ b/tests/Ioxide.Tests.Unit/Http2FlowControlTests.cs @@ -272,7 +272,9 @@ await ReadAll(body, read, between: () => using var client = new StrictClient(); Task run = client.Connection.RunAsync(async (_, writer) => { - await AnswerWith(writer, new byte[64 * 1024]); + writer.WriteHeaders(new Http2Response { Status = 200 }); + writer.Write(new byte[64 * 1024]); + await writer.FlushAsync(); // waits on credit, where CompleteAsync leaves it to the connection finished = true; }); diff --git a/tests/Ioxide.Tests.Unit/Program.cs b/tests/Ioxide.Tests.Unit/Program.cs index bba8de8b..db3ffe68 100644 --- a/tests/Ioxide.Tests.Unit/Program.cs +++ b/tests/Ioxide.Tests.Unit/Program.cs @@ -43,6 +43,7 @@ private static int Main() Http2BufferedCreditTests.Register(runner); Http2EarlyAnswerTests.Register(runner); Http2EarlyAnswerGuardTests.Register(runner); + Http2EarlyAnswerParkedTests.Register(runner); Nghttp2ResetResponseTests.Register(runner); Http2PeerClosedFlushTests.Register(runner); Http2StreamResetTests.Register(runner);