Repository navigation
Description
#209 fixed the two fire-and-forget senders that wrote past the coalesce tail. The
awaited sender does the same, and it takes two small responses ahead of the
stream to show it — which is why the reproduction there did not catch it.
Reproduction
Four pipelined requests on one plaintext connection: /1 and /2 answered with a
2-byte buffered body, /3 and /4 streamed.
$server->addHttpHandler(function ($req, $res) {
$n = (int) ltrim($req->getPath(), '/');
if ($n >= 3) { $res->write("W{$n}a"); $res->end("W{$n}b"); return; }
$res->setBody("S$n")->end();
});Markers read off the socket, five runs out of five:
marks: S1,W3a,S2,W3b,W4a,W4b
Expected S1,S2,W3a,W3b,W4a,W4b. The stream's first chunk lands between the two
buffered responses.
Cause
Response 1 goes in flight through http_connection_send_zstr_batched; response 2
is appended to conn->out_pending_buf behind it. The stream's first chunk then
takes http_connection_send_strv_awaited / http_connection_send_raw
(src/core/http_connection.c:1348), which submits ZEND_ASYNC_IO_WRITE at
conn->io without reading out_in_flight. libuv writes it as soon as response 1
completes; the tail carrying response 2 follows after.
A pipelined client matches responses to requests by order (RFC 9112 §9.3.1), so it
reads the stream's first chunk as the tail of response 1 and response 2 as part of
the stream.
Why the fix is not the same as #209's
The two senders #209 moved were fire-and-forget: appending them to the tail costs
nothing, because no caller was waiting for their completion. This one awaits, and
the await is HTTP/1's entire backpressure story — the handler parks until the
socket takes the bytes, which is what keeps a fast producer from growing an
unbounded buffer.
Two shapes, and they differ in what a handler feels:
- Wait for the tail, then submit. The awaited sender parks until
out_in_flightis false and the tail is empty, then writes as it does today.
Ordering restored, backpressure unchanged, and it needs a drain the coroutine
can wait on —out_signal_draintoday only calls the WebSocket hook. - Append to the tail and return. No new primitive, but the handler stops
feeling the socket: a streaming loop would fillout_pending_bufat memory
speed, and HTTP/1 has no high-water refusal to stop it.
Scope: plaintext HTTP/1. TLS goes through the BIO ring, HTTP/2 and HTTP/3 order
their own streams.
Evidence to write
A phpt pipelining two buffered responses ahead of two streamed ones and asserting
the markers come back in request order.
Activity
EdmondDantes commentedon Aug 22, 2026
Decision: shape 1 — the awaited sender waits for the tail, then writes as today
Shape 2 (append to the tail and return) is refused on three counts, all of them
things HTTP/1 answers with today:
h1_stream_append_chunktakesstream_deadfrom the send's return value(
src/http1/http1_stream.c:325). An append always succeeds, so a peer that leftstops being visible and
isWritable()keeps answering true for the rest of theresponse (Add isWritable() so a handler can tell a slow client from a gone one (#175) #176).
http_write_timer_arm/http_write_timer_stopbracket the await(
src/core/http_connection.c:1364). An append has nothing to bracket, sowrite_timeout_msstops covering the streamed path.out_pending_bufon every chunk — the copy Send a streamed HTTP/1 chunk as one write below 32 KiB (#179) #184removed by handing owned slots to the reactor.
Shape of the fix
http_connection_send_rawandhttp_connection_send_strv_awaited, after thewrite_timed_outguard and insidethe write-timer span, so the timeout bounds the tail wait plus the write itself.
!out_in_flight && out_pending_len == 0, rechecked after everywake: the wake fires from loop-callback context and another submit can arrive
before the parked coroutine resumes.
out_signal_drain. That one is low-water gated and firesmid-chain, while
out_in_flightis still set. The three places that clear theflag are
http_send_batched_finishand the two submit-failure branches inhttp_send_batched_completion_cbandh1_batched_drain_pending; each must wakethe waiter, or a writer parked behind a failed submit hangs until the write
timeout, which defaults to off.
on_outbound_drainstays theWebSocket's, with its low-water semantics unchanged.
tls_pushbeforesend_rawisreached, and the batched senders are plaintext-only, so
out_in_flightis neverset on a TLS connection and the wait condition is never true.
timer before any submit, keeping
send_strv_awaited's contract that everyreference is consumed in every outcome (The reactor owns the HTTP/1 frame, so a cancelled write cannot outlive it (#179) #186).
Evidence
The reproduction in this issue, as a phpt: four pipelined requests,
/1and/2buffered,
/3and/4streamed.S1,W3a,S2,W3b,W4a,W4bonmaintoday, 3 runsout of 3 re-checked at 919dab4;
S1,S2,W3a,W3b,W4a,W4bafter. Controls that mustnot move: the same shape over TLS, a
write()returning false on a dead peer, andthe steady-stream cost in
dev/BENCHMARKS.md— a lone streaming handler neverfinds
out_in_flightset, so the wait is paid only in the transition.Correction to the issue text
stream_write_buffer_bytesdoes not default to 0.DEFAULT_STREAM_WRITE_BUFFER_BYTESis 262144 (
src/http_server_config.c:180), and the comment claiming 0 atsrc/core/http_connection.h:480is stale. The high-water predicate is still neverconsulted on the HTTP/1 send path, which is what shape 2 would have needed.
fix(http1): park the awaited sender until the coalesce tail is empty (#…
fix(http1): park the awaited sender until the coalesce tail is empty (#…
Merge pull request #212 from true-async/211-awaited-sender-waits-for-…