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..d0e600ae 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) @@ -194,6 +202,7 @@ private void DropQueued() { 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..18d9c3ea 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; // 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). @@ -447,11 +441,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..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/src/protocols/ioxide.http2/Http2Connection.cs b/src/protocols/ioxide.http2/Http2Connection.cs index c94d63ce..c269c524 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. @@ -484,16 +488,31 @@ 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; } // 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.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/Http2CreditStepTests.cs b/tests/Ioxide.Tests.Unit/Http2CreditStepTests.cs new file mode 100644 index 00000000..2623d305 --- /dev/null +++ b/tests/Ioxide.Tests.Unit/Http2CreditStepTests.cs @@ -0,0 +1,337 @@ +using System.Buffers.Binary; +using System.Globalization; +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; + + public static void Register(Runner runner) + { + RegisterSteps(runner); + RegisterNoStall(runner); + RegisterEnds(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); + }); + } + + /// + /// 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) + { + List preface = Frame.Walk(client.ReleaseFlush()); + var peer = new PeerWindow(); + peer.Absorb(applied ? preface : preface.Where(f => f.Type != Frame.Settings)); + + 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) }; + } + + 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/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 5843827f..0c50d847 100644 --- a/tests/Ioxide.Tests.Unit/Http2EarlyAnswerTests.cs +++ b/tests/Ioxide.Tests.Unit/Http2EarlyAnswerTests.cs @@ -203,6 +203,26 @@ private static void RegisterResets(Runner runner) Assert.True(frames.All(f => f.StreamId is not (1 or 3)), $"frames went out on the streams of a torn pass: [{string.Join(", ", frames.Select(f => $"stream {f.StreamId} type {f.Type}"))}]"); }); + + 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); + }); } private static void RegisterControls(Runner runner) @@ -268,6 +288,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) @@ -291,9 +335,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(); @@ -304,14 +347,16 @@ 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)); - List credited = client.Drain(); - Assert.Equal(16384, Credit(credited, 1)); + // 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 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); }); 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 2138ef0d..6aef174a 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);