Skip to content
Closed
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
33 changes: 26 additions & 7 deletions src/protocols/ioxide.http2/Http2BodyReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ namespace ioxide.http2;
///
/// The window is the stream's own. The connection's is opened to its maximum and kept there, so a
/// handler that never reads holds back only its own upload, never the other streams'.
///
/// A handler that answers without reading its body to the end leaves the rest to the connection
/// once it returns: while its response still waits on the client's credit, what arrives is credited
/// and dropped, and once the response has ended a request still arriving is reset with NO_ERROR. A
/// client that reads its response only once its upload has ended, as .NET's <c>HttpClient</c> does
/// with a content that is not duplex, can then finish both. A handler that instead waits on
/// <see cref="Http2ResponseWriter.FlushAsync"/> for that credit, its body unread, waits until such a
/// client gives up.
/// </summary>
/// <remarks>
/// Single consumer, on any thread: a read made off the reactor is served on it. Each
Expand Down Expand Up @@ -88,12 +96,18 @@ public ValueTask<ReadOnlyMemory<byte>> ReadAsync()

// Body bytes straight off a DATA frame. The span points into the connection's accumulator and
// dies when the parser moves on, so it has to be copied. Never completes a parked reader
// inline: the wake is deferred to FireIfReady, after the parser unwinds.
internal void Push(ReadOnlySpan<byte> data)
// inline: the wake is deferred to FireIfReady, after the parser unwinds. False once the body has
// ended: its handler is done with it, and the caller credits bytes nobody will read.
internal bool Push(ReadOnlySpan<byte> data)
{
if (_ended)
{
return false;
}

if (data.IsEmpty)
{
return;
return true;
}

byte[] buffer = ArrayPool<byte>.Shared.Rent(data.Length);
Expand All @@ -104,6 +118,7 @@ internal void Push(ReadOnlySpan<byte> data)
{
_owner.NoteBodyWake(this);
}
return true;
}

/// <summary>End of body: END_STREAM, or a cut one once <see cref="Abort"/> has said so. Idempotent.</summary>
Expand Down Expand Up @@ -175,25 +190,29 @@ internal void Abort(string? reason = null)
}

// The handler is done with the request, or never got it: recycle everything, the chunk it held
// included, and complete a read it left parked.
// included, and complete a read it left parked. Ended, so DATA still on its way is credited as it
// arrives while the stream stays open for a response waiting on credit.
internal void Drop()
{
ReleaseHandedOut();
DropQueued();
_owner.CreditBody(_streamId, DropQueued());

// Drained first, so the wake below reports end-of-body rather than handing out a chunk
// whose stream is already gone.
_ended = true;
FireIfReady();
}

// Owed no credit: the connection was charged for these on arrival, and their stream is done.
private void DropQueued()
// The connection was charged for these on arrival; only a stream still open is owed them, which CreditBody checks.
private int DropQueued()
{
int dropped = 0;
while (_chunks.TryDequeue(out (byte[] Buffer, int Length) chunk))
{
dropped += chunk.Length;
ArrayPool<byte>.Shared.Return(chunk.Buffer);
}
return dropped;
}

private void ReleaseHandedOut()
Expand Down
20 changes: 9 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; // closed, so owed no credit (RFC 9113 5.1): a request whose answer waits on credit stays in _streams
}

// Past its window is a stream error (6.9), and its response stops with it; an empty frame may still end it (6.9.1).
Expand All @@ -433,9 +427,14 @@ private void HandleData(in FrameHeader header, ReadOnlySpan<byte> payload)
return;
}

