From 40c9b8f18172f4293f818d53eb36fd071c2acdc1 Mon Sep 17 00:00:00 2001 From: Diogo Martins Date: Sat, 14 Nov 2026 21:43:38 +0000 Subject: [PATCH 1/3] http2: a stream's window is credited a quarter of it at a time instead of once per DATA frame, so an upload costs one WINDOW_UPDATE per 64 KiB (#630) A stream's receive window was credited frame by frame: a buffered body and a streamed body's padding as each DATA frame arrived, a streamed body as its handler read each chunk, and the upload behind a buffered answer parked on credit as each frame was dropped. Outside a read pass each read also made a flush of its own. A stream is now credited once what it has consumed and not yet been credited reaches a step, in one WINDOW_UPDATE for all of it. The step is a quarter of InitialWindowSize, at most 65,535 bytes and at least one: until the client has applied the server's SETTINGS it counts the default 65,535-byte window (RFC 9113 6.9.2), and a quarter of the default 256 KiB is one byte more than that. Consumed is what the stream was sent and not credited, from its ReceiveWindow, less what its body reader still holds unread, which a new count on the reader keeps; unread bytes are never credited, so the window still bounds what a handler holds. A request that has ended is owed nothing, as before, and a stream that is reset or ended is credited nothing afterwards. A read outside a pass flushes only when a step is due. The upload behind a parked buffered answer has no request left to count against, so the connection keeps a count per such stream, freed when the answer ends or the stream does. It counts what the handler left unread too: with steps, an upload whose window the handler had nearly filled would otherwise wait on credit that never comes. --- .../Http2/ManagedStreamedRequest/Program.cs | 8 +- docs/index.html | 8 +- src/protocols/ioxide.http2/Http2BodyReader.cs | 21 +- .../ioxide.http2/Http2Connection.Frames.cs | 16 +- .../ioxide.http2/Http2Connection.Write.cs | 1 + src/protocols/ioxide.http2/Http2Connection.cs | 62 ++- src/protocols/ioxide.http2/Http2Options.cs | 5 +- .../Ioxide.Tests.Unit/Http2CreditStepTests.cs | 424 ++++++++++++++++++ .../Http2EarlyAnswerTests.cs | 6 +- .../Http2FlowControlTests.cs | 15 +- .../Http2ReceiveWindowTests.cs | 9 +- .../Http2StreamedRequestTests.cs | 6 +- tests/Ioxide.Tests.Unit/Program.cs | 1 + 13 files changed, 539 insertions(+), 43 deletions(-) create mode 100644 tests/Ioxide.Tests.Unit/Http2CreditStepTests.cs diff --git a/Playground/Http2/ManagedStreamedRequest/Program.cs b/Playground/Http2/ManagedStreamedRequest/Program.cs index c3fbb8d4..fbe75149 100644 --- a/Playground/Http2/ManagedStreamedRequest/Program.cs +++ b/Playground/Http2/ManagedStreamedRequest/Program.cs @@ -82,10 +82,10 @@ long total = 0; while (true) { - // Empty means end of body. Each read hands back the peer's credit for the - // chunk it returns, which is what lets the next one arrive - and the - // memory it points at is recycled by the NEXT read, so anything worth - // keeping has to be copied out here. + // Empty means end of body. What the reads return is handed back to the peer + // as credit, a quarter of the window at a time, which is what lets more + // arrive - and the memory a chunk points at is recycled by the NEXT read, + // so anything worth keeping has to be copied out here. ReadOnlyMemory chunk = await body.ReadAsync(); if (chunk.IsEmpty) { diff --git a/docs/index.html b/docs/index.html index d9db9393..7a4c4133 100644 --- a/docs/index.html +++ b/docs/index.html @@ -5496,10 +5496,10 @@

HTTP/2 · request streamed

long total = 0; while (true) { - // Empty means end of body. Each read hands back the peer's credit for the - // chunk it returns, which is what lets the next one arrive - and the - // memory it points at is recycled by the NEXT read, so anything worth - // keeping has to be copied out here. + // Empty means end of body. What the reads return is handed back to the peer + // as credit, a quarter of the window at a time, which is what lets more + // arrive - and the memory a chunk points at is recycled by the NEXT read, + // so anything worth keeping has to be copied out here. ReadOnlyMemory<byte> chunk = await body.ReadAsync(); if (chunk.IsEmpty) { diff --git a/src/protocols/ioxide.http2/Http2BodyReader.cs b/src/protocols/ioxide.http2/Http2BodyReader.cs index bc84bd78..f26d22d0 100644 --- a/src/protocols/ioxide.http2/Http2BodyReader.cs +++ b/src/protocols/ioxide.http2/Http2BodyReader.cs @@ -17,9 +17,10 @@ namespace ioxide.http2; /// Whether there is one to read is , known before the first read. /// /// Backpressure is real rather than buffered-and-hoped-for: a chunk is credited back to the peer's -/// window only as it is handed over, so a slow consumer stops replenishing, the peer runs out of -/// window and stops sending. Memory is bound by one window instead of by the whole body, which is -/// the difference that makes a hostile upload harmless. +/// window only once it has been handed over, in the steps +/// describes, so a slow consumer stops replenishing, the peer runs out of window and stops sending. +/// Memory is bound by one window instead of by the whole body, which is the difference that makes a +/// hostile upload harmless. /// /// 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'. @@ -37,6 +38,10 @@ public sealed class Http2BodyReader : IValueTaskSource> private readonly int _streamId; private readonly Queue<(byte[] Buffer, int Length)> _chunks = new(); + + // What _chunks holds, counted: a credit check that summed the queue at every read would go quadratic in tiny frames. + internal int Unread { get; private set; } + private (byte[]? Buffer, int Length) _handedOut; private bool _ended; private Exception? _fault; // what the end is reported as, when the body was cut off @@ -72,7 +77,8 @@ public ValueTask> ReadAsync() if (_chunks.TryDequeue(out (byte[] Buffer, int Length) chunk)) { _handedOut = chunk; - _owner.CreditBody(_streamId, chunk.Length); // consumption is what opens the window + Unread -= chunk.Length; + _owner.CreditBody(_streamId); // consumption is what opens the window return new ValueTask>(chunk.Buffer.AsMemory(0, chunk.Length)); } @@ -99,6 +105,7 @@ internal void Push(ReadOnlySpan data) byte[] buffer = ArrayPool.Shared.Rent(data.Length); data.CopyTo(buffer); _chunks.Enqueue((buffer, data.Length)); + Unread += data.Length; if (_armed) { @@ -142,7 +149,8 @@ internal void FireIfReady() { _armed = false; _handedOut = chunk; - _owner.CreditBody(_streamId, chunk.Length); + Unread -= chunk.Length; + _owner.CreditBody(_streamId); _core.SetResult(chunk.Buffer.AsMemory(0, chunk.Length)); } else if (_ended) @@ -187,13 +195,14 @@ internal void Drop() FireIfReady(); } - // Owed no credit: the connection was charged for these on arrival, and their stream is done. + // Owed no credit here: the connection was charged for these on arrival, and a stream left open behind a parked answer counts them at its retire. private void DropQueued() { while (_chunks.TryDequeue(out (byte[] Buffer, int Length) chunk)) { ArrayPool.Shared.Return(chunk.Buffer); } + Unread = 0; } private void ReleaseHandedOut() diff --git a/src/protocols/ioxide.http2/Http2Connection.Frames.cs b/src/protocols/ioxide.http2/Http2Connection.Frames.cs index a49d89e9..8304e1f5 100644 --- a/src/protocols/ioxide.http2/Http2Connection.Frames.cs +++ b/src/protocols/ioxide.http2/Http2Connection.Frames.cs @@ -404,12 +404,8 @@ 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); - } + // A buffered answer parked on credit still has its upload credited; any other stream's window went with it. + CreditParkedUpload(header.StreamId, payload.Length, ended); return; } @@ -447,11 +443,11 @@ 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) + // Consumed now: a buffered body, or a streamed one's padding, its body as read; an ended request is owed none. + int consumed = pending.BodyReader is null ? payload.Length : payload.Length - body.Length; + if (consumed > 0 && !ended) { - CreditStream(pending, credit); + CreditConsumed(pending); } if (ended) diff --git a/src/protocols/ioxide.http2/Http2Connection.Write.cs b/src/protocols/ioxide.http2/Http2Connection.Write.cs index 6413c622..7ebfcd9c 100644 --- a/src/protocols/ioxide.http2/Http2Connection.Write.cs +++ b/src/protocols/ioxide.http2/Http2Connection.Write.cs @@ -484,6 +484,7 @@ private async Task CompleteParkedAsync(Http2ResponseWriter writer) { _creditWaiters.Remove(writer.StreamId); _responseWindows.Remove(writer.StreamId); + _parkedUploads.Remove(writer.StreamId); _writerPool.Push(writer); } } diff --git a/src/protocols/ioxide.http2/Http2Connection.cs b/src/protocols/ioxide.http2/Http2Connection.cs index 1a2a338d..beef1af9 100644 --- a/src/protocols/ioxide.http2/Http2Connection.cs +++ b/src/protocols/ioxide.http2/Http2Connection.cs @@ -87,6 +87,9 @@ public sealed partial class Http2Connection : IDisposable // A new stream's receive window: InitialWindowSize, but no less than the 65535 a peer uses until it has our SETTINGS. private int _initialStreamWindow; + // What a stream consumes before it is credited: a quarter of its window, but at most the 65,535 a peer counts until it applies our SETTINGS, so any window it counts holds a step. + private readonly int _creditStep; + // The reactor this connection is served on; null when it runs without one, as over a test pipe. private Reactor? _reactor; @@ -102,6 +105,7 @@ public Http2Connection(IDuplexPipe pipe, Http2Options? options = null) _options = options ?? new Http2Options(); _decoder = new HpackDecoder(); _initialStreamWindow = Math.Max(_options.InitialWindowSize, DefaultWindow); + _creditStep = Math.Clamp(_options.InitialWindowSize / 4, 1, DefaultWindow); } /// Convenience for cleartext h2c: wraps the connection in its own duplex pipe. @@ -425,6 +429,12 @@ private void RetireStream(PendingRequest pending) { ResetStream(pending.StreamId, Http2Error.NoError); } + else if (live && !pending.RequestEnded) + { + // Its upload goes on behind the parked rest, and what its reader held is dropped below: all it was sent is consumed. + _parkedUploads[pending.StreamId] = 0; + CreditParkedUpload(pending.StreamId, _initialStreamWindow - pending.ReceiveWindow, ended: false); + } Recycle(pending); @@ -471,16 +481,58 @@ private PendingRequest RentPending(int streamId) return pending; } - /// Return a read chunk's credit to its stream alone: the connection was charged for it on arrival. - internal void CreditBody(int streamId, int length) + /// A chunk was read: its stream alone is credited once a step is consumed, as the connection was charged for it on arrival. + internal void CreditBody(int streamId) { - if (length <= 0 || IsBroken || !_streams.TryGetValue(streamId, out PendingRequest? pending) || pending.RequestEnded) + if (IsBroken || !_streams.TryGetValue(streamId, out PendingRequest? pending) || pending.RequestEnded) { return; } - CreditStream(pending, length); - FlushCredit(); + if (CreditConsumed(pending)) + { + FlushCredit(); + } + } + + // Received and not yet credited, less what the reader still holds: the window is what bounds the unread bytes, so they are never credited. + private bool CreditConsumed(PendingRequest pending) + { + int consumed = _initialStreamWindow - pending.ReceiveWindow - (pending.BodyReader?.Unread ?? 0); + if (consumed < _creditStep) + { + return false; + } + + CreditStream(pending, consumed); + return true; + } + + // The upload still arriving behind each buffered answer parked on credit, by stream: what it has had dropped and not been credited. + private readonly Dictionary _parkedUploads = new(); + + // Credited in steps as it is dropped, so a client that credits the answer only once its upload ends can finish both. + private void CreditParkedUpload(int streamId, int dropped, bool ended) + { + if (!_parkedUploads.TryGetValue(streamId, out int owed)) + { + return; + } + + // An ended request is owed none, and a stream whose answer has gone takes none (RFC 9113 5.1). + if (ended || !_responseWindows.ContainsKey(streamId)) + { + _parkedUploads.Remove(streamId); + return; + } + + owed += dropped; + if (owed >= _creditStep) + { + WriteWindowUpdate(streamId, owed); + owed = 0; + } + _parkedUploads[streamId] = owed; } // Every WINDOW_UPDATE the server sends goes through these two, which add back exactly what they grant. diff --git a/src/protocols/ioxide.http2/Http2Options.cs b/src/protocols/ioxide.http2/Http2Options.cs index f9f1720f..6c985d27 100644 --- a/src/protocols/ioxide.http2/Http2Options.cs +++ b/src/protocols/ioxide.http2/Http2Options.cs @@ -18,7 +18,10 @@ public sealed record Http2Options /// peer may send ahead of the credit the server gives back. A DATA frame past it resets its /// stream with FLOW_CONTROL_ERROR (RFC 9113 6.9). A buffered body is credited as it arrives, a /// streamed one as its handler reads it, so a handler that stops reading holds one window and - /// stalls only its own upload. + /// stalls only its own upload. The credit goes back in steps of a quarter of this, and of at most + /// 65,535 bytes, the window a peer counts until it has applied the SETTINGS (RFC 9113 6.9.2): one + /// WINDOW_UPDATE per step rather than per DATA frame, with the peer never out of window while the + /// handler keeps reading. /// /// The connection's own window is opened to 2^31-1 right behind the server's SETTINGS and kept /// there, so what bounds a connection's request bodies in flight is diff --git a/tests/Ioxide.Tests.Unit/Http2CreditStepTests.cs b/tests/Ioxide.Tests.Unit/Http2CreditStepTests.cs new file mode 100644 index 00000000..cfe718b0 --- /dev/null +++ b/tests/Ioxide.Tests.Unit/Http2CreditStepTests.cs @@ -0,0 +1,424 @@ +using System.Buffers.Binary; +using System.Globalization; +using System.Runtime.CompilerServices; +using System.Text; +using ioxide.http2; +using static Ioxide.Tests.Http2FlowControlTests; + +namespace Ioxide.Tests; + +/// +/// A stream's receive window is credited a step at a time, a quarter of the window and at most 65,535 bytes, +/// rather than once per DATA frame (RFC 9113 6.9): fewer WINDOW_UPDATEs, a client that is never left out of +/// window while its handler reads, whatever window it counts, and no credit for a stream that has ended or +/// been reset. +/// +internal static class Http2CreditStepTests +{ + // The default window's step: a quarter of 256 KiB is 65,536, one byte past the window a client counts before it applies the SETTINGS. + private const int Step = 65535; + + private const uint EnhanceYourCalm = 0xb; + + [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_parkedUploads")] + private static extern ref Dictionary ParkedUploads(Http2Connection connection); + + public static void Register(Runner runner) + { + RegisterSteps(runner); + RegisterNoStall(runner); + RegisterEnds(runner); + RegisterParked(runner); + } + + private static void RegisterSteps(Runner runner) + { + foreach (int frame in new[] { 16384, 1000 }) + { + runner.Test($"h2 credit: an upload in {frame}-byte frames gets one WINDOW_UPDATE per step, each a whole step, and completes", () => + { + const long Size = 4L << 20; + var read = new Tally(); + + using var client = new StrictClient(); + Task run = client.Connection.RunAsync(async (_, body, writer) => + { + await ReadAll(body, read); + await Answer(writer, 200); + }); + + Uploader up = Start(client, applied: true, frame); + Assert.True(up.SendTo(Size, Size) && read.Bytes == Size, + $"the upload stopped at {up.Sent} of {Size} bytes, {read.Bytes} read"); + + List credit = Increments(up.Frames, 1); + Assert.True(credit.Count > 0 && credit.Count * 4 <= up.DataFrames && credit.All(c => c >= Step), + $"{up.DataFrames} DATA frames were credited by {credit.Count} WINDOW_UPDATEs of [{Summary(credit)}], not by one per {Step}-byte step"); + + client.Close(run); + }); + } + + runner.Test("h2 credit: what a handler has not read is never credited, so its window still bounds what it holds", () => + { + var first = new TaskCompletionSource(); + var rest = new TaskCompletionSource(); + var read = new Tally(); + + using var client = new StrictClient(); + Task run = client.Connection.RunAsync(async (_, body, writer) => + { + await first.Task; + read.Bytes += (await body.ReadAsync()).Length; // one chunk; the rest stays queued + await rest.Task; + await ReadAll(body, read); + await Answer(writer, 200); + }); + + client.ReleaseFlush(); // server SETTINGS + client.Feed([.. Open(), .. Post(1), .. Fill(1, StreamWindow)]); + client.Drain(); + + first.SetResult(); + int credited = Credit(client.Drain(), 1); + Assert.True(read.Bytes == 16384 && credited == 0, + $"{read.Bytes} of the {StreamWindow} bytes in its window were read and {credited} were credited"); + + rest.SetResult(); + credited += Credit(client.Drain(), 1); + Assert.True(read.Bytes == StreamWindow && credited == StreamWindow, + $"read to the end of its window, {read.Bytes} bytes, the stream was credited {credited}"); + + client.Feed(Data(1, 0, endStream: true)); + client.Drain(); + client.Close(run); + }); + + runner.Test("h2 credit: reads short of a step flush nothing, outside the pass too, and the read that completes it is credited in one WINDOW_UPDATE", () => + { + var gate = new TaskCompletionSource(); + var read = new Tally(); + + using var client = new StrictClient(); + Task run = client.Connection.RunAsync(async (_, body, writer) => + { + await gate.Task; + writer.WriteHeaders(new Http2Response { Status = 200 }); // staged for the writer's own flush, which a read has no reason to make + await ReadAll(body, read); + await writer.CompleteAsync(); + }); + + client.ReleaseFlush(); // server SETTINGS + client.Feed([.. Open(), .. Post(1), .. Fill(1, 3 * 16384)]); + client.Drain(); + + gate.SetResult(); // three reads outside the pass, 16,383 bytes short of a step + Assert.True(read.Bytes == 3 * 16384 && client.PendingFlushes == 0, + $"{read.Bytes} bytes were read short of a step and {client.PendingFlushes} flushes went out behind them"); + + client.Feed(Data(1, 16384, endStream: false)); + var flushes = new List>(); + while (client.PendingFlushes > 0) + { + flushes.Add(Frame.Walk(client.ReleaseFlush())); + } + List credit = Increments(flushes.SelectMany(f => f), 1); + Assert.True(flushes.Count == 1 && credit.Count == 1 && credit[0] == 4 * 16384, + $"the read that made the step went out in {flushes.Count} flushes with [{Summary(credit)}] on its stream"); + + client.Feed(Data(1, 0, endStream: true)); + client.Drain(); + client.Close(run); + }); + + runner.Test("h2 credit: a buffered body is credited in steps as it arrives, and MaxRequestBytes still bounds it", () => + { + const int Cap = 1 << 20; + + using var client = new StrictClient(new Http2Options { MaxRequestBytes = Cap }); + Task run = client.Connection.RunAsync(request => Http2Response.Text(request.Body.Length.ToString(CultureInfo.InvariantCulture))); + + Uploader up = Start(client, applied: true, frame: 16384); + Assert.True(up.SendTo(Cap, Cap), $"a body of exactly MaxRequestBytes stopped at {up.Sent} of {Cap} bytes"); + Assert.Equal(Cap.ToString(CultureInfo.InvariantCulture), Encoding.ASCII.GetString(Frame.Body(up.Frames, 1))); + + List credit = Increments(up.Frames, 1); + Assert.True(credit.Count > 0 && credit.Count * 4 <= up.DataFrames && credit.All(c => c >= Step), + $"{up.DataFrames} DATA frames of a buffered body were credited by [{Summary(credit)}], not in whole steps"); + + client.Feed(Post(3)); + var over = new Uploader(client, up.Peer, streamId: 3, frame: 16384); + over.SendTo(Cap + 16384, Cap + (2 * 16384)); // the frame past the cap, its body not yet ended + int reset = over.Frames.FindIndex(f => f is { Type: RstStreamFrame, StreamId: 3 }); + Assert.True(reset >= 0 && BinaryPrimitives.ReadUInt32BigEndian(over.Frames[reset].Payload) == EnhanceYourCalm, + $"a body past MaxRequestBytes was not reset with ENHANCE_YOUR_CALM, {over.Sent} bytes in"); + + client.Feed(Data(3, 16384, endStream: false)); // in flight when the reset went out + over.Take(); + Assert.True(Increments(over.Frames.Skip(reset), 3).Count == 0, "stream 3 was credited after the server reset it"); + + client.Close(run); + }); + } + + private static void RegisterNoStall(Runner runner) + { + runner.Test("h2 credit: a reader that keeps up never leaves the client out of window, whatever InitialWindowSize, before and after the client applies and acknowledges the SETTINGS", () => + { + var stalls = new List(); + foreach (int window in new[] { StreamWindow, 1 << 20, 65536, 65535, 16384, 1000, 100, 3 }) + { + foreach (SettingsState when in Enum.GetValues()) + { + if (UploadWhileReading(window, when) is { } stall) + { + stalls.Add(stall); + } + } + } + + Assert.True(stalls.Count == 0, string.Join("; ", stalls)); + }); + } + + // How far the client has got with the server's SETTINGS when it uploads: not applied, applied but the ACK not yet + // sent, acknowledged before the upload, or applied with one 65,535 window in flight and acknowledged after the next. + private enum SettingsState { Unseen, Applied, Acknowledged, MidUpload } + + private static string? UploadWhileReading(int window, SettingsState when) + { + long size = (3L * Math.Max(window, 65535)) + 1001; + var read = new Tally(); + + using var client = new StrictClient(new Http2Options { InitialWindowSize = window }); + Task run = client.Connection.RunAsync(async (_, body, writer) => + { + await ReadAll(body, read); + await Answer(writer, 200); + }); + + bool applied = when is SettingsState.Applied or SettingsState.Acknowledged; + Uploader up = Start(client, applied, frame: 16384, acknowledge: when == SettingsState.Acknowledged); + + bool done; + if (when == SettingsState.MidUpload) + { + done = up.SendTo(65535, size); + up.Peer.Absorb([up.ServerSettings]); // resizes the open stream by the difference (RFC 9113 6.9.2) + done = done && up.SendTo(2 * 65535, size); + client.Feed(SettingsAck()); // and from here the server holds it to the new window (6.5.3) + done = done && up.SendTo(size, size); + } + else + { + done = up.SendTo(size, size); + } + + string? stall = done && read.Bytes == size ? null + : $"InitialWindowSize {window}, SETTINGS {when}: the upload stopped at {up.Sent} of {size} bytes, {read.Bytes} of them read, the client out of window"; + client.Close(run); + return stall; + } + + private static void RegisterEnds(Runner runner) + { + runner.Test("h2 credit: a stream that ends or is reset a step short is credited none of it afterwards", () => + { + var read = new Dictionary { [1] = new(), [3] = new(), [5] = new() }; + + using var client = new StrictClient(); + Task run = client.Connection.RunAsync(async (request, body, writer) => + { + Tally tally = read[request.StreamId]; + if (request.StreamId == 5) + { + tally.Bytes = await ReadAtLeast(body, 3 * 16384); + await writer.ResetStreamAsync(Http2ErrorCode.Cancel); + return; + } + + try + { + await ReadAll(body, tally); + } + catch (IOException) + { + // stream 3, reset by the peer + } + await Answer(writer, 200); + }); + + // Three frames read on each: 16,383 bytes short of a step. + client.ReleaseFlush(); + client.Feed([.. Open(), .. Post(1), .. Fill(1, 3 * 16384), .. Post(3), .. Fill(3, 3 * 16384), .. Post(5), .. Fill(5, 3 * 16384)]); + List before = client.Drain(); + Assert.True(Increments(before, 1).Count + Increments(before, 3).Count + Increments(before, 5).Count == 0, + "a stream was credited short of a step"); + + // A frame each that would make the step: 1 ends with it, 3 is reset by the peer ahead of it, 5 was reset by its handler. + client.Feed([.. Data(1, 16384, endStream: true), .. RstStream(3, Cancel), .. Data(3, 16384, endStream: false), .. Data(5, 16384, endStream: false)]); + List after = client.Drain(); + Assert.True(read[1].Bytes == 4 * 16384 && read[3].Bytes == 3 * 16384 && read[5].Bytes == 3 * 16384, + $"the handlers read {read[1].Bytes}, {read[3].Bytes} and {read[5].Bytes} bytes, so stream 1 never read the step it was owed"); + Assert.True(Credit(after, 1) == 0 && Credit(after, 3) == 0 && Credit(after, 5) == 0, + $"credit went out after the end: {Credit(after, 1)} to 1, which ended, {Credit(after, 3)} to 3, which the peer reset, " + + $"{Credit(after, 5)} to 5, which its handler reset"); + + client.Close(run); + }); + } + + private static void RegisterParked(Runner runner) + { + // The answer is twice the client's 16 KiB window for it, so its rest parks on credit the client gives only once its upload is done. + foreach (int unread in new[] { StreamWindow - 16384, StreamWindow }) + { + runner.Test($"h2 credit: a buffered answer parked on credit, its handler gone with {unread} bytes unread, still lets a client that credits it only after its upload finish both", () => + { + const long Size = 1L << 20; + byte[] answer = new byte[32 * 1024]; + var gate = new TaskCompletionSource(); + + using var client = new StrictClient(); + Task run = client.Connection.RunAsync(async (_, _) => + { + await gate.Task; + return new Http2Response { Status = 413, Body = answer }; + }); + + Uploader up = Start(client, applied: true, frame: 16384, clientSettings: [(InitialWindowSize, 16384)]); + Assert.True(up.SendTo(unread, Size), $"the upload stopped at {up.Sent} bytes, before the {unread} its handler leaves unread"); + + gate.SetResult(); + Assert.True(up.SendTo(Size, Size), + $"behind the parked answer the upload stopped at {up.Sent} of {Size} bytes, its stream credited [{Summary(Increments(up.Frames, 1))}]"); + + client.Feed(WindowUpdate(1, 1 << 20)); + up.Take(); + Assert.True(Frame.Body(up.Frames, 1).Length == answer.Length && up.Frames.Last(f => f.StreamId == 1 && f.Type == Frame.Data).EndStream, + $"the answer came to {Frame.Body(up.Frames, 1).Length} of {answer.Length} bytes once the client credited it"); + + List credit = Increments(up.Frames, 1); + Assert.True(credit.All(c => c >= Step), $"the stream was credited [{Summary(credit)}], not in whole steps"); + + client.Close(run); + }); + } + + runner.Test("h2 credit: an upload behind a parked answer is credited nothing, and leaves nothing behind, once it ends, once its answer has ended, or once either side resets it", () => + { + foreach (string end in new[] { "it ended", "its answer ended", "the peer reset it", "the server reset it" }) + { + using var client = new StrictClient(); + Task run = client.Connection.RunAsync((_, _) => new ValueTask(new Http2Response { Status = 413, Body = new byte[32 * 1024] })); + + // Answered unread: 16,384 bytes of the answer go out, its flush left in flight, the rest parks, and 50,152 bytes are dropped, 15,383 short of a step. + client.ReleaseFlush(); + client.Feed([.. Open((InitialWindowSize, 16384)), .. WindowUpdate(0, 1 << 20), .. Post(1), .. Data(1, 1000, endStream: false), .. Fill(1, 3 * 16384)]); + Assert.True(ParkedUploads(client.Connection).TryGetValue(1, out int owed) && owed == 1000 + (3 * 16384), + $"the upload behind the parked answer was counted at {owed} bytes, not the 50152 dropped"); + + // Then a frame that would make the step. + switch (end) + { + case "it ended": + client.Feed(Data(1, 16384, endStream: true)); + break; + case "its answer ended": + client.Drain(); + client.Feed(WindowUpdate(1, 1 << 20)); + Assert.True(client.Drain().Any(f => f.StreamId == 1 && f.EndStream), "the answer never ended"); + client.Feed(Data(1, 16384, endStream: false)); + break; + case "the peer reset it": + client.Feed(RstStream(1, Cancel)); + break; + default: + // Past 2^31-1, so reset with FLOW_CONTROL_ERROR under the answer's flush, the client's DATA already on its way. + client.Feed([.. WindowUpdate(1, 1), .. WindowUpdate(1, int.MaxValue), .. Data(1, 16384, endStream: false)]); + break; + } + + int credited = Credit(client.Drain(), 1); + int left = ParkedUploads(client.Connection).Count; + Assert.True(credited == 0 && left == 0, + $"once {end}, the upload behind a parked answer was credited {credited} bytes, and {left} of its counts were left behind"); + + client.Close(run); + } + }); + } + + /// + /// A client uploading on one stream: it sends what the windows it counts allow, in frames of at most + /// frame bytes, and takes in everything the server flushes before each frame. + /// + private sealed class Uploader(StrictClient client, PeerWindow peer, int streamId, int frame) + { + public PeerWindow Peer => peer; + public List Frames { get; } = []; + public Frame ServerSettings { get; init; } + public long Sent { get; private set; } + public int DataFrames { get; private set; } + + // Sends on until `until` of a `size`-byte body; false once the client is out of window with nothing more coming. + public bool SendTo(long until, long size) + { + while (Sent < until) + { + Take(); + int room = (int)Math.Min(Math.Min(frame, peer.Room(streamId)), until - Sent); + if (room <= 0) + { + return false; + } + + client.Feed(Data(streamId, room, endStream: Sent + room == size)); + peer.Spend(streamId, room); + Sent += room; + DataFrames++; + } + + Take(); + return true; + } + + public void Take() + { + List flushed = client.Drain(); + peer.Absorb(flushed); + Frames.AddRange(flushed); + } + } + + // The preface taken in, the server's SETTINGS applied by the client or not, then the client's own preface and a POST on stream 1. + private static Uploader Start(StrictClient client, bool applied, int frame, bool acknowledge = false, + params (ushort Id, uint Value)[] clientSettings) + { + List preface = Frame.Walk(client.ReleaseFlush()); + var peer = new PeerWindow(); + peer.Absorb(applied ? preface : preface.Where(f => f.Type != Frame.Settings)); + + client.Feed([.. Open(clientSettings), .. (acknowledge ? SettingsAck() : []), .. WindowUpdate(0, 1 << 20), .. Post(1)]); + return new Uploader(client, peer, streamId: 1, frame) { ServerSettings = preface.First(f => f.Type == Frame.Settings) }; + } + + private static async ValueTask ReadAtLeast(Http2BodyReader body, int bytes) + { + long read = 0; + while (read < bytes) + { + read += (await body.ReadAsync()).Length; + } + return read; + } + + private static List Increments(IEnumerable frames, int streamId) + => frames.Where(f => f.Type == Frame.WindowUpdate && f.StreamId == streamId) + .Select(f => (int)(BinaryPrimitives.ReadUInt32BigEndian(f.Payload) & 0x7FFFFFFF)).ToList(); + + private static string Summary(List credit) + => credit.Count <= 8 ? string.Join(", ", credit) : $"{string.Join(", ", credit.Take(8))}, ... {credit.Count} in all"; + + private static byte[] SettingsAck() => FrameBytes(0x4, 0x1, 0, []); +} diff --git a/tests/Ioxide.Tests.Unit/Http2EarlyAnswerTests.cs b/tests/Ioxide.Tests.Unit/Http2EarlyAnswerTests.cs index 72fb2d46..071d0550 100644 --- a/tests/Ioxide.Tests.Unit/Http2EarlyAnswerTests.cs +++ b/tests/Ioxide.Tests.Unit/Http2EarlyAnswerTests.cs @@ -235,9 +235,11 @@ private static void RegisterCredit(Runner runner) List frames = client.Drain(); Assert.Equal(16384, Frame.Body(frames, 1).Length); // one stream window, then parked - client.Feed(Data(1, 16384, endStream: false)); + // A step's worth, in one WINDOW_UPDATE that counts the 1,000 bytes dropped unread with its handler too. + client.Feed(Fill(1, 4 * 16384)); List credited = client.Drain(); - Assert.Equal(16384, Credit(credited, 1)); + Assert.Equal(1000 + (4 * 16384), Credit(credited, 1)); + Assert.Equal(1, credited.Count(f => f is { Type: Frame.WindowUpdate, StreamId: 1 })); client.Feed(WindowUpdate(1, 1 << 20)); frames.AddRange(credited); diff --git a/tests/Ioxide.Tests.Unit/Http2FlowControlTests.cs b/tests/Ioxide.Tests.Unit/Http2FlowControlTests.cs index 7737476c..7c979953 100644 --- a/tests/Ioxide.Tests.Unit/Http2FlowControlTests.cs +++ b/tests/Ioxide.Tests.Unit/Http2FlowControlTests.cs @@ -186,11 +186,12 @@ await ReadAll(body, read, between: () => client.Close(run); }); + // The window's step is the padded body's payload: its padding-only frame completes it on arrival, the handler parked on its next read. runner.Test("h2 flow: padding on a streamed body is credited", () => { var read = new Tally(); - using var client = new StrictClient(); + using var client = new StrictClient(new Http2Options { InitialWindowSize = 4 * PaddedBodyPayload }); Task run = client.Connection.RunAsync(async (_, body, writer) => { await ReadAll(body, read); @@ -198,8 +199,12 @@ await ReadAll(body, read, between: () => }); client.ReleaseFlush(); - client.Feed([.. Open(), .. Post(1), .. PaddedBody(1)]); + byte[] padded = PaddedBody(1); + int first = padded.Length - (9 + 256); // the frame with the body, then the one of nothing but padding + client.Feed([.. Open(), .. Post(1), .. padded[..first]]); List frames = client.Drain(); + client.Feed(padded[first..]); + frames.AddRange(client.Drain()); Assert.Equal(1000L, read.Bytes); int credited = Credit(frames, 1); @@ -213,7 +218,7 @@ await ReadAll(body, read, between: () => runner.Test("h2 flow: padding on the buffered path is credited in full", () => { - using var client = new StrictClient(new Http2Options()); + using var client = new StrictClient(new Http2Options { InitialWindowSize = 4 * PaddedBodyPayload }); Task run = client.Connection.RunAsync(_ => new Http2Response { Status = 200 }); client.ReleaseFlush(); @@ -225,10 +230,10 @@ await ReadAll(body, read, between: () => client.Close(run); }); - // Its connection share was counted on arrival, and the stream is gone, so the reset is credited nothing. + // Its connection share was counted on arrival, and the stream is gone, so the reset is credited nothing. The window's step is one frame. runner.Test("h2 flow: a DATA frame past MaxRequestBytes resets its stream, and only the frames taken are credited", () => { - using var client = new StrictClient(new Http2Options { MaxRequestBytes = 20_000 }); + using var client = new StrictClient(new Http2Options { MaxRequestBytes = 20_000, InitialWindowSize = 4 * 16384 }); Task run = client.Connection.RunAsync(_ => new Http2Response { Status = 200 }); client.ReleaseFlush(); diff --git a/tests/Ioxide.Tests.Unit/Http2ReceiveWindowTests.cs b/tests/Ioxide.Tests.Unit/Http2ReceiveWindowTests.cs index c57c80c5..e2d107c4 100644 --- a/tests/Ioxide.Tests.Unit/Http2ReceiveWindowTests.cs +++ b/tests/Ioxide.Tests.Unit/Http2ReceiveWindowTests.cs @@ -284,13 +284,14 @@ void Send(byte[] frame, int streamId) client.Feed([.. Open(), .. Post(1), .. Get(3)]); // stream 3's answer leaves on this pass's flush, which the peer does not take Assert.Equal(1, client.PendingFlushes); - client.Feed([.. Data(1, 16384, endStream: false), .. Data(1, 16384, endStream: false), .. Ping()]); - Assert.True(read.Bytes == 2 * 16384, - $"behind the stalled flush the handler read {read.Bytes} of the {2 * 16384} bytes sent after it"); + const int Sent = 4 * 16384; // a step of credit, so the reads have some to queue + client.Feed([.. Fill(1, Sent), .. Ping()]); + Assert.True(read.Bytes == Sent, + $"behind the stalled flush the handler read {read.Bytes} of the {Sent} bytes sent after it"); Assert.Equal(1, client.PendingFlushes); // what the frames produced waits behind it List frames = client.Drain(); - Assert.Equal(2 * 16384, Credit(frames, 1)); + Assert.Equal(Sent, Credit(frames, 1)); Assert.True(frames.Any(f => f is { Type: PingFrame, Flags: 1 }), "the PING read behind the stalled flush was never answered"); client.Feed(Data(1, 0, endStream: true)); diff --git a/tests/Ioxide.Tests.Unit/Http2StreamedRequestTests.cs b/tests/Ioxide.Tests.Unit/Http2StreamedRequestTests.cs index ad6d9ede..6a64ec51 100644 --- a/tests/Ioxide.Tests.Unit/Http2StreamedRequestTests.cs +++ b/tests/Ioxide.Tests.Unit/Http2StreamedRequestTests.cs @@ -25,7 +25,8 @@ public static void Register(Runner runner) var gate = new TaskCompletionSource(); var chunks = new List(); - using var peer = new Peer(new Http2Options()); + // A 4,000-byte window is credited in steps of a quarter of it: the two chunks make one. + using var peer = new Peer(new Http2Options { InitialWindowSize = 4000 }); Task run = peer.Connection.RunAsync(async (_, body) => { await gate.Task; // hold the body unread, like a slow consumer @@ -137,7 +138,8 @@ public static void Register(Runner runner) int seen = -1; bool? hasBody = null; - using var peer = new Peer(new Http2Options()); + // A 1,600-byte window is credited in steps of 400: the first frame makes one. + using var peer = new Peer(new Http2Options { InitialWindowSize = 1600 }); Task run = peer.Connection.RunAsync(request => // no body reader: the body comes whole { seen = request.Body.Length; diff --git a/tests/Ioxide.Tests.Unit/Program.cs b/tests/Ioxide.Tests.Unit/Program.cs index bba8de8b..3fd7cf0c 100644 --- a/tests/Ioxide.Tests.Unit/Program.cs +++ b/tests/Ioxide.Tests.Unit/Program.cs @@ -40,6 +40,7 @@ private static int Main() Http2StreamedBodyTests.Register(runner); Http2FlowControlTests.Register(runner); Http2ReceiveWindowTests.Register(runner); + Http2CreditStepTests.Register(runner); Http2BufferedCreditTests.Register(runner); Http2EarlyAnswerTests.Register(runner); Http2EarlyAnswerGuardTests.Register(runner); From 143e397d6c29b81c794a4624ed0973393dfd154f Mon Sep 17 00:00:00 2001 From: Diogo Martins Date: Sat, 14 Nov 2026 23:29:17 +0000 Subject: [PATCH 2/3] http2: the upload behind a buffered answer parked on credit is dropped uncredited, like any stream its handler is done with (#630) The count kept for that upload is gone: the branch in RetireStream that started it, CreditParkedUpload, _parkedUploads and its Remove in CompleteParkedAsync. So is the credit origin/main gave the same upload, one WINDOW_UPDATE per frame dropped: HandleData returns for any stream outside the stream table. A client that uploads its whole body before it reads, given a buffered answer bigger than its stream window by a handler that did not read the upload, now waits until its own timeout. A client that reads while it uploads still gets the whole answer. Tests: the two that had a client finish its upload behind a parked answer, and the one that read the count, tested only what is removed and are gone. Http2EarlyAnswerTests' parked test now checks that the upload is credited nothing and that a client crediting the answer gets it whole; with origin/main's per-frame credit put back it fails with "expected [0], got [65536]". --- src/protocols/ioxide.http2/Http2BodyReader.cs | 2 +- .../ioxide.http2/Http2Connection.Frames.cs | 4 +- .../ioxide.http2/Http2Connection.Write.cs | 1 - src/protocols/ioxide.http2/Http2Connection.cs | 33 ------- .../Ioxide.Tests.Unit/Http2CreditStepTests.cs | 91 +------------------ .../Http2EarlyAnswerTests.cs | 17 ++-- 6 files changed, 12 insertions(+), 136 deletions(-) diff --git a/src/protocols/ioxide.http2/Http2BodyReader.cs b/src/protocols/ioxide.http2/Http2BodyReader.cs index f26d22d0..d0e600ae 100644 --- a/src/protocols/ioxide.http2/Http2BodyReader.cs +++ b/src/protocols/ioxide.http2/Http2BodyReader.cs @@ -195,7 +195,7 @@ internal void Drop() FireIfReady(); } - // Owed no credit here: the connection was charged for these on arrival, and a stream left open behind a parked answer counts them at its retire. + // Owed no credit: the connection was charged for these on arrival, and their stream is done. private void DropQueued() { while (_chunks.TryDequeue(out (byte[] Buffer, int Length) chunk)) diff --git a/src/protocols/ioxide.http2/Http2Connection.Frames.cs b/src/protocols/ioxide.http2/Http2Connection.Frames.cs index 8304e1f5..18d9c3ea 100644 --- a/src/protocols/ioxide.http2/Http2Connection.Frames.cs +++ b/src/protocols/ioxide.http2/Http2Connection.Frames.cs @@ -404,9 +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; any other stream's window went with it. - CreditParkedUpload(header.StreamId, payload.Length, ended); - return; + return; // done with its request, so owed no credit, behind a buffered answer still parked on credit too } // 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). diff --git a/src/protocols/ioxide.http2/Http2Connection.Write.cs b/src/protocols/ioxide.http2/Http2Connection.Write.cs index 7ebfcd9c..6413c622 100644 --- a/src/protocols/ioxide.http2/Http2Connection.Write.cs +++ b/src/protocols/ioxide.http2/Http2Connection.Write.cs @@ -484,7 +484,6 @@ private async Task CompleteParkedAsync(Http2ResponseWriter writer) { _creditWaiters.Remove(writer.StreamId); _responseWindows.Remove(writer.StreamId); - _parkedUploads.Remove(writer.StreamId); _writerPool.Push(writer); } } diff --git a/src/protocols/ioxide.http2/Http2Connection.cs b/src/protocols/ioxide.http2/Http2Connection.cs index beef1af9..e22c5fff 100644 --- a/src/protocols/ioxide.http2/Http2Connection.cs +++ b/src/protocols/ioxide.http2/Http2Connection.cs @@ -429,12 +429,6 @@ private void RetireStream(PendingRequest pending) { ResetStream(pending.StreamId, Http2Error.NoError); } - else if (live && !pending.RequestEnded) - { - // Its upload goes on behind the parked rest, and what its reader held is dropped below: all it was sent is consumed. - _parkedUploads[pending.StreamId] = 0; - CreditParkedUpload(pending.StreamId, _initialStreamWindow - pending.ReceiveWindow, ended: false); - } Recycle(pending); @@ -508,33 +502,6 @@ private bool CreditConsumed(PendingRequest pending) return true; } - // The upload still arriving behind each buffered answer parked on credit, by stream: what it has had dropped and not been credited. - private readonly Dictionary _parkedUploads = new(); - - // Credited in steps as it is dropped, so a client that credits the answer only once its upload ends can finish both. - private void CreditParkedUpload(int streamId, int dropped, bool ended) - { - if (!_parkedUploads.TryGetValue(streamId, out int owed)) - { - return; - } - - // An ended request is owed none, and a stream whose answer has gone takes none (RFC 9113 5.1). - if (ended || !_responseWindows.ContainsKey(streamId)) - { - _parkedUploads.Remove(streamId); - return; - } - - owed += dropped; - if (owed >= _creditStep) - { - WriteWindowUpdate(streamId, owed); - owed = 0; - } - _parkedUploads[streamId] = owed; - } - // Every WINDOW_UPDATE the server sends goes through these two, which add back exactly what they grant. private void CreditStream(PendingRequest pending, int increment) { diff --git a/tests/Ioxide.Tests.Unit/Http2CreditStepTests.cs b/tests/Ioxide.Tests.Unit/Http2CreditStepTests.cs index cfe718b0..2623d305 100644 --- a/tests/Ioxide.Tests.Unit/Http2CreditStepTests.cs +++ b/tests/Ioxide.Tests.Unit/Http2CreditStepTests.cs @@ -1,6 +1,5 @@ using System.Buffers.Binary; using System.Globalization; -using System.Runtime.CompilerServices; using System.Text; using ioxide.http2; using static Ioxide.Tests.Http2FlowControlTests; @@ -20,15 +19,11 @@ internal static class Http2CreditStepTests private const uint EnhanceYourCalm = 0xb; - [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_parkedUploads")] - private static extern ref Dictionary ParkedUploads(Http2Connection connection); - public static void Register(Runner runner) { RegisterSteps(runner); RegisterNoStall(runner); RegisterEnds(runner); - RegisterParked(runner); } private static void RegisterSteps(Runner runner) @@ -268,87 +263,6 @@ private static void RegisterEnds(Runner runner) }); } - private static void RegisterParked(Runner runner) - { - // The answer is twice the client's 16 KiB window for it, so its rest parks on credit the client gives only once its upload is done. - foreach (int unread in new[] { StreamWindow - 16384, StreamWindow }) - { - runner.Test($"h2 credit: a buffered answer parked on credit, its handler gone with {unread} bytes unread, still lets a client that credits it only after its upload finish both", () => - { - const long Size = 1L << 20; - byte[] answer = new byte[32 * 1024]; - var gate = new TaskCompletionSource(); - - using var client = new StrictClient(); - Task run = client.Connection.RunAsync(async (_, _) => - { - await gate.Task; - return new Http2Response { Status = 413, Body = answer }; - }); - - Uploader up = Start(client, applied: true, frame: 16384, clientSettings: [(InitialWindowSize, 16384)]); - Assert.True(up.SendTo(unread, Size), $"the upload stopped at {up.Sent} bytes, before the {unread} its handler leaves unread"); - - gate.SetResult(); - Assert.True(up.SendTo(Size, Size), - $"behind the parked answer the upload stopped at {up.Sent} of {Size} bytes, its stream credited [{Summary(Increments(up.Frames, 1))}]"); - - client.Feed(WindowUpdate(1, 1 << 20)); - up.Take(); - Assert.True(Frame.Body(up.Frames, 1).Length == answer.Length && up.Frames.Last(f => f.StreamId == 1 && f.Type == Frame.Data).EndStream, - $"the answer came to {Frame.Body(up.Frames, 1).Length} of {answer.Length} bytes once the client credited it"); - - List credit = Increments(up.Frames, 1); - Assert.True(credit.All(c => c >= Step), $"the stream was credited [{Summary(credit)}], not in whole steps"); - - client.Close(run); - }); - } - - runner.Test("h2 credit: an upload behind a parked answer is credited nothing, and leaves nothing behind, once it ends, once its answer has ended, or once either side resets it", () => - { - foreach (string end in new[] { "it ended", "its answer ended", "the peer reset it", "the server reset it" }) - { - using var client = new StrictClient(); - Task run = client.Connection.RunAsync((_, _) => new ValueTask(new Http2Response { Status = 413, Body = new byte[32 * 1024] })); - - // Answered unread: 16,384 bytes of the answer go out, its flush left in flight, the rest parks, and 50,152 bytes are dropped, 15,383 short of a step. - client.ReleaseFlush(); - client.Feed([.. Open((InitialWindowSize, 16384)), .. WindowUpdate(0, 1 << 20), .. Post(1), .. Data(1, 1000, endStream: false), .. Fill(1, 3 * 16384)]); - Assert.True(ParkedUploads(client.Connection).TryGetValue(1, out int owed) && owed == 1000 + (3 * 16384), - $"the upload behind the parked answer was counted at {owed} bytes, not the 50152 dropped"); - - // Then a frame that would make the step. - switch (end) - { - case "it ended": - client.Feed(Data(1, 16384, endStream: true)); - break; - case "its answer ended": - client.Drain(); - client.Feed(WindowUpdate(1, 1 << 20)); - Assert.True(client.Drain().Any(f => f.StreamId == 1 && f.EndStream), "the answer never ended"); - client.Feed(Data(1, 16384, endStream: false)); - break; - case "the peer reset it": - client.Feed(RstStream(1, Cancel)); - break; - default: - // Past 2^31-1, so reset with FLOW_CONTROL_ERROR under the answer's flush, the client's DATA already on its way. - client.Feed([.. WindowUpdate(1, 1), .. WindowUpdate(1, int.MaxValue), .. Data(1, 16384, endStream: false)]); - break; - } - - int credited = Credit(client.Drain(), 1); - int left = ParkedUploads(client.Connection).Count; - Assert.True(credited == 0 && left == 0, - $"once {end}, the upload behind a parked answer was credited {credited} bytes, and {left} of its counts were left behind"); - - client.Close(run); - } - }); - } - /// /// A client uploading on one stream: it sends what the windows it counts allow, in frames of at most /// frame bytes, and takes in everything the server flushes before each frame. @@ -392,14 +306,13 @@ public void Take() } // The preface taken in, the server's SETTINGS applied by the client or not, then the client's own preface and a POST on stream 1. - private static Uploader Start(StrictClient client, bool applied, int frame, bool acknowledge = false, - params (ushort Id, uint Value)[] clientSettings) + private static Uploader Start(StrictClient client, bool applied, int frame, bool acknowledge = false) { List preface = Frame.Walk(client.ReleaseFlush()); var peer = new PeerWindow(); peer.Absorb(applied ? preface : preface.Where(f => f.Type != Frame.Settings)); - client.Feed([.. Open(clientSettings), .. (acknowledge ? SettingsAck() : []), .. WindowUpdate(0, 1 << 20), .. Post(1)]); + client.Feed([.. Open(), .. (acknowledge ? SettingsAck() : []), .. WindowUpdate(0, 1 << 20), .. Post(1)]); return new Uploader(client, peer, streamId: 1, frame) { ServerSettings = preface.First(f => f.Type == Frame.Settings) }; } diff --git a/tests/Ioxide.Tests.Unit/Http2EarlyAnswerTests.cs b/tests/Ioxide.Tests.Unit/Http2EarlyAnswerTests.cs index 071d0550..59a6af66 100644 --- a/tests/Ioxide.Tests.Unit/Http2EarlyAnswerTests.cs +++ b/tests/Ioxide.Tests.Unit/Http2EarlyAnswerTests.cs @@ -222,9 +222,8 @@ private static void RegisterCredit(Runner runner) client.Close(run); }); - runner.Test("h2 early answer: an upload still arriving while a buffered answer is parked on credit is credited on its stream", () => + runner.Test("h2 early answer: an upload still arriving behind a buffered answer parked on credit is credited nothing, and a client that credits the answer gets it whole", () => { - // A client that credits its answer only once its upload ends can then end it. byte[] answer = new byte[64 * 1024]; using var client = new StrictClient(); @@ -235,16 +234,16 @@ private static void RegisterCredit(Runner runner) List frames = client.Drain(); Assert.Equal(16384, Frame.Body(frames, 1).Length); // one stream window, then parked - // A step's worth, in one WINDOW_UPDATE that counts the 1,000 bytes dropped unread with its handler too. + // Its handler is done with the request, so what still arrives is dropped and its window stays shut. client.Feed(Fill(1, 4 * 16384)); - List credited = client.Drain(); - Assert.Equal(1000 + (4 * 16384), Credit(credited, 1)); - Assert.Equal(1, credited.Count(f => f is { Type: Frame.WindowUpdate, StreamId: 1 })); + List dropped = client.Drain(); + Assert.Equal(0, Credit(dropped, 1)); - client.Feed(WindowUpdate(1, 1 << 20)); - frames.AddRange(credited); + client.Feed(WindowUpdate(1, 1 << 20)); // a client that reads its answer while it uploads + frames.AddRange(dropped); frames.AddRange(client.Drain()); - Assert.Equal(answer.Length, Frame.Body(frames, 1).Length); + Assert.True(Frame.Body(frames, 1).Length == answer.Length && frames.Last(f => f is { Type: Frame.Data, StreamId: 1 }).EndStream, + $"the answer came to {Frame.Body(frames, 1).Length} of {answer.Length} bytes once the client credited it"); client.Close(run); }); From 2a734acd568a2168bdedd636d4bebeec9a4eb5e9 Mon Sep 17 00:00:00 2001 From: Diogo Martins Date: Sat, 14 Nov 2026 23:36:47 +0000 Subject: [PATCH 3/3] http2: an answer that waited on the client's credit is followed by RST_STREAM(NO_ERROR) while its request is still arriving (#588) A buffered answer bigger than the client's stream window parks the rest on the client's credit, and its stream is retired with the rest still to go, so the retire does not reset it. An answer that ends at once is followed by RST_STREAM(NO_ERROR) once its END_STREAM is out, which asks the client to stop sending a request the server is done with and leaves it the answer (RFC 9113 8.1); a parked one was not. With that upload no longer credited, a client that reads while it uploads got the whole answer and then had its upload stall. ParkRest now reads whether the request is still arriving, before the retire takes the stream out of the stream table, and the parked rest resets the stream with NO_ERROR once its END_STREAM is staged, if the response is still live: not after the peer's reset, nor after one of the stack's own. No connection state. One imprecision: a request END_STREAM that arrives while the answer is parked is not seen, so the reset can follow a stream already closed both ways; RFC 9113 5.1 has the peer ignore a RST_STREAM in that state. Tests: - Unit: a buffered answer that waited on credit, its request still arriving, is followed by RST_STREAM(NO_ERROR) after its END_STREAM, as the stream's last frame. Without the reset: "the answer ended with its request still arriving, and no RST_STREAM asked the client to stop uploading". - Unit control: a request that had ended before its answer parked, with a body reader and without, gets no reset. Reading only whether the stream is in the table: "a request that had ended was reset". - Unit guard: no reset back once the peer has reset the stream, nor a second after the stack's own FLOW_CONTROL_ERROR, both while the answer's first flush is in flight. Without the liveness check: "stream 1 was reset after the peer had reset it". - E2E: .NET's HttpClient with a duplex content, which reads while it uploads, posting 64 MiB to a handler that answers a 256 KiB 413 unread: the answer arrives whole and the upload stops under 512 KiB. Without the reset: "the upload neither finished nor stopped, at 262144 bytes". --- .../ioxide.http2/Http2Connection.Write.cs | 13 +++- .../Protocols/EarlyAnswerTests.cs | 64 +++++++++++++++++-- .../Http2EarlyAnswerGuardTests.cs | 33 ++++++++++ .../Http2EarlyAnswerTests.cs | 43 +++++++++++++ 4 files changed, 146 insertions(+), 7 deletions(-) diff --git a/src/protocols/ioxide.http2/Http2Connection.Write.cs b/src/protocols/ioxide.http2/Http2Connection.Write.cs index 6413c622..9ca2d941 100644 --- a/src/protocols/ioxide.http2/Http2Connection.Write.cs +++ b/src/protocols/ioxide.http2/Http2Connection.Write.cs @@ -467,14 +467,23 @@ private void ParkRest(int streamId, ReadOnlySpan rest) rest.CopyTo(writer.GetSpan(rest.Length)); writer.Advance(rest.Length); - _ = CompleteParkedAsync(writer); + // Read before the retire takes the stream out of _streams, which leaves a request still arriving here for the rest's end to reset. + bool arriving = _streams.TryGetValue(streamId, out PendingRequest? pending) && !pending.RequestEnded; + _ = CompleteParkedAsync(writer, arriving); } - private async Task CompleteParkedAsync(Http2ResponseWriter writer) + private async Task CompleteParkedAsync(Http2ResponseWriter writer, bool arriving) { try { await writer.CompleteAsync(); + + // After its END_STREAM, so the client keeps the answer and stops uploading (RFC 9113 8.1); not once the stream is reset. + if (arriving && IsResponseLive(writer.StreamId)) + { + ResetStream(writer.StreamId, Http2Error.NoError); + await MaybeFlushAsync(); + } } catch { diff --git a/tests/Ioxide.Tests.E2E/Protocols/EarlyAnswerTests.cs b/tests/Ioxide.Tests.E2E/Protocols/EarlyAnswerTests.cs index 66b904de..7092065b 100644 --- a/tests/Ioxide.Tests.E2E/Protocols/EarlyAnswerTests.cs +++ b/tests/Ioxide.Tests.E2E/Protocols/EarlyAnswerTests.cs @@ -41,6 +41,9 @@ private static void RegisterHttpClient(Runner runner) byte[] bufferedAnswer = Pattern(8 * 1024); + // Past the client's 64 KiB stream window, so the rest waits on the credit it gives as it reads. + byte[] parkedAnswer = Pattern(256 * 1024); + runner.Test("h2 early answer: a streamed 413 to an upload arrives whole, and the client stops uploading", () => { int port = StartH2(connection => connection.RunAsync((_, _, writer) => AnswerAsync(writer, h2Answer))); @@ -56,6 +59,14 @@ private static void RegisterHttpClient(Runner runner) AssertAnsweredAndStopped(client, $"http://127.0.0.1:{port}/", bufferedAnswer, H2Stopped); }); + runner.Test("h2 early answer: a buffered 413 that waits on the client's credit arrives whole to a client that reads while it uploads, and the client stops uploading", () => + { + int port = StartH2(connection => connection.RunAsync((_, _) => + new ValueTask(new Http2Response { Status = 413, Body = parkedAnswer }))); + using HttpClient client = H2Client(); + AssertAnsweredAndStopped(client, $"http://127.0.0.1:{port}/", parkedAnswer, H2Stopped, duplex: true); + }); + runner.Test("control: h2 an upload its handler reads to the end is sent whole and answered", () => { int port = StartH2(connection => connection.RunAsync(async (_, body, writer) => @@ -191,9 +202,9 @@ 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); + ICountedUpload 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"); @@ -217,11 +228,11 @@ 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, ICountedUpload upload) { try { - using HttpResponseMessage response = await client.PostAsync(url, upload); + using HttpResponseMessage response = await client.PostAsync(url, (HttpContent)upload); byte[] body = await response.Content.ReadAsByteArrayAsync(); // Held open until then: disposing it would have the client cancel the upload itself. @@ -372,8 +383,16 @@ private static byte[] Pattern(int length) return bytes; } + // How far an upload got, and when it ended, whichever content sent it. + private interface ICountedUpload + { + long Sent { get; } + + Task Ended { get; } + } + // 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 sealed class CountedUpload : StreamContent, ICountedUpload { private readonly Source _source; private readonly TaskCompletionSource _ended = new(TaskCreationOptions.RunContinuationsAsynchronously); @@ -406,6 +425,41 @@ protected override async Task SerializeToStreamAsync(Stream stream, TransportCon } } + // Not a StreamContent, so duplex: over HTTP/2 the client reads, and credits, the answer while it still uploads. + private sealed class DuplexUpload(long size) : HttpContent, ICountedUpload + { + private readonly TaskCompletionSource _ended = new(TaskCreationOptions.RunContinuationsAsynchronously); + private long _sent; + + 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 < size) + { + int count = (int)Math.Min(chunk.Length, size - Sent); + await stream.WriteAsync(chunk.AsMemory(0, count)); + Interlocked.Add(ref _sent, count); + } + } + finally + { + _ended.TrySetResult(Sent); + } + } + + protected override bool TryComputeLength(out long length) + { + length = size; + 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/Http2EarlyAnswerGuardTests.cs b/tests/Ioxide.Tests.Unit/Http2EarlyAnswerGuardTests.cs index 115d528c..8e618f45 100644 --- a/tests/Ioxide.Tests.Unit/Http2EarlyAnswerGuardTests.cs +++ b/tests/Ioxide.Tests.Unit/Http2EarlyAnswerGuardTests.cs @@ -173,6 +173,39 @@ public static void Register(Runner runner) client.Close(run); }); + runner.Test("h2 early answer: a buffered answer that waited on credit, its request still arriving, gets no reset back once the peer has reset it, nor a second once the stack has", () => + { + foreach (bool byPeer in new[] { true, false }) + { + using var client = new StrictClient(); + Task run = client.Connection.RunAsync((_, _) => new ValueTask(Http2Response.Text(new string('x', 32 * 1024), 413))); + + // Answered unread, the rest parked on credit, and the flush of its first 16 KiB left in flight. + client.ReleaseFlush(); + client.Feed([.. Open((InitialWindowSize, 16384)), .. WindowUpdate(0, 1 << 20), .. Post(1), .. Data(1, 1000, endStream: false)]); + + if (byPeer) + { + client.Feed(RstStream(1, Cancel)); + } + else + { + client.Feed([.. WindowUpdate(1, 1), .. WindowUpdate(1, 0x7FFFFFFF)]); // past 2^31-1 once the window holds a byte: FLOW_CONTROL_ERROR + } + List frames = client.Drain(); + + if (byPeer) + { + Assert.True(Resets(frames, 1).Length == 0, "stream 1 was reset after the peer had reset it"); + } + else + { + AssertResetOnce(frames, FlowControlError); + } + + client.Close(run); + } + }); } private static void AssertResetOnce(List frames, uint code) diff --git a/tests/Ioxide.Tests.Unit/Http2EarlyAnswerTests.cs b/tests/Ioxide.Tests.Unit/Http2EarlyAnswerTests.cs index 59a6af66..202bb9a9 100644 --- a/tests/Ioxide.Tests.Unit/Http2EarlyAnswerTests.cs +++ b/tests/Ioxide.Tests.Unit/Http2EarlyAnswerTests.cs @@ -132,6 +132,25 @@ private static void RegisterResets(Runner runner) frames.AddRange(client.Drain()); AssertAnsweredThenStopped(frames, streamId: 1, 500, ""); + client.Close(run); + }); + runner.Test("h2 early answer: a buffered answer that waited on the client's credit, its request still arriving, is followed by RST_STREAM(NO_ERROR) once its END_STREAM is out", () => + { + string answer = new('x', 32 * 1024); // twice the client's window for it, so the rest waits on its credit + + using var client = new StrictClient(); + Task run = client.Connection.RunAsync((_, _) => new ValueTask(Http2Response.Text(answer, 413))); + + client.ReleaseFlush(); + client.Feed([.. Open((InitialWindowSize, 16384)), .. WindowUpdate(0, 1 << 20), .. Post(1), .. Data(1, 1000, endStream: false)]); + List frames = client.Drain(); + Assert.True(!frames.Any(f => f.StreamId == 1 && (f.EndStream || f.Type == RstStreamFrame)), + "the answer ended, or its stream was reset, before the client had credited the rest of it"); + + client.Feed([.. Data(1, 16384, endStream: false), .. WindowUpdate(1, 1 << 20)]); // a client that reads its answer while it uploads + frames.AddRange(client.Drain()); + AssertAnsweredThenStopped(frames, streamId: 1, 413, answer); + client.Close(run); }); } @@ -199,6 +218,30 @@ private static void RegisterControls(Runner runner) client.Close(run); }); + runner.Test("control: a buffered answer that waited on the client's credit, to a request that had ended, is not followed by a reset", () => + { + string answer = new('x', 32 * 1024); + + // With a body reader the request is still in the stream table when its answer parks; without one it left at its END_STREAM. + foreach (bool reader in new[] { true, false }) + { + using var client = new StrictClient(); + Task run = reader + ? client.Connection.RunAsync((_, _) => new ValueTask(Http2Response.Text(answer, 413))) + : client.Connection.RunAsync(_ => Http2Response.Text(answer, 413)); + + client.ReleaseFlush(); + client.Feed([.. Open((InitialWindowSize, 16384)), .. WindowUpdate(0, 1 << 20), .. Post(1), .. Data(1, 1000, endStream: true)]); + List frames = client.Drain(); + Assert.True(!frames.Any(f => f.StreamId == 1 && f.EndStream), "the answer ended before the client had credited the rest of it"); + + client.Feed(WindowUpdate(1, 1 << 20)); + frames.AddRange(client.Drain()); + AssertAnsweredNotReset(frames, answer); + + client.Close(run); + } + }); } private static void RegisterCredit(Runner runner)