diff --git a/src/protocols/ioxide.http3/Http3Connection.cs b/src/protocols/ioxide.http3/Http3Connection.cs index 09d029a3..15be819a 100644 --- a/src/protocols/ioxide.http3/Http3Connection.cs +++ b/src/protocols/ioxide.http3/Http3Connection.cs @@ -685,7 +685,7 @@ private void DispatchReady(Func handler) resp = new Http3Response { Status = 500 }; } - Submit(rs.Request.StreamId, resp); + SubmitAnswer(rs.Request.StreamId, resp); Recycle(rs); } _ready.Clear(); @@ -726,7 +726,7 @@ private void DispatchReadyAsync(Func> han { RetireBody(rs); Http3Response response = pending.Result; // once: a ValueTask is consumed by reading it - Submit(sid, response); + SubmitAnswer(sid, response); StopReadingIfAnsweredEarly(rs); Recycle(rs); } @@ -757,11 +757,30 @@ private async Task CompleteBufferedAsync(ValueTask pending, ReqSt { return; } - Submit(rs.Request.StreamId, resp); + SubmitAnswer(rs.Request.StreamId, resp); StopReadingIfAnsweredEarly(rs); Recycle(rs); } + // A null answer has sent nothing, so it gets what a handler that threw gets. + private void SubmitAnswer(long streamId, Http3Response resp) + { + if (resp is null) + { + AnswerNull(streamId); + return; + } + Submit(streamId, resp); + } + + // Out of line, as only a handler that answered null comes here. + [MethodImpl(MethodImplOptions.NoInlining)] + private void AnswerNull(long streamId) + { + Console.Error.WriteLine($"[ioxide.http3] request handler faulted: {new NullReferenceException().Message}"); + Submit(streamId, new Http3Response { Status = 500 }); + } + /// Back for the next stream once the handler is done and the response submitted. private void Recycle(ReqStream rs) { diff --git a/src/protocols/ioxide.nghttp2/Connection/Nghttp2Connection.RunBuffered.cs b/src/protocols/ioxide.nghttp2/Connection/Nghttp2Connection.RunBuffered.cs index 403abf10..94dfd146 100644 --- a/src/protocols/ioxide.nghttp2/Connection/Nghttp2Connection.RunBuffered.cs +++ b/src/protocols/ioxide.nghttp2/Connection/Nghttp2Connection.RunBuffered.cs @@ -1,5 +1,6 @@ using System.Buffers; using System.IO.Pipelines; +using System.Runtime.CompilerServices; namespace ioxide.nghttp2; @@ -174,7 +175,7 @@ private void DispatchBuffered(Func> h { try { - SubmitResponse(pending.StreamId, inFlight.Result); + SubmitAnswer(pending.StreamId, inFlight.Result); } finally { @@ -210,13 +211,7 @@ private async Task CompleteAsync(ValueTask inFlight, PendingReq await new ReactorSwitch(_reactor); // Nobody can observe this task, so an escaping exception would vanish silently and the // peer would wait on a stream that is never coming. - Console.Error.WriteLine( - $"[ioxide.nghttp2] request handler faulted: {exception.GetBaseException().Message}"); - - if (!IsBroken) - { - SubmitResponse(pending.StreamId, new Nghttp2Response { Status = 500 }); - } + AnswerFault(pending.StreamId, exception); } finally { @@ -224,4 +219,29 @@ private async Task CompleteAsync(ValueTask inFlight, PendingReq await FlushEgressAsync(); } } + + // A null answer has submitted nothing, so it gets what a handler that threw gets. Tested, not caught: a catch + // around the submit stops the JIT inlining its P/Invoke. + private void SubmitAnswer(int streamId, Nghttp2Response response) + { + if (response is null) + { + AnswerFault(streamId, new NullReferenceException()); + return; + } + SubmitResponse(streamId, response); + } + + // A handler that threw, or answered null, gets a 500 on its own stream. Out of line, as only a fault comes here. + [MethodImpl(MethodImplOptions.NoInlining)] + private void AnswerFault(int streamId, Exception exception) + { + Console.Error.WriteLine( + $"[ioxide.nghttp2] request handler faulted: {exception.GetBaseException().Message}"); + + if (!IsBroken) + { + SubmitResponse(streamId, new Nghttp2Response { Status = 500 }); + } + } } diff --git a/src/protocols/ioxide.nghttp3/Connection/Nghttp3Connection.RunBuffered.cs b/src/protocols/ioxide.nghttp3/Connection/Nghttp3Connection.RunBuffered.cs index 2da49b46..c5d94e2e 100644 --- a/src/protocols/ioxide.nghttp3/Connection/Nghttp3Connection.RunBuffered.cs +++ b/src/protocols/ioxide.nghttp3/Connection/Nghttp3Connection.RunBuffered.cs @@ -1,3 +1,5 @@ +using System.Runtime.CompilerServices; + namespace ioxide.nghttp3; /// @@ -78,7 +80,7 @@ private void DispatchBufferedSync(Func handler) response = new Nghttp3Response { Status = 500 }; } - QueueResponse(request.StreamId, response); + QueueAnswer(request, response); ReleaseRequest(request); // sync handler returned - the contract just ended } _readyStreamIds.Clear(); @@ -113,7 +115,7 @@ private void DispatchBufferedAsync(Func pending, Ngh } await new ReactorSwitch(Reactor); // back on the reactor, wherever the handler finished - long streamId = request.StreamId; request.HandlerDone = true; RetireBody(request); if (_nghttp3Handle != 0 && !_protocolFailed) { - QueueResponse(streamId, response); + QueueAnswer(request, response); PumpEgress(); } StopReadingIfAnsweredEarly(request); @@ -158,4 +159,24 @@ private async Task CompleteBufferedAsync(ValueTask pending, Ngh ReleaseRequest(request); } } + + // A null answer has submitted nothing, so it gets what a handler that threw gets. Tested, not caught: a catch + // around the submit stops the JIT inlining its P/Invoke. + private void QueueAnswer(Nghttp3Request request, Nghttp3Response response) + { + if (response is null) + { + AnswerNull(request); + return; + } + QueueResponse(request.StreamId, response); + } + + // Out of line, as only a handler that answered null comes here. + [MethodImpl(MethodImplOptions.NoInlining)] + private void AnswerNull(Nghttp3Request request) + { + ReportHandlerFault(request, new NullReferenceException()); + QueueResponse(request.StreamId, new Nghttp3Response { Status = 500 }); + } } diff --git a/tests/Ioxide.Tests.E2E/Program.cs b/tests/Ioxide.Tests.E2E/Program.cs index e14961d1..fae2ded9 100644 --- a/tests/Ioxide.Tests.E2E/Program.cs +++ b/tests/Ioxide.Tests.E2E/Program.cs @@ -82,6 +82,7 @@ private static int Main() Http3RequestReuseTests.Register(runner); H3BodyModeTests.Register(runner); H3BufferedFaultTests.Register(runner); + H3NullAnswerTests.Register(runner); StreamResetTests.Register(runner); HandlerFaultHookTests.Register(runner); H3BodyLimitTests.Register(runner); diff --git a/tests/Ioxide.Tests.E2E/Protocols/H3NullAnswerTests.cs b/tests/Ioxide.Tests.E2E/Protocols/H3NullAnswerTests.cs new file mode 100644 index 00000000..3677531d --- /dev/null +++ b/tests/Ioxide.Tests.E2E/Protocols/H3NullAnswerTests.cs @@ -0,0 +1,236 @@ +using ioxide; +using ioxide.http3; +using ioxide.nghttp3; +using ioxide.ngtcp2; +using static Ioxide.Tests.H3ErrorCodeTests; + +namespace Ioxide.Tests; + +/// +/// A whole-response HTTP/3 handler that answers null, on both stacks and from every buffered overload. A null answer +/// cannot be sent, so its request is answered as one whose handler threw: a bare 500 on its own stream, the fault +/// reported, and the connection's other requests served. Checked on a transport that records what the stack sends, +/// where two requests share one pass, and over QUIC. +/// +internal static class H3NullAnswerTests +{ + // A GET framed by hand: static :method GET, :scheme https and :path /, and a literal :authority, which nghttp3 requires. + private static readonly byte[] Get = [0x01, 0x10, 0x00, 0x00, 0xD1, 0xD7, 0xC1, 0x50, 0x09, .. "localhost"u8]; + + private enum Shape { Synchronous, Completed, Awaited, ReaderCompleted, ReaderAwaited } + + public static void Register(Runner runner) + { + foreach (Shape shape in Enum.GetValues()) + { + Shape s = shape; + + runner.Test($"h3 null answer (nghttp3, {Name(s)}): the request is answered 500, the hook hears the fault, and the next request in the pass is served", () => + { + var faults = new List(); + var options = new Nghttp3Options { OnHandlerFault = (request, fault) => faults.Add($"stream {request.StreamId}: {fault.GetType().Name}") }; + AssertAnsweredInPass((quic, gate) => ServeNghttp3(new Nghttp3Connection(quic, options), s, gate)); + Assert.Equal("stream 0: NullReferenceException", string.Join(", ", faults)); + }); + + runner.Test($"h3 null answer (ioxide.http3, {Name(s)}): the request is answered 500, stderr hears the fault, and the next request in the pass is served", () => + { + var stderr = new StringWriter(); + TextWriter original = Console.Error; + Console.SetError(stderr); + try + { + AssertAnsweredInPass((quic, gate) => ServeHttp3(new Http3Connection(quic), s, gate)); + } + finally + { + Console.SetError(original); + } + + int reported = stderr.ToString().Split('\n').Count(line => line.StartsWith("[ioxide.http3] request handler faulted")); + Assert.True(reported == 1, $"stderr had: [{stderr}]"); + }); + } + + foreach (string stack in new[] { "nghttp3", "ioxide.http3" }) + { + foreach (Shape shape in new[] { Shape.Synchronous, Shape.Awaited }) + { + string at = stack; + Shape s = shape; + runner.Test($"h3 null answer ({at}, {Name(s)}, over QUIC): the request is answered 500, and the connection serves on", () => + { + using H3TestClient client = Connect(at, s); + + (int status, string body) = client.Get("/null", timeoutMs: 5000); + Assert.True(status == 500 && body == "", $"the request whose handler answered null got {Describe(status, body)}, not a bare 500"); + + (status, body) = client.Get("/ok", timeoutMs: 5000); + Assert.True(status == 200 && body == "ok", + $"the next request on the connection got {Describe(status, body)}" + (client.PeerClosed ? ": the server closed the connection" : "")); + }); + } + } + } + + private static string Name(Shape shape) => shape switch + { + Shape.Synchronous => "synchronous", + Shape.Completed => "completed task", + Shape.Awaited => "awaited", + Shape.ReaderCompleted => "body reader, completed task", + _ => "body reader, awaited", + }; + + // Stream 0 answers null, once the gate opens when its shape awaits; stream 4 answers "ok" at once. Both arrive in one pass. + private static void AssertAnsweredInPass(Func serve) + { + var quic = new RecordingQuic(); + Assert.True(quic.EnqueueStreamData(0, Get, fin: true) && quic.EnqueueStreamData(4, Get, fin: true), "the recv ring refused a request"); + + var gate = new TaskCompletionSource(); + Task run = serve(quic, gate.Task); + Assert.True(!run.IsCompleted, $"the run loop ended on a null answer: {Why(run)}"); + + gate.SetResult(); + quic.MarkClosed(); + Assert.True(Task.WaitAny([run], 5000) == 0 && run.IsCompletedSuccessfully, $"the run loop did not end cleanly once the transport closed: {Why(run)}"); + + Assert.Equal("500 []", Answer(quic, 0)); + Assert.Equal("200 [ok]", Answer(quic, 4)); + Assert.True(quic.ClosedWith is null, $"the connection was closed with 0x{quic.ClosedWith:x}"); + } + + private static Task ServeNghttp3(Nghttp3Connection connection, Shape shape, Task gate) + { + static Nghttp3Response? Respond(Nghttp3Request request) => request.StreamId == 0 ? null : Nghttp3Response.Text("ok"); + + async ValueTask Later(Nghttp3Request request) + { + if (request.StreamId == 0) + { + await gate; + } + return Respond(request)!; + } + + return shape switch + { + Shape.Synchronous => connection.RunAsync(request => Respond(request)!), + Shape.Completed => connection.RunAsync(request => new ValueTask(Respond(request)!)), + Shape.Awaited => connection.RunAsync(Later), + Shape.ReaderCompleted => connection.RunAsync((Nghttp3Request request, Nghttp3BodyReader _) => new ValueTask(Respond(request)!)), + _ => connection.RunAsync((Nghttp3Request request, Nghttp3BodyReader _) => Later(request)), + }; + } + + private static Task ServeHttp3(Http3Connection connection, Shape shape, Task gate) + { + static Http3Response? Respond(Http3Request request) => request.StreamId == 0 ? null : Http3Response.Text("ok"); + + async ValueTask Later(Http3Request request) + { + if (request.StreamId == 0) + { + await gate; + } + return Respond(request)!; + } + + return shape switch + { + Shape.Synchronous => connection.RunAsync(request => Respond(request)!), + Shape.Completed => connection.RunAsync(request => new ValueTask(Respond(request)!)), + Shape.Awaited => connection.RunAsync(Later), + Shape.ReaderCompleted => connection.RunAsync((Http3Request request, Http3BodyReader _) => new ValueTask(Respond(request)!)), + _ => connection.RunAsync((Http3Request request, Http3BodyReader _) => Later(request)), + }; + } + + // "/null" answers null, after a later reactor pass when the shape awaits; anything else answers "ok". + private static H3TestClient Connect(string stack, Shape shape) + { + (string certPath, string keyPath) = TestCert.Ensure(); + var engine = TestServer.Own(new QuicEngine(certPath, keyPath, cidLength: 8, alpn: ["h3"])); + + (_, int udpPort) = TestServer.StartDatagram( + onDatagram: null, + quicFactory: engine.CreateFactory(), + quicHandle: (_, conn) => (stack, shape) switch + { + ("nghttp3", Shape.Synchronous) => new Nghttp3Connection(conn).RunAsync(request => IsNull(request.Path) ? null! : Nghttp3Response.Text("ok")), + ("nghttp3", _) => new Nghttp3Connection(conn).RunAsync(async request => + { + await TestServer.LaterPass(); + return IsNull(request.Path) ? null! : Nghttp3Response.Text("ok"); + }), + (_, Shape.Synchronous) => new Http3Connection(conn).RunAsync(request => IsNull(request.Path) ? null! : Http3Response.Text("ok")), + _ => new Http3Connection(conn).RunAsync(async request => + { + await TestServer.LaterPass(); + return IsNull(request.Path) ? null! : Http3Response.Text("ok"); + }), + }); + + var client = new H3TestClient("127.0.0.1", udpPort); + client.Connect(); + Assert.True(client.CompleteHandshake(timeoutMs: 5000), "handshake did not complete"); + return client; + + static bool IsNull(ReadOnlyMemory path) => path.Span.SequenceEqual("/null"u8); + } + + // "status [body]" from what the stack sent on one stream, read off its HEADERS and DATA frames; "nothing" when it sent nothing. + private static string Answer(RecordingQuic quic, long streamId) + { + List<(long StreamId, byte[] Bytes, bool Fin)> sent = [.. quic.Sent.Where(s => s.StreamId == streamId)]; + if (sent.Count == 0) + { + return "nothing"; + } + + byte[] stream = [.. sent.SelectMany(s => s.Bytes)]; + byte[]? fields = null; + var body = new List(); + for (int at = 0; at < stream.Length;) + { + long type = ReadVarint(stream, ref at); + int length = (int)ReadVarint(stream, ref at); + if (type == 0x01) + { + fields ??= stream[at..(at + length)]; + } + else if (type == 0x00) + { + body.AddRange(stream[at..(at + length)]); + } + at += length; + } + + // :status follows the field section's two-byte prefix: static index 25 is 200, 71 is 500. + string status = fields switch + { + null => "no HEADERS", + [_, _, 0xD9, ..] => "200", + [_, _, 0xFF, 0x08, ..] => "500", + _ => Convert.ToHexString(fields), + }; + return $"{status} [{System.Text.Encoding.ASCII.GetString(body.ToArray())}]" + (sent[^1].Fin ? "" : ", not ended"); + } + + private static long ReadVarint(byte[] bytes, ref int at) + { + int length = 1 << (bytes[at] >> 6); + long value = bytes[at] & 0x3F; + for (int i = 1; i < length; i++) + { + value = (value << 8) | bytes[at + i]; + } + at += length; + return value; + } + + private static string Why(Task run) => run.Exception?.GetBaseException().Message ?? run.Status.ToString(); + + private static string Describe(int status, string body) => status < 0 ? "no answer" : $"{status} [{body}]"; +} diff --git a/tests/Ioxide.Tests.E2E/Protocols/Http2BufferedFaultTests.cs b/tests/Ioxide.Tests.E2E/Protocols/Http2BufferedFaultTests.cs index 2eaa65d5..63dace7d 100644 --- a/tests/Ioxide.Tests.E2E/Protocols/Http2BufferedFaultTests.cs +++ b/tests/Ioxide.Tests.E2E/Protocols/Http2BufferedFaultTests.cs @@ -8,8 +8,8 @@ namespace Ioxide.Tests; /// /// A whole-response HTTP/2 handler that throws, on both stacks. Throwing from its task or before it /// has one - as every synchronous handler does - answers 500 on its own stream, and every other -/// stream on the connection carries on. On ioxide.http2 so does a handler that answers null, which -/// cannot be written. +/// stream on the connection carries on. So does a handler that answers null, which cannot be +/// written. /// internal static class Http2BufferedFaultTests { @@ -44,32 +44,32 @@ public static void Register(Runner runner) Assert.Equal("200: slow", Describe(slow)); Assert.True(Volatile.Read(ref server.Connections) == 1, WholeConnection(server)); }); - } - runner.Test("h2 fault (ioxide.http2): a synchronous handler that answers null answers 500, and the connection serves on", () => - { - var server = new FaultServer(); - int port = Start(native: false, server, synchronous: true); - using HttpClient client = H2Client(); + runner.Test($"h2 fault ({stack}): a synchronous handler that answers null answers 500, and the connection serves on", () => + { + var server = new FaultServer(); + int port = Start(ng, server, synchronous: true); + using HttpClient client = H2Client(); - Assert.Equal("500: ", Fetch(client, port, "/null")); - Assert.Equal("200: ok", Fetch(client, port, "/ok")); - Assert.True(Volatile.Read(ref server.Connections) == 1, WholeConnection(server)); - }); + Assert.Equal("500: ", Fetch(client, port, "/null")); + Assert.Equal("200: ok", Fetch(client, port, "/ok")); + Assert.True(Volatile.Read(ref server.Connections) == 1, WholeConnection(server)); + }); - runner.Test("h2 fault (ioxide.http2): a request in flight beside a handler that answers null without parking is answered", () => - { - var server = new FaultServer(); - int port = Start(native: false, server, synchronous: false); - using HttpClient client = H2Client(); + runner.Test($"h2 fault ({stack}): a request in flight beside a handler that answers null without parking is answered", () => + { + var server = new FaultServer(); + int port = Start(ng, server, synchronous: false); + using HttpClient client = H2Client(); - Task slow = client.GetAsync($"http://127.0.0.1:{port}/slow"); - Assert.True(server.SlowArrived.Task.Wait(5_000), "/slow never reached its handler"); + Task slow = client.GetAsync($"http://127.0.0.1:{port}/slow"); + Assert.True(server.SlowArrived.Task.Wait(5_000), "/slow never reached its handler"); - Assert.Equal("500: ", Fetch(client, port, "/null")); - Assert.Equal("200: slow", Describe(slow)); - Assert.True(Volatile.Read(ref server.Connections) == 1, WholeConnection(server)); - }); + Assert.Equal("500: ", Fetch(client, port, "/null")); + Assert.Equal("200: slow", Describe(slow)); + Assert.True(Volatile.Read(ref server.Connections) == 1, WholeConnection(server)); + }); + } } private static string WholeConnection(FaultServer server) @@ -82,7 +82,7 @@ private sealed class FaultServer public bool BoomSeen; } - // "/boom" throws before the handler returns, on ioxide.http2 "/null" answers null without parking, + // "/boom" throws before the handler returns, "/null" answers null without parking, // "/slow" parks until either has been dispatched, anything else answers "ok". Synchronous serves // through the synchronous overload; otherwise through the asynchronous one, with a lambda that // is not async and so throws, or answers, before its task. @@ -99,10 +99,10 @@ private static int Start(bool native, FaultServer server, bool synchronous) await (synchronous ? connection.RunAsync(request => Answer(request.Path) is { } body ? new Nghttp2Response { Status = 200, Body = body } - : throw new InvalidOperationException("the handler failed")) + : IsNull(request.Path) ? null! : throw new InvalidOperationException("the handler failed")) : connection.RunAsync(request => Answer(request.Path) is { } body ? Park(timer, server, body, b => new Nghttp2Response { Status = 200, Body = b }) - : throw new InvalidOperationException("the handler failed"))); + : IsNull(request.Path) ? new ValueTask((Nghttp2Response)null!) : throw new InvalidOperationException("the handler failed"))); } else { diff --git a/tests/Ioxide.Tests.Unit/Nghttp2NullAnswerTests.cs b/tests/Ioxide.Tests.Unit/Nghttp2NullAnswerTests.cs new file mode 100644 index 00000000..377ecef9 --- /dev/null +++ b/tests/Ioxide.Tests.Unit/Nghttp2NullAnswerTests.cs @@ -0,0 +1,51 @@ +using System.Text; +using ioxide.nghttp2; + +namespace Ioxide.Tests; + +/// A buffered handler that answers null is answered as one that throws: a bare 500 on its own stream, and the connection serves on. +internal static class Nghttp2NullAnswerTests +{ + public static void Register(Runner runner) + { + runner.Test("nghttp2: a synchronous handler that answers null is answered 500 on its own stream, the fault reaches stderr, and the next stream in the same read is served", () => + { + using var client = new StrictClient(); + var connection = new Nghttp2Connection(client); + var stderr = new StringWriter(); + TextWriter original = Console.Error; + Console.SetError(stderr); + try + { + Task run = connection.RunAsync(request => request.StreamId == 1 ? null! : Nghttp2Response.Text("ok")); + + client.SendRequests(); // preface and SETTINGS + client.Drain(); + client.Feed([.. Get(1), .. Get(3)]); + List frames = client.Drain(); + + Assert.True(!run.IsCompleted, "the connection ended on a null answer"); + List stream = frames.Where(f => f.StreamId == 1).ToList(); + Assert.True(stream is [{ Type: Frame.Headers, EndStream: true }], + $"stream 1 was sent [{string.Join(", ", stream.Select(f => $"type {f.Type} flags 0x{f.Flags:x}"))}], not one HEADERS that ends it"); + Assert.True(stream[0].Payload is [0x8E], $"stream 1's header block was [{Convert.ToHexString(stream[0].Payload)}], not :status 500 alone"); + Assert.Equal("ok", Encoding.UTF8.GetString(Frame.Body(frames, 3))); + Assert.True(stderr.ToString().Contains("[ioxide.nghttp2] request handler faulted"), $"stderr had: [{stderr}]"); + + client.Close(run); + } + finally + { + Console.SetError(original); + connection.Dispose(); + } + }); + } + + // A GET with END_STREAM: :scheme http, :path /, and an :authority, which nghttp2 requires. + private static byte[] Get(int streamId) + { + byte[] block = [0x82, 0x86, 0x84, 0x01, 9, .. "a.example"u8.ToArray()]; + return Http2FlowControlTests.FrameBytes(0x1, 0x5, streamId, block); + } +} diff --git a/tests/Ioxide.Tests.Unit/Program.cs b/tests/Ioxide.Tests.Unit/Program.cs index 6aef174a..715baeba 100644 --- a/tests/Ioxide.Tests.Unit/Program.cs +++ b/tests/Ioxide.Tests.Unit/Program.cs @@ -34,6 +34,7 @@ private static int Main() Http2HeaderListTests.Register(runner); Nghttp2HeaderListTests.Register(runner); Nghttp2OversizedResponseTests.Register(runner); + Nghttp2NullAnswerTests.Register(runner); Http2RequestHeaderTests.Register(runner); Http2RequestReuseTests.Register(runner); Nghttp2RequestReuseTests.Register(runner);