// Consumed now: a buffered body, one its handler is done with, or a streamed one's padding, its body credited as read.
int credit = payload.Length;
if (pending.BodyReader is { } reader)
{
reader.Push(body);
if (reader.Push(body))
{
credit -= body.Length;
}
}
else
{
Expand All @@ -447,9 +446,8 @@ 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)
// An ended request is owed none.
if (credit > 0 && !ended && !pending.RequestEnded)
{
CreditStream(pending, credit);
}
Expand Down
15 changes: 11 additions & 4 deletions src/protocols/ioxide.http2/Http2Connection.Streamed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,17 @@ private async Task ServeStreamedAsync(Func<Http2Request, Http2ResponseWriter, Va
// The buffered dispatch retires the stream in its own finally; the streamed one skips
// that path, so it has to do it here. Without this every response leaks its arena and
// the connection grows without bound - 20 GB over eight seconds of load, measured.
_creditWaiters.Remove(writer.StreamId);
_responseWindows.Remove(writer.StreamId);
RetireStream(pending);
_writerPool.Push(writer);
if (writer.ParkedEnd is { IsCompleted: false })
{
RetireAfterEnd(writer, pending);
}
else
{
_creditWaiters.Remove(writer.StreamId);
_responseWindows.Remove(writer.StreamId);
RetireStream(pending);
_writerPool.Push(writer);
}
}
}

Expand Down
39 changes: 9 additions & 30 deletions src/protocols/ioxide.http2/Http2Connection.Write.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,22 +346,20 @@

private static readonly byte[] StatusName = ":status"u8.ToArray();

private void WriteResponse(int streamId, Http2Response response)
// Null once the body is staged whole; otherwise the writer its rest waits in for credit, which the caller retires the stream behind.
private Http2ResponseWriter? WriteResponse(int streamId, Http2Response response)
{
ReadOnlyMemory<byte> body = response.Body; // a null response throws here, with the window still there for the 500

// Gone once the peer has reset the stream, and nothing may follow a reset (RFC 9113 5.1).
if (!_responseWindows.Remove(streamId, out int window))
{
return;
return null;
}

WriteHeaders(streamId, response, endStream: body.Length == 0);

if (body.Length > 0)
{
WriteData(streamId, body.Span, window);
}
return body.Length > 0 ? WriteData(streamId, body.Span, window) : null;
}

private void WriteHeaders(int streamId, Http2Response response, bool endStream)
Expand Down Expand Up @@ -406,7 +404,7 @@
}
}

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

Check warning on line 407 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 407 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 407 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 407 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 407 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 407 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 407 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 All @@ -426,7 +424,7 @@
}
}

private void WriteData(int streamId, ReadOnlySpan<byte> body, int window)
private Http2ResponseWriter? WriteData(int streamId, ReadOnlySpan<byte> body, int window)
{
int offset = 0;

Expand All @@ -441,13 +439,12 @@
{
// Out of credit: the rest waits for a WINDOW_UPDATE the way a streamed response does.
_responseWindows[streamId] = window;
ParkRest(streamId, body[offset..]);
return;
return ParkRest(streamId, body[offset..]);
}

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

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

Check warning on line 447 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 447 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 447 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 447 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 447 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 447 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 @@ -457,35 +454,17 @@
window -= chunk;
offset += chunk;
}
return null;
}

// Copied into a writer, because the body is the handler's again once it has returned.
private void ParkRest(int streamId, ReadOnlySpan<byte> rest)
private Http2ResponseWriter ParkRest(int streamId, ReadOnlySpan<byte> rest)
{
Http2ResponseWriter writer = RentWriter(streamId);
writer.ContinueBody();
rest.CopyTo(writer.GetSpan(rest.Length));
writer.Advance(rest.Length);

_ = CompleteParkedAsync(writer);
}

private async Task CompleteParkedAsync(Http2ResponseWriter writer)
{
try
{
await writer.CompleteAsync();
}
catch
{
// FlushAsync has already marked the connection broken; the read loop sees it.
}
finally
{
_creditWaiters.Remove(writer.StreamId);
_responseWindows.Remove(writer.StreamId);
_writerPool.Push(writer);
}
return writer;
}

// Three ASCII digits: HTTP/2 has no reason phrase and every valid status is 100..599.
Expand Down
79 changes: 72 additions & 7 deletions src/protocols/ioxide.http2/Http2Connection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Buffers;
using System.IO.Pipelines;
using System.Runtime.CompilerServices;

namespace ioxide.http2;

