Repository navigation
fix(h2): refuse a body the server cannot take, and put the frame back after a caught bailout (#226) - #230
Merged
Conversation
… 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.
Contributor
CoverageTotal lines: 82.78% → 82.82% (+0.04 pp)
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #226.
The refusal that reached nobody
setMaxBodySizeis documented to end an over-large HTTP/2 body with a streamreset (
src/http_server_config.c,setMaxBodySize), and the check incb_on_data_chunk_recvsaid so by answeringNGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE. That callback's contract does not carrythe value:
nghttp2.h:1509gives itNGHTTP2_ERR_PAUSEand treats every othernon-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 noframe 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_STREAMitself now and answers0, the way the admission reject in
cb_on_begin_headersalready did, so thesibling streams on the connection live.
The handler learns of it through
HttpException: 413 for the cap, 500 for therest. A new
refused_statuson the stream carries the reason and keeps our ownreset out of the peer-reset metric —
local_abortedcould 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_bailoutclearsEG(current_execute_data)and armsgc_protectbeforethe longjmp (
Zend/zend.c:1281), andzend_end_trygives backEG(bailout)alone (
Zend/zend.h:283). Fifteen firewalls in this server catch a bailout andthen 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_allocthat could not get the 1 MiB a declaredContent-Lengthasked for. It runs on the C stack of whichever coroutine isdriving 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()andHttpResponse::write()are declaredstaticandresolve that type through
EG(current_execute_data), so the return tookE_CORE_ERROR— "Return value must be of type static, TrueAsync\HttpRequestreturned" — and the request was abandoned mid-response.
src/core/bailout_guard.hsnapshots the frame and the collector flag beforezend_tryand restores them as the first statement ofzend_catch. It isapplied at all fifteen firewalls rather than the one that fires today: the next
zend_tryadded here would otherwise carry the same defect silently.Evidence
h2/061is deterministic and needs no load: four 8 KiB frames against a 16 KiBcap, 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
mainit readsreset_code=-1andcaught_code=0.h2/062drivesh2load -n 16 -c 4 -m 4with 1 MiB bodies atmemory_limit=8M: 12 of 16 requests timed out againstmainand 16 of 16finish 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 4took oneE_CORE_ERRORper run againstmain, 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;
ctest16 of 16.