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
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.nghttp2;

Expand Down Expand Up @@ -96,7 +97,11 @@ private static int WriteStatus(int status, Span<byte> destination)
private bool _draining;
private bool _drainAgain;

private async ValueTask FlushEgressAsync()
// The read loop's wait, at its end, for the drain in flight to be done.
private TaskCompletionSource? _drained;

// A detached drain, as the read loop's is, has nobody to throw to: its fault is reported through IsBroken.
private async ValueTask FlushEgressAsync(bool detached = false)
{
if (_handle == 0)
{
Expand Down Expand Up @@ -146,12 +151,60 @@ private async ValueTask FlushEgressAsync()
}
while (_drainAgain && _handle != 0);
}
catch
{
_failed = true; // the transport refused a write: nothing queued can leave now
if (!detached)
{
throw;
}
}
finally
{
_draining = false;
if (OffReactor)
{
_ = EndDrainOnReactorAsync(); // a flush that faulted ends the drain on the thread that completed it
}
else
{
EndDrain();
}
}
}

private async Task EndDrainOnReactorAsync()
{
try
{
await new ReactorSwitch(_reactor);
}
catch (ObjectDisposedException)
{
// The reactor has stopped, so nothing races the end of the drain: it goes on here.
}
EndDrain();
}

// Out of line: what it runs loops, and a loop in an async method's finally costs it its optimized code.
[MethodImpl(MethodImplOptions.NoInlining)]
private void EndDrain()
{
_draining = false;

// The read loop reads on behind a drain, so one that found the connection gone ends that read, or the next.
if (_failed && !_disposed)
{
_pipe.Input.CancelPendingRead();
}

FireBodyWakes(); // a stream this drain closed, sending its RST_STREAM, ends its reader now
WakeParkedWriters();

if (_drained is { } drained)
{
_drained = null;
drained.SetResult();
}
}

private unsafe int DrainOnce()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ private async Task RunCoreAsync(Action dispatch)
Setup();

// SETTINGS go out before anything is read: a peer that opens with the preface and a
// request immediately expects ours to be on the way.
// request immediately expects ours to be on the way. Waited for, unlike a pass's drain: the
// peer may send only 65,535 bytes until it has them, and the first pass finds no drain in flight.
await FlushEgressAsync();

while (!IsBroken)
Expand All @@ -55,6 +56,13 @@ private async Task RunCoreAsync(Action dispatch)
_reactor ??= Reactor.Current; // a RunAsync begun off the reactor learns it here
await new ReactorSwitch(_reactor); // a pipe may complete its read elsewhere: a default Pipe on the thread pool

// A drain the loop read on behind found the connection gone, and ended this read to say so.
if (IsBroken)
{
_pipe.Input.AdvanceTo(read.Buffer.End);
break;
}

_inPass = true;
Feed(read.Buffer);
_pipe.Input.AdvanceTo(read.Buffer.End);
Expand All @@ -66,16 +74,25 @@ private async Task RunCoreAsync(Action dispatch)
// this pass's drain rather than a write of its own.
FireBodyWakes();
_inPass = false;
await FlushEgressAsync();

// Not waited for: an upload behind a stalled download is read on, not left to overflow the receive queue.
_ = FlushEgressAsync(detached: true);

// A stream nghttp2 reset itself closes as the drain sends the RST_STREAM; its reader ends now.
FireBodyWakes();

if (read.IsCompleted || read.IsCanceled || Nghttp2.ih2_is_dead(_handle) != 0)
{
return;
break;
}
}

// What the loop left to a drain still in flight, a GOAWAY among it, goes out before the teardown, if anything still can.
if (_draining && !IsBroken)
{
_drained = new TaskCompletionSource();
await _drained.Task;
}
}
catch (Exception)
{
Expand Down
3 changes: 2 additions & 1 deletion src/protocols/ioxide.nghttp2/Connection/Nghttp2Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ private void Retire(PendingRequest pending)
/// </summary>
public Nghttp2Connection(IDuplexPipe pipe, Nghttp2Options? options = null)
{
Nghttp2.RequireAbi();
_pipe = pipe;
_options = options ?? new Nghttp2Options();
}
Expand Down Expand Up @@ -134,7 +135,7 @@ private unsafe void Setup()
};