Expand Down Expand Up @@ -331,13 +332,14 @@ private void DispatchBuffered(Func<Http2Request, ValueTask<Http2Response>> handl
// there is no Task to allocate.
if (inFlight.IsCompletedSuccessfully)
{
Http2ResponseWriter? parked = null;
try
{
WriteResponse(pending.StreamId, inFlight.Result);
parked = WriteResponse(pending.StreamId, inFlight.Result);
}
finally
{
RetireStream(pending);
Retire(pending, parked);
}
continue;
}
Expand All @@ -358,11 +360,12 @@ private void DispatchBuffered(Func<Http2Request, ValueTask<Http2Response>> handl
/// </summary>
private async Task CompleteBufferedAsync(ValueTask<Http2Response> inFlight, Http2Request request, PendingRequest pending)
{
Http2ResponseWriter? parked = null;
try
{
Http2Response response = await inFlight;
await new ReactorSwitch(_reactor); // back on the reactor, wherever the handler finished
WriteResponse(pending.StreamId, response);
parked = WriteResponse(pending.StreamId, response);
}
catch (Exception exception)
{
Expand All @@ -373,12 +376,12 @@ private async Task CompleteBufferedAsync(ValueTask<Http2Response> inFlight, Http

if (!IsBroken)
{
WriteResponse(pending.StreamId, new Http2Response { Status = 500 });
WriteResponse(pending.StreamId, new Http2Response { Status = 500 }); // no body, so nothing parks
}
}
finally
{
RetireStream(pending);
Retire(pending, parked);
await MaybeFlushAsync();
}
}
Expand Down Expand Up @@ -418,7 +421,7 @@ private void RetireStream(PendingRequest pending)
// Every reset takes the stream out of _streams; short of one, a streamed writer has staged END_STREAM.
bool live = _streams.Remove(pending.StreamId) && !IsBroken;

// A buffered rest parked on credit keeps its window: its END_STREAM is still to go.
// A response parked on credit keeps its window: its END_STREAM is still to go.
bool reset = live && !pending.RequestEnded && !_responseWindows.ContainsKey(pending.StreamId);

if (reset)
Expand All @@ -435,6 +438,68 @@ private void RetireStream(PendingRequest pending)
}
}

// A handler is done with its stream: retired now, unless the rest of its response waits on credit in a writer.
private void Retire(PendingRequest pending, Http2ResponseWriter? parked)
{
if (parked is null)
{
RetireStream(pending);
}
else
{
RetireAfterEnd(parked, pending);
}
}

/// <summary>
/// A handler is done, and the end of its response waits on the peer's credit. Its stream stays open behind it:
/// a request still arriving is drained meanwhile, credited and dropped, since a client that reads its response
/// only once its upload has ended credits nothing before then. The stream is retired once the end is out, and
/// reset with NO_ERROR after it if the request is still arriving then (RFC 9113 8.1).
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
private void RetireAfterEnd(Http2ResponseWriter writer, PendingRequest pending)
{
PendingRequest? open = null;
if (!IsBroken && _streams.ContainsKey(pending.StreamId))
{
pending.BodyReader?.Drop(); // credits what the handler left unread, and DATA still to come as it lands
open = pending;
}
else
{
RetireStream(pending); // reset, broken, or a whole body that left _streams once it arrived: no stream to hold open
}

_ = FinishParkedAsync(writer, open);
}

private async Task FinishParkedAsync(Http2ResponseWriter writer, PendingRequest? open)
{
try
{
await writer.CompleteAsync(); // a buffered rest starts here; a streamed handler's writer has completed already
if (writer.ParkedEnd is { } end)
{
await end;
}
}
catch
{
// FlushAsync has already marked the connection broken; the read loop sees it.
}
finally
{
_creditWaiters.Remove(writer.StreamId);
_responseWindows.Remove(writer.StreamId);
if (open is not null)
{
RetireStream(open);
}
_writerPool.Push(writer);
}
}

// Kept unless the pool is full or a peer's header lines grew its lists; only the reactor rents.
private void Recycle(PendingRequest pending)
{
Expand All @@ -445,7 +510,7 @@ private void Recycle(PendingRequest pending)
}
}

// Ended before its request did: without a body reader it was never dispatched; with one, its handler retires it.
// Ended before its request did: without a body reader it was never dispatched; with one, its handler or the response it left on credit retires it.
private void Discard(PendingRequest pending)
{
if (pending.BodyReader is null)
Expand Down
Loading
Loading