Skip to content

Pipelined HTTP/1 responses come back out of order when a large one follows a small one #209

Closed

Description

@EdmondDantes

Three pipelined requests on one plaintext HTTP/1 connection come back in the
order 1, 3, 2. A pipelined client matches responses to requests by order
(RFC 9112 §9.3.1), so it reads response 3's body as the answer to request 2 and
response 2's as the answer to request 3.

Reproduction

Handler answers /1 and /2 with a 2-byte body and /3 with 1800 bytes. All
three requests are written in one fwrite, so they land in the read buffer
together.

$server->addHttpHandler(function ($req, $res) {
    $n = (int) ltrim($req->getPath(), '/');
    $res->setBody($n >= 3 ? str_repeat("L$n", 900) : "S$n")->end();
});
GET /1 HTTP/1.1 … GET /2 HTTP/1.1 … GET /3 HTTP/1.1 (Connection: close)

Bodies read back off the socket, three runs out of three:

order: S1,L3,S2

Cause

The HTTP/1 dispose picks its sender by body size
(src/core/http_connection.c:2965):

  • under HTTP_WRITEV_THRESHOLD (1024) — http_connection_send_zstr_batched,
    which honours the connection's write ordering: one uv_write in flight at a
    time, everything behind it appended to conn->out_pending_buf, drained by the
    completion callback;
  • at or over it — http_connection_send_strv_owned, which submits
    ZEND_ASYNC_IO_WRITEV straight at conn->io (:1946) without reading
    out_in_flight and without setting it.

So response 1 goes in flight, response 2 is appended to the pending tail, and
response 3 is submitted directly past both. libuv writes it as soon as
response 1 completes; the tail carrying response 2 follows.

http_connection_send_str_owned (:1888) has the same shape and is used by the
HTTP/1 sendfile head (src/http1/http1_sendfile.c:213).

Scope

Plaintext HTTP/1 only. TLS goes through tls_push and the BIO ring. HTTP/2 and
HTTP/3 frame and order their own streams.

http_connection_emit_parse_error also bypasses the tail — a direct send(2) in
the plaintext read callback (:2140) — but it retires the connection in the same
breath and its bypass is documented with a reason. Named here, not part of this.

What to do

Bring the two owned-buffer senders under the same discipline as the batched ones:
append behind an in-flight write, and mark the connection in flight when they
submit. http_connection_send_batched_writev already does exactly this for an
iov with a free callback.

Evidence to write

A phpt pipelining small, small, large on one connection and asserting the bodies
come back in request order.

Activity

  1. EdmondDantes commented on Aug 21, 2026

    @EdmondDantes
    ContributorAuthor

    The parse-error responder is not the safe exception the report assumed. Same three pipelined requests with the last one malformed answer 200, 400, 200, three runs out of three: the 4xx is built in the read path and, in a coroutine context, went out through http_connection_send → http_connection_send_raw, which submits at the io like the owned senders did.

    Fixed in the same branch: the plaintext side queues through http_connection_send_batched in both contexts, and the destroy that follows already defers on out_in_flight, so the response still leaves before the connection goes. TLS keeps the two paths it had — the awaited send through the BIO pair in a coroutine, the read FSM's atomic SSL_write outside one.

    Evidence: h1/051, which reads 200, 400, 200 against main.

    Also checked and not filed: http_connection_send_raw reads neither out_in_flight nor the tail either, and no reproduction puts it out of order. An HTTP/1 connection dispatches one request at a time and the await drains the tail before the next handler starts.

  2. added 2 commits that reference this issue on Aug 21, 2026
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