_handle = Nghttp2.ih2_server_new(callbacks, (void*)GCHandle.ToIntPtr(_self),
_streamBodies ? 1 : 0);
_streamBodies ? 1 : 0, (uint)_options.MaxConcurrentStreams, (uint)_options.InitialWindowSize);
if (_handle == 0)
{
_failed = true;
Expand Down
21 changes: 21 additions & 0 deletions src/protocols/ioxide.nghttp2/Connection/Nghttp2Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,25 @@ public sealed record Nghttp2Options
/// ENHANCE_YOUR_CALM, never dispatched.
/// </summary>
public int MaxHeaderListSize { get; init; } = 64 * 1024;

/// <summary>
/// Flow-control window advertised per stream, and enforced by nghttp2: how much of one request's
/// body the peer may send ahead of the credit the server gives back. nghttp2 answers a DATA frame
/// past it by ending the connection with FLOW_CONTROL_ERROR, which RFC 9113 6.9 allows as well as
/// a stream error. 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.
///
/// The connection's own window is opened to 2^31-1 right behind the server's SETTINGS, and nghttp2
/// tops it up once half of it is consumed, so what bounds a connection's request bodies in flight
/// is <see cref="MaxConcurrentStreams"/> times this. nghttp2 holds a stream to it once the peer
/// has acknowledged the SETTINGS, and to the RFC's 65,535 until then.
/// </summary>
public int InitialWindowSize { get; init; } = 256 * 1024;

/// <summary>
/// Streams the peer may have open at once, advertised in SETTINGS and enforced by nghttp2. It also
/// bounds a connection's request bodies in flight: this many streams times
/// <see cref="InitialWindowSize"/>, since the connection's window is not the limit.
/// </summary>
public int MaxConcurrentStreams { get; init; } = 100;
}
6 changes: 3 additions & 3 deletions src/protocols/ioxide.nghttp2/Http/Nghttp2BodyReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ namespace ioxide.nghttp2;
/// one window instead of by the whole body, which is the difference that makes a hostile upload
/// harmless.
///
/// As on <c>ioxide.http2</c>, credit is shared. Every stream rides one TCP connection, so a chunk
/// opens both the stream's window and the CONNECTION's - and a handler that never reads holds down
/// the connection window for every other stream on it, until it returns.
/// The window that holds an upload back is its stream's own. A chunk is credited to the
/// connection's window too, but that one is opened to its maximum and topped up as it is consumed,
/// so a handler that never reads holds back only its own upload, never the other streams'.
/// </summary>
/// <remarks>
/// Single consumer, on any thread: a read made off the reactor is served on it. Each
Expand Down
48 changes: 41 additions & 7 deletions src/protocols/ioxide.nghttp2/Interop/Nghttp2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ internal struct Callbacks
public delegate* unmanaged<void*, int, uint, void> OnStreamError;
}

/// <summary>Create a server session and queue SETTINGS. The peer's connection preface is
/// validated out of <see cref="ih2_read"/>. With <paramref name="manualFlow"/> non-zero nghttp2
/// sends no WINDOW_UPDATE for DATA by itself - every byte handed to OnData is credited once,
/// through <see cref="ih2_consume"/> - and OnStreamError reports every close, NO_ERROR included.
/// Without it, OnStreamError reports every close but the normal end of a stream: NO_ERROR once
/// the response's END_STREAM has gone out.
/// <summary>Create a server session and queue SETTINGS, advertising <paramref name="maxConcurrentStreams"/>
/// and <paramref name="initialWindowSize"/>, then the WINDOW_UPDATE that opens the connection's window to
/// 2^31-1. The peer's connection preface is validated out of <see cref="ih2_read"/>. With
/// <paramref name="manualFlow"/> non-zero nghttp2 sends no WINDOW_UPDATE for DATA by itself - every byte
/// handed to OnData is credited once, through <see cref="ih2_consume"/> - and OnStreamError reports every
/// close, NO_ERROR included. Without it, OnStreamError reports every close but the normal end of a stream:
/// NO_ERROR once the response's END_STREAM has gone out.
/// Returns the handle, or 0 on failure.</summary>
[DllImport(Lib)] internal static extern nint ih2_server_new(Callbacks callbacks, void* user, int manualFlow);
[DllImport(Lib)] internal static extern nint ih2_server_new(Callbacks callbacks, void* user, int manualFlow,
uint maxConcurrentStreams, uint initialWindowSize);

/// <summary>Credit DATA the application is done with: the connection window, and the stream's
/// while it is open. Manual flow control only. Returns 0, or a negative nghttp2 error.</summary>
Expand Down Expand Up @@ -89,4 +91,36 @@ internal struct Callbacks

