Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Playground/Http2/ManagedStreamedRequest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte> chunk = await body.ReadAsync();
if (chunk.IsEmpty)
{
Expand Down
8 changes: 4 additions & 4 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5496,10 +5496,10 @@ <h3>HTTP/2 &middot; request streamed</h3>
long total = 0;
while (true)
{
// Empty means end of body. Each read hands back the peer&#x27;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&lt;byte&gt; chunk = await body.ReadAsync();
if (chunk.IsEmpty)
{
Expand Down
19 changes: 14 additions & 5 deletions src/protocols/ioxide.http2/Http2BodyReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ namespace ioxide.http2;
/// Whether there is one to read is <see cref="Http2Request.HasBody"/>, 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 <see cref="Http2Options.InitialWindowSize"/>
/// 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'.
Expand All @@ -37,6 +38,10 @@ public sealed class Http2BodyReader : IValueTaskSource<ReadOnlyMemory<byte>>
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
Expand Down Expand Up @@ -72,7 +77,8 @@ public ValueTask<ReadOnlyMemory<byte>> 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<ReadOnlyMemory<byte>>(chunk.Buffer.AsMemory(0, chunk.Length));
}

Expand All @@ -99,6 +105,7 @@ internal void Push(ReadOnlySpan<byte> data)
byte[] buffer = ArrayPool<byte>.Shared.Rent(data.Length);
data.CopyTo(buffer);
_chunks.Enqueue((buffer, data.Length));
Unread += data.Length;

if (_armed)
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -194,6 +202,7 @@ private void DropQueued()
{
ArrayPool<byte>.Shared.Return(chunk.Buffer);
}
Unread = 0;
}

private void ReleaseHandedOut()
Expand Down
16 changes: 5 additions & 11 deletions src/protocols/ioxide.http2/Http2Connection.Frames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,13 +404,7 @@ private void HandleData(in FrameHeader header, ReadOnlySpan<byte> 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).
Expand Down Expand Up @@ -447,11 +441,11 @@ private void HandleData(in FrameHeader header, ReadOnlySpan<byte> 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)
Expand Down
13 changes: 11 additions & 2 deletions src/protocols/ioxide.http2/Http2Connection.Write.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@
}
}

Span<byte> header = stackalloc byte[FrameHeader.Size];

Check warning on line 409 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 409 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 409 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 409 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04-arm)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 409 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04-arm)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 409 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04-arm)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 409 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04-arm)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)
new FrameHeader(chunk, first ? FrameType.Headers : FrameType.Continuation, flags, streamId)
.Write(header);

Expand Down Expand Up @@ -447,7 +447,7 @@

bool last = offset + chunk >= body.Length;

Span<byte> header = stackalloc byte[FrameHeader.Size];

Check warning on line 450 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 450 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 450 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 450 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04-arm)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 450 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04-arm)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 450 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04-arm)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)
new FrameHeader(chunk, FrameType.Data, last ? FrameFlags.EndStream : FrameFlags.None, streamId)
.Write(header);

Expand All @@ -467,14 +467,23 @@
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
{
Expand Down
29 changes: 24 additions & 5 deletions src/protocols/ioxide.http2/Http2Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}

/// <summary>Convenience for cleartext h2c: wraps the connection in its own duplex pipe.</summary>
Expand Down Expand Up @@ -484,16 +488,31 @@ private PendingRequest RentPending(int streamId)
return pending;
}

/// <summary>Return a read chunk's credit to its stream alone: the connection was charged for it on arrival.</summary>
internal void CreditBody(int streamId, int length)
/// <summary>A chunk was read: its stream alone is credited once a step is consumed, as the connection was charged for it on arrival.</summary>
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.
Expand Down
5 changes: 4 additions & 1 deletion src/protocols/ioxide.http2/Http2Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 59 additions & 5 deletions tests/Ioxide.Tests.E2E/Protocols/EarlyAnswerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand All @@ -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<Http2Response>(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) =>
Expand Down Expand Up @@ -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");
Expand All @@ -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.
Expand Down Expand Up @@ -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<long> 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<long> _ended = new(TaskCreationOptions.RunContinuationsAsynchronously);
Expand Down Expand Up @@ -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<long> _ended = new(TaskCreationOptions.RunContinuationsAsynchronously);
private long _sent;

public long Sent => Interlocked.Read(ref _sent);

public Task<long> 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
{
Expand Down
Loading
Loading