Skip to content

nghttp2, nghttp3, http3: a buffered handler that answers null gets a 500 on its own stream instead of ending the connection or going unanswered (#616) - #628

Merged
MDA2AV merged 2 commits into
mainfrom
fix-616
Nov 15, 2026

Conversation

@MDA2AV

@MDA2AV MDA2AV commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Fixes #616.

A buffered handler that answers null made each of these stacks throw a NullReferenceException at its first read of the response (PackHeaders on nghttp2 and nghttp3, resp.HeadIsValid in ioxide.http3's Submit), before anything was submitted. Only nghttp2's parked path caught it, in CompleteAsync. Everywhere else:

  • ioxide.nghttp2, answered synchronously. That is RunAsync(Func<Nghttp2Request, Nghttp2Response>), and either ValueTask overload when the handler's task is already complete, as nearly every one is. The exception left DispatchBuffered's try/finally, and RunCoreAsync's catch took it for a broken peer: it marked the connection failed and freed the session. Requests later in the same read were never dispatched, parked handlers' answers had nowhere to go, nothing the pass had submitted was drained, and the TCP connection closed with no GOAWAY, which RFC 9113 6.8 says should come before a close. stderr heard nothing. .NET's HttpClient saw "The response ended prematurely while waiting for the next frame from the server. (ResponseEnded)" on every request in flight and opened a new connection for the next one.
  • ioxide.nghttp3 and ioxide.http3, answered synchronously or with a completed task. The exception left the dispatch (DispatchBufferedSync and DispatchBufferedAsync's fast path on nghttp3, DispatchReady and DispatchReadyAsync's fast path on http3) and then RunCoreAsync's try/finally, so the task RunAsync returned faulted and the reactor logged "quic handler faulted". The teardown sent no CONNECTION_CLOSE, since nghttp3 sends one only once an error code is recorded and http3 only after a fatal protocol error. So every request on the connection, the null one included, got neither a response nor a reset, and the client could only wait out its idle timeout (RFC 9000 10.1). nghttp3's OnHandlerFault heard nothing.
  • ioxide.nghttp3 and ioxide.http3, answered after an await. CompleteBufferedAsync submits outside its try, so the exception faulted that task, which nothing awaits. The stream got no final response, after which RFC 9114 4.1 has the server close it, and no reset either: the client waited on it until its own timeout. The connection served its other requests.

The fault concerns one response, and HTTP answers it with a 500 (RFC 9110 15.6.1); even a protocol error that affects only one stream is a stream error, not a connection error (RFC 9113 5.4.2, RFC 9114 8).

The change. As #603 does for ioxide.http2: a null answer gets a 500 on its own stream and is reported like a handler that throws, and nothing else thrown while a response is handed over is handled differently.

  • src/protocols/ioxide.nghttp2/Connection/Nghttp2Connection.RunBuffered.cs: DispatchBuffered's synchronous path submits through SubmitAnswer, which tests the answer for null first. A null answer goes to AnswerFault with a NullReferenceException: stderr hears "[ioxide.nghttp2] request handler faulted: ...", and the stream gets a 500 unless the connection is broken. AnswerFault is the body of CompleteAsync's catch, which now calls it, and is NoInlining so the cold path stays out of the dispatch's tier-1 code.
  • src/protocols/ioxide.nghttp3/Connection/Nghttp3Connection.RunBuffered.cs: the three places a buffered answer is queued, DispatchBufferedSync, DispatchBufferedAsync's completed fast path and CompleteBufferedAsync, call QueueAnswer, which tests for null. AnswerNull, out of line, tells OnHandlerFault (or stderr without one) with a NullReferenceException and queues a 500. CompleteBufferedAsync's streamId local only fed the call this replaces, so it goes.
  • src/protocols/ioxide.http3/Http3Connection.cs: the same three places, DispatchReady, DispatchReadyAsync's fast path and CompleteBufferedAsync, call SubmitAnswer; AnswerNull writes the stderr line every other fault writes and submits a 500.

Whatever follows an answer at those places follows the 500 too: the request is retired or recycled, and on nghttp3 an upload still arriving is asked to stop with STOP_SENDING(H3_NO_ERROR). Nothing changes in src/ioxide, in a native shim or in the public API, and no state is added.

What a caller notices.

What to weigh.

  • Tested, not caught. http2: a synchronous buffered handler that answers null gets a 500 on its own stream instead of ending the connection (#592) #603 catches ioxide.http2's NullReferenceException; here each stack tests the answer before it hands it over. The rule is the same, only a null answer and before anything is submitted, but on the native stacks a catch costs every response: on x64 the JIT does not inline a P/Invoke inside a try that has a catch. A first version of this fix caught as http2: a synchronous buffered handler that answers null gets a 500 on its own stream instead of ending the connection (#592) #603 does, and under h2load the tier-1 DispatchBuffered (nghttp2) and DispatchBufferedSync (nghttp3) called ih2_submit_response and ih3_submit_response through their IL stubs, where main inlines them. In a microbenchmark of a blittable P/Invoke (libc labs), a call inside a try with a filtered catch took 4.80 ns, against 1.58 ns inside a try/finally. On ioxide.http3, which has no P/Invoke there, the catch was inlined into DispatchReady and grew its tier-1 code from 1192 to 1404 bytes. With the test, read with DOTNET_JitDisasmSummary under the same load: Nghttp2Connection.DispatchBuffered goes from 1974 to 2019 bytes, Nghttp3Connection.DispatchBufferedSync from 1923 to 1960, Http3Connection.DispatchReady from 1187 to 1218. Both submits stay inlined P/Invokes, and each null path is one call (AnswerFault, AnswerNull). If you would rather all four stacks read alike, the catch is a few lines in place of each helper, at the cost above.
  • nghttp2's parked path still catches, as it always has: there the NullReferenceException is thrown and caught, here it is created. Both report the same line.
  • Throughput of this version: Playground/Http2/Nghttp2Buffered, Playground/Http3/Nghttp3Buffered and Playground/Http3/ManagedBuffered, all three on the synchronous buffered overload, each with 2 reactors on CPUs 2-3 and h2load -t6 -c64 -m32 (h2c, or h3 through an HTTP/3 build of h2load) on six other CPUs, in 10 s runs ordered fix, main, main, fix: 4 rounds on nghttp2, 3 on each h3 stack. Other jobs shared the box (load 1 to 6) and a few runs were plainly disturbed, so these are medians of CPU per request: nghttp2 1.407 us against main's 1.398 (+0.6%), nghttp3 3.988 against 3.992 (-0.1%), http3 1.986 against 1.998 (-0.7%). Each is inside the spread of main's own runs (1.374 to 1.478, 3.919 to 4.738, 1.985 to 2.204). A few ns a response is below what this box can resolve, so the case against the catch rests on the code it compiles to, above.

Tests.

  • Unit, Nghttp2NullAnswerTests, over StrictClient: 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. GET 1 and GET 3 arrive in one read and stream 1 answers null. Stream 1 gets exactly one frame, HEADERS with END_STREAM whose block is :status 500 alone; stream 3 gets "ok"; stderr hears the fault; the connection is still up.
  • E2E, Http2BufferedFaultTests, the nghttp2 twins of http2: a synchronous buffered handler that answers null gets a 500 on its own stream instead of ending the connection (#592) #603's two, over .NET's HttpClient and h2c:
    • h2 fault (nghttp2): a synchronous handler that answers null answers 500, and the connection serves on: /null gets 500 and /ok 200 "ok", on one connection.
    • h2 fault (nghttp2): a request in flight beside a handler that answers null without parking is answered: /slow parks on a RingTimer, then /null answers with a completed null. /null gets 500 and /slow 200 "slow", on one connection.
  • E2E, H3NullAnswerTests, new:
    • Over the recording transport (H3ErrorCodeTests.RecordingQuic), for each stack and each buffered shape (the synchronous overload; the ValueTask overload, completed and awaited; the body-reader overload, completed and awaited), 10 tests: stream 0 answers null and stream 4 "ok", both in one pass. Stream 0 must get a bare 500 with FIN and stream 4 its 200, the run loop must still be running and end cleanly once the transport closes, with no CONNECTION_CLOSE, and the fault must reach nghttp3's OnHandlerFault as a NullReferenceException for stream 0, or http3's stderr once.
    • Over QUIC, with the harness's client, for each stack, synchronous and awaiting a later reactor pass, 4 tests: GET /null gets a bare 500, then GET /ok gets 200 "ok" on the same connection.

Against main's code all 17 new tests fail. The nghttp2 ones with "the connection ended on a null answer" (Unit) and "expected [500: ], got [reset: The response ended prematurely while waiting for the next frame from the server. (ResponseEnded)]" (both E2E); the h3 ones, synchronous or completed, with "the run loop ended on a null answer: Object reference not set to an instance of an object.", and awaited with "expected [500 []], got [nothing]"; and the four over QUIC with "the request whose handler answered null got no answer, not a bare 500".

Seen failing, one part of the fix at a time, on the final code:

  • nghttp2's synchronous path back to SubmitResponse: the Unit test ("the connection ended on a null answer") and both nghttp2 null tests in E2E ("expected [500: ], got [reset: The response ended prematurely while waiting for the next frame from the server. (ResponseEnded)]").
  • AnswerFault without its 500: the Unit test ("stream 1 was sent [], not one HEADERS that ends it"), both null tests in E2E, and the two existing nghttp2 tests of a handler that throws ("The request was canceled due to the configured HttpClient.Timeout of 10 seconds elapsing."). So the helper is covered from both paths.
  • AnswerFault without its stderr line: the Unit test ("stderr had: []").
  • nghttp3, each call site back to QueueResponse in turn. DispatchBufferedSync: the synchronous recorded test ("the run loop ended on a null answer: Object reference not set to an instance of an object.") and the synchronous test over QUIC ("the request whose handler answered null got no answer, not a bare 500"). The fast path: the two completed-task recorded tests, with the same message. CompleteBufferedAsync: the two awaited recorded tests ("expected [500 []], got [nothing]") and the awaited test over QUIC.
  • nghttp3's AnswerNull without the hook call: the five recorded nghttp3 tests ("expected [stream 0: NullReferenceException], got []"). Without the 500: those five ("expected [500 []], got [nothing]") and both nghttp3 tests over QUIC.
  • ioxide.http3, each call site back to Submit in turn: the same tests as on nghttp3, with the same messages. AnswerNull without its stderr line: the five recorded http3 tests ("stderr had: []"). Without the 500: those five and both http3 tests over QUIC.

Suites. Unit 264/0, E2E 746/0, Chaos 105/0 and Http 104/0, nothing skipped, on a pinned Release build of the final commit; that is main's 263 and 730 plus the new tests. Http is in because its cross-stack tests serve through nghttp3. The build adds no warnings: the solution's seven are main's (CA2014, CA2022, CA1416). Tls, File, Pg and Redis weren't run, since none of them reaches these three stacks.

Overlaps.

…500 on its own stream instead of ending the connection or going unanswered (#616)

A buffered handler that answers null made the stack throw a NullReferenceException at its first read of
the response, before anything was submitted. Only nghttp2's parked path caught it.

nghttp2: answered synchronously, as nearly every handler is, it left DispatchBuffered's try/finally,
and the read loop took it for a broken peer. It freed the session without a GOAWAY, losing every other
stream on the connection and what the pass had submitted, and told nothing to stderr.

nghttp3 and http3: answered synchronously or with a completed task, it left the dispatch and the run
loop's try/finally, so the task RunAsync returned faulted. The teardown sent no CONNECTION_CLOSE, since
no error code had been recorded, so the client heard nothing and every request on the connection waited
out its idle timeout. Answered after an await, it faulted the task that finishes the request, which
nothing awaits, outside its catch: that stream got neither a response nor a reset.

Each stack now hands every buffered answer, synchronous, completed or awaited, to one helper that tests
it for null first. A null answer gets what a handler that threw gets: the fault is reported with a
NullReferenceException (OnHandlerFault on nghttp3, else stderr; stderr on nghttp2 and http3) and the
stream gets a 500. nghttp2's parked path keeps its catch, and its body is the helper both paths share.
The test comes before the submit, rather than a catch around it, because a catch stops the JIT
inlining the submit's P/Invoke. Nothing else thrown while a response is handed over is handled
differently.
# Conflicts:
#	src/protocols/ioxide.http3/Http3Connection.cs
#	tests/Ioxide.Tests.E2E/Protocols/Http2BufferedFaultTests.cs
@MDA2AV
MDA2AV merged commit ae9448f into main Nov 15, 2026
2 checks passed
@MDA2AV
MDA2AV deleted the fix-616 branch November 15, 2026 16:42
@MDA2AV MDA2AV mentioned this pull request Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

nghttp2, nghttp3, http3: a synchronous buffered handler that answers null takes down the whole connection, and on h3 an awaited one is never answered

1 participant