/// <summary>Free the session and any retained request bodies.</summary>
[DllImport(Lib)] internal static extern void ih2_free(nint connection);

/// <summary>The shim's exported-surface revision; checked against <see cref="Abi"/> as a connection is
/// built, so a stale native library fails loudly instead of taking arguments in the wrong places.</summary>
[DllImport(Lib)] private static extern uint ih2_abi();

/// <summary>What this managed binding was written against. Bump both together.</summary>
internal const uint Abi = 1;

/// <summary>
/// Refuse to run against a native library this binding was not built for. The shim and this
/// assembly ship together, so a mismatch means a stale libioxide_nghttp2.so picked up out of a
/// build output.
/// </summary>
internal static void RequireAbi()
{
uint native;
try
{
native = ih2_abi();
}
catch (EntryPointNotFoundException)
{
native = 0; // predates the counter, so certainly too old
}

if (native != Abi)
{
throw new InvalidOperationException(
$"ioxide.nghttp2: the native library reports ABI {native}, this binding needs {Abi}. " +
"A stale libioxide_nghttp2.so is being loaded - rebuild it with scripts/build-nghttp2-native.sh.");
}
}
}
35 changes: 26 additions & 9 deletions src/protocols/ioxide.nghttp2/native/ioxide_nghttp2_shim.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
* conn = ih2_client_new(cbs, user) one per TCP connection (client preface queued)
* ih2_submit_request(hdrs, body) headers packed [u16 nlen][name][u16 vlen][value]*
*
* conn = ih2_server_new(cbs, user, manual) one per accepted connection (server SETTINGS queued)
* conn = ih2_server_new(cbs, user, manual, one per accepted connection (server SETTINGS queued)
* streams, window)
* ih2_submit_response(sid, hdrs, body)
* ih2_consume(sid, len) manual flow control: credit DATA once it is read
*
Expand All @@ -29,6 +30,11 @@

#include <nghttp2/nghttp2.h>

/* Exported-surface revision. Bump on any change to an exported signature or a struct crossing the
* boundary; ih2_abi() hands it to the managed side, which refuses to start on a mismatch.
* 1 - ih2_server_new gained max_concurrent_streams and initial_window_size */
#define IH2_ABI 1

/* ---- callback table into C# ------------------------------------------------------------- */

