Skip to content

A streamed HTTP/1 response overtakes a buffered one still in the coalesce tail #211

Closed

Description

@EdmondDantes

#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:

  1. Wait for the tail, then submit. The awaited sender parks until
    out_in_flight is 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_drain today only calls the WebSocket hook.
  2. Append to the tail and return. No new primitive, but the handler stops
    feeling the socket: a streaming loop would fill out_pending_buf at 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

  1. EdmondDantes commented on Aug 22, 2026

    @EdmondDantes
    ContributorAuthor

    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:

    Shape of the fix

    • The wait goes inside http_connection_send_raw and
      http_connection_send_strv_awaited, after the write_timed_out guard and inside
      the write-timer span, so the timeout bounds the tail wait plus the write itself.
    • The condition is !out_in_flight && out_pending_len == 0, rechecked after every
      wake: the wake fires from loop-callback context and another submit can arrive
      before the parked coroutine resumes.
    • The wake is not out_signal_drain. That one is low-water gated and fires
      mid-chain, while out_in_flight is still set. The three places that clear the
      flag are http_send_batched_finish and the two submit-failure branches in
      http_send_batched_completion_cb and h1_batched_drain_pending; each must wake
      the waiter, or a writer parked behind a failed submit hangs until the write
      timeout, which defaults to off.
    • The waiter gets its own event on the connection. on_outbound_drain stays the
      WebSocket's, with its low-water semantics unchanged.
    • TLS needs no branch: every TLS send goes through tls_push before send_raw is
      reached, and the batched senders are plaintext-only, so out_in_flight is never
      set on a TLS connection and the wait condition is never true.
    • A cancellation while parked in the wait releases the owned slots and stops the
      timer before any submit, keeping send_strv_awaited's contract that every
      reference 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, /1 and /2
    buffered, /3 and /4 streamed. S1,W3a,S2,W3b,W4a,W4b on main today, 3 runs
    out of 3 re-checked at 919dab4; S1,S2,W3a,W3b,W4a,W4b after. Controls that must
    not move: the same shape over TLS, a write() returning false on a dead peer, and
    the steady-stream cost in dev/BENCHMARKS.md — a lone streaming handler never
    finds out_in_flight set, so the wait is paid only in the transition.

    Correction to the issue text

    stream_write_buffer_bytes does not default to 0. DEFAULT_STREAM_WRITE_BUFFER_BYTES
    is 262144 (src/http_server_config.c:180), and the comment claiming 0 at
    src/core/http_connection.h:480 is stale. The high-water predicate is still never
    consulted on the HTTP/1 send path, which is what shape 2 would have needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions