Skip to content

fix(h2): refuse a body the server cannot take, and put the frame back after a caught bailout (#226) - #230

Merged
EdmondDantes merged 1 commit into
mainfrom
226-h2-refuse-body-and-restore-frame
Aug 22, 2026
Merged

EdmondDantes merged 1 commit into
mainfrom
226-h2-refuse-body-and-restore-frame

Conversation

@EdmondDantes

Copy link
Copy Markdown
Contributor

Closes #226.

The refusal that reached nobody

setMaxBodySize is documented to end an over-large HTTP/2 body with a stream
reset (src/http_server_config.c, setMaxBodySize), and the check in
cb_on_data_chunk_recv said so by answering
NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE. That callback's contract does not carry
the value: nghttp2.h:1509 gives it NGHTTP2_ERR_PAUSE and treats every other
non-zero answer as a fatal connection error. Neither happened — the refusal was
swallowed, no frame reached the wire, and the handler stayed parked in
awaitBody() with nothing else coming. 32 KiB against a 16 KiB cap produced no
frame at all, three runs of three.

Five refusals were spelled that way: the body cap on the buffered path and on
the streaming one, a queue push that failed, a WebSocket codec error, and the
out-of-memory guard below. Each submits the RST_STREAM itself now and answers
0, the way the admission reject in cb_on_begin_headers already did, so the
sibling streams on the connection live.

The handler learns of it through HttpException: 413 for the cap, 500 for the
rest. A new refused_status on the stream carries the reason and keeps our own
reset out of the peer-reset metric — local_aborted could not be reused,
because it also suppresses the cancel, and the handler still has to be woken.

The frame a caught bailout took with it

_zend_bailout clears EG(current_execute_data) and arms gc_protect before
the longjmp (Zend/zend.c:1281), and zend_end_try gives back EG(bailout)
alone (Zend/zend.h:283). Fifteen firewalls in this server catch a bailout and
then keep running, because one failed stream must not take the connection and
one failed request must not take the worker, and none of them restored either.

The out-of-memory guard around the HTTP/2 body buffer is the one that fires in
practice: 204 catches in a single 200-request run at the default 128 MiB limit,
each an smart_str_alloc that could not get the 1 MiB a declared
Content-Length asked for. It runs on the C stack of whichever coroutine is
driving the reactor tick, and a coroutine woken inside that same tick returns
from its suspend without a context switch, so nothing else puts the frame back.
HttpRequest::awaitBody() and HttpResponse::write() are declared static and
resolve that type through EG(current_execute_data), so the return took
E_CORE_ERROR — "Return value must be of type static, TrueAsync\HttpRequest
returned" — and the request was abandoned mid-response.

src/core/bailout_guard.h snapshots the frame and the collector flag before
zend_try and restores them as the first statement of zend_catch. It is
applied at all fifteen firewalls rather than the one that fires today: the next
zend_try added here would otherwise carry the same defect silently.

Evidence

h2/061 is deterministic and needs no load: four 8 KiB frames against a 16 KiB
cap, with the third passing the cap while the total is still inside the
65535-byte initial window, so what ends the stream is the cap and not flow
control. Against main it reads reset_code=-1 and caught_code=0.

h2/062 drives h2load -n 16 -c 4 -m 4 with 1 MiB bodies at
memory_limit=8M: 12 of 16 requests timed out against main and 16 of 16
finish now, three runs of three. Which four of them answer is the allocator's
call, so the assertion is the part the fix owns — every request reaches an end.

The lost frame is carried without a test. Its window needs about two hundred
concurrent uploads and opens once per run: h2load -n 200 -c 20 -m 4 took one
E_CORE_ERROR per run against main, three runs of three, and none after.
The same run went from wedged past 200 s to 200 of 200 finished, 0 timeouts.

472 phpt, 448 passed, 0 failed, 0 warned, 24 skipped; ctest 16 of 16.

… after a caught bailout (#226)

Two defects, both reachable from one h2load run with 1 MiB bodies.

A refusal in cb_on_data_chunk_recv was spelled as
NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE, which that callback's contract does not
carry: nghttp2.h gives it NGHTTP2_ERR_PAUSE and nothing else. Nothing reached
the wire and nothing reached the handler, so a body past setMaxBodySize left
the stream open until the peer timed out. Five sites answered that way; each
submits the RST_STREAM itself now and answers 0, the way the admission reject
in cb_on_begin_headers already did. The handler learns of it through
HttpException — 413 for the cap, 500 for the rest — and the reset is no longer
counted as one the peer sent.

_zend_bailout clears EG(current_execute_data) and arms gc_protect before the
longjmp, and zend_end_try gives back EG(bailout) alone. Fifteen firewalls here
catch a bailout and keep running, and none restored either. The out-of-memory
guard around the h2 body buffer runs on the C stack of whichever coroutine
drives the reactor tick, and a coroutine woken in that same tick returns from
its suspend without a context switch, so nothing else put the frame back;
awaitBody() and write() are declared `static` and resolve that type through it,
so the return took E_CORE_ERROR and abandoned the request mid-response.

Measured: 32 KiB against a 16 KiB cap put no frame on the wire and now answers
RST_STREAM(ENHANCE_YOUR_CALM); h2load -n 16 -c 4 -m 4 with 1 MiB bodies at
memory_limit=8M timed out 12 of 16 and now finishes 16 of 16, three runs of
three; h2load -n 200 -c 20 -m 4 at the default limit took one E_CORE_ERROR per
run, three runs of three, and none after.

Evidence: h2/061, h2/062. 472 phpt, 448 passed, 0 failed, 0 warned, 24 skipped;
ctest 16 of 16.
@github-actions

Copy link
Copy Markdown
Contributor

Coverage

Total lines: 82.78% → 82.82% (+0.04 pp)

File Baseline Current Δ Touched
src/core/bailout_guard.h 0.00% 100.00% +100.00 pp ●
src/core/body_pool.c 95.45% 95.65% +0.20 pp ●
src/core/http_connection.c 78.82% 78.79% -0.03 pp ●
src/core/http_connection_tls.c 73.46% 73.41% -0.06 pp ●
src/core/worker_dispatch.c 81.53% 81.41% -0.12 pp ●
src/http1/http_parser.c 84.06% 84.05% -0.01 pp ●
src/http2/http2_session.c 86.76% 86.34% -0.42 pp ●
src/http2/http2_strategy.c 84.85% 84.62% -0.23 pp ●
src/http3/http3_callbacks.c 82.42% 83.07% +0.64 pp
src/http3/http3_dispatch.c 85.52% 85.40% -0.13 pp ●
src/http3/http3_listener.c 76.88% 77.33% +0.45 pp
src/http3/http3_packet.c 85.22% 90.43% +5.22 pp
src/http_server_class.c 75.09% 75.04% -0.05 pp ●
src/room/room_hub.c 66.25% 66.25% +0.00 pp ●
src/websocket/ws_dispatch.c 85.85% 85.58% -0.27 pp ●
src/websocket/ws_session.c 90.04% 90.41% +0.37 pp

@EdmondDantes
EdmondDantes merged commit 5228d5c into main Aug 22, 2026
9 checks passed
@EdmondDantes
EdmondDantes deleted the 226-h2-refuse-body-and-restore-frame branch August 22, 2026 17:15
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.

A suspended write() fails its own 'static' return-type check under bidirectional HTTP/2 load

1 participant