typedef struct ih2_callbacks {
Expand Down Expand Up @@ -250,7 +256,8 @@ static ssize_t read_body(nghttp2_session *session, int32_t stream_id, uint8_t *b

/* Both directions register the same callbacks and differ only in which nghttp2 constructor runs
* and which SETTINGS go out first, so the wiring lives here once. */
static ih2_conn *ih2_new(ih2_callbacks cbs, void *user, int server, int manual_flow)
static ih2_conn *ih2_new(ih2_callbacks cbs, void *user, int server, int manual_flow,
uint32_t max_concurrent_streams, uint32_t initial_window_size)
{
ih2_conn *c = calloc(1, sizeof(*c));
if (c == NULL) {
Expand Down Expand Up @@ -298,10 +305,12 @@ static ih2_conn *ih2_new(ih2_callbacks cbs, void *user, int server, int manual_f
* ENABLE_PUSH is off in both directions: nothing routes a pushed stream. */
nghttp2_settings_entry settings[] = {
{NGHTTP2_SETTINGS_ENABLE_PUSH, 0},
{NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 1000},
{NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, 1 << 20},
{NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, max_concurrent_streams},
{NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, initial_window_size},
};
if (nghttp2_submit_settings(c->session, NGHTTP2_FLAG_NONE, settings, 3) != 0) {
/* SETTINGS size streams only, so this opens the connection's own window behind them (RFC 9113 6.9.2). */
if (nghttp2_submit_settings(c->session, NGHTTP2_FLAG_NONE, settings, 3) != 0 ||
nghttp2_session_set_local_window_size(c->session, NGHTTP2_FLAG_NONE, 0, NGHTTP2_MAX_WINDOW_SIZE) != 0) {
nghttp2_session_del(c->session);
free(c);
return NULL;
Expand All @@ -312,15 +321,18 @@ static ih2_conn *ih2_new(ih2_callbacks cbs, void *user, int server, int manual_f

/* One per ACCEPTED connection. The peer is expected to have sent (or to be about to send) the
* client connection preface; nghttp2 validates it out of ih2_read. manual_flow non-zero: every
* DATA byte handed to on_data is credited through ih2_consume, once. */
ih2_conn *ih2_server_new(ih2_callbacks cbs, void *user, int manual_flow)
* DATA byte handed to on_data is credited through ih2_consume, once. The connection's receive
* window is opened to 2^31-1, so what the peer may send ahead of the server's credit is
* max_concurrent_streams times initial_window_size. */
ih2_conn *ih2_server_new(ih2_callbacks cbs, void *user, int manual_flow,
uint32_t max_concurrent_streams, uint32_t initial_window_size)
{
return ih2_new(cbs, user, 1, manual_flow);
return ih2_new(cbs, user, 1, manual_flow, max_concurrent_streams, initial_window_size);
}

ih2_conn *ih2_client_new(ih2_callbacks cbs, void *user)
{
return ih2_new(cbs, user, 0, 0);
return ih2_new(cbs, user, 0, 0, 1000, 1 << 20);
}

/* Headers arrive packed as [u16 namelen][name][u16 valuelen][value]... - the same format the
Expand Down Expand Up @@ -700,3 +712,8 @@ const char *ih2_strerror(int error_code)
{
return nghttp2_strerror(error_code);
}

uint32_t ih2_abi(void)
{
return IH2_ABI;
}
Binary file not shown.
Binary file not shown.
46 changes: 46 additions & 0 deletions tests/Ioxide.Tests.Chaos/Nghttp2ChaosTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@ public static void Register(Runner runner)

using var client = new H2cClient(port);
client.Open();

// A chunk flushed while another drain is in flight leaves with that one, so the SETTINGS exchange settles first.
Assert.True(client.AwaitAnyOf([0x4]) == 0x4, "the server never acknowledged the client's SETTINGS");
client.Request(streamId: 1);

(int frames, int bytes, bool ended) = client.DrainBody(streamId: 1);
Expand Down Expand Up @@ -416,5 +419,48 @@ public static void Register(Runner runner)
Assert.True(seen.Contains((1, Cap)),
$"the handler saw {Describe(seen)}, not stream 1 with {Cap} body bytes");
});

runner.Test("nghttp2: a stream window of upload sent while the server's flush is stalled behind a download the client is not reading arrives whole", () =>
{
// A download loopback's socket buffers cannot hold, and an upload in more 4 KiB receives than 64 queue entries hold.
const int Download = 32 * 1024 * 1024, Upload = 256 * 1024;
byte[] big = new byte[Download];
int port = TestServer.Start(async (_, conn) =>
{
try
{
await new Nghttp2Connection(conn).RunAsync(request => request.Path.Span.SequenceEqual("/download"u8)
? new Nghttp2Response { Status = 200, Body = big }
: Nghttp2Response.Text(request.Body.Length.ToString(System.Globalization.CultureInfo.InvariantCulture)));
}
finally
{
conn.DecRef();
}
});

using var client = new H2cClient(port, timeoutMs: 30_000);
client.WriteRaw("PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"u8);
client.WriteFrame(0x4, flags: 0, streamId: 0, [0, 0x4, 0x7F, 0xFF, 0xFF, 0xFF]); // INITIAL_WINDOW_SIZE 2^31-1
client.WriteWindowUpdate(streamId: 0, int.MaxValue - 65535); // so all of the download goes out at once
Assert.True(client.AwaitConnectionWindow(int.MaxValue), "the server did not open its connection window");

client.Request(streamId: 1, path: "/download");
Thread.Sleep(200); // its response staged and its flush stuck, before the upload lands

client.OpenPost(streamId: 3, "/upload");
byte[] frame = new byte[16384];
for (int sent = 0; sent < Upload; sent += frame.Length)
{
client.WriteFrame(0x0, flags: (byte)(sent + frame.Length == Upload ? 0x1 : 0x0), streamId: 3, frame);
}

var bytes = new Dictionary<int, long>();
var ended = new HashSet<int>();
bool done = client.ReadData(bytes, ended, () => ended.Contains(1) && ended.Contains(3), credit: false, timeoutMs: 30_000);
Assert.True(done && bytes.GetValueOrDefault(1) == Download,
$"the download stopped at {bytes.GetValueOrDefault(1)} of {Download} bytes, and the upload's answer {(ended.Contains(3) ? "came" : "never came")}");
Assert.Equal((long)Upload.ToString(System.Globalization.CultureInfo.InvariantCulture).Length, bytes.GetValueOrDefault(3));
});
}
}
Loading
Loading