Skip to content

HTTP/1 writes a response through two channels, which blocks a non-blocking append #179

Closed

Description

@EdmondDantes

Found while trying to give HttpResponse a non-blocking append (#177), and it outlives that attempt.

Every byte of an HTTP/1 response goes through http_connection_send → http_connection_send_raw, which submits a uv_write and awaits its completion (src/core/http_connection.c:1374-1389). So a streamed chunk always parks the handler coroutine, whether or not the socket had room. A non-blocking offer is impossible on this path by construction.

The connection also owns a second writer, http_connection_send_batched, which keeps one write in flight and coalesces the rest into out_pending_buf, submitted from the completion callback (:1588-1606). It never suspends. WebSocket's trySend() uses it, and http_connection_outbound_over_highwater() measures its tail.

Using the second writer for streamed chunks is what does not work today, and the reason is ordering. Headers (h1_emit_headers_once), a blocking send(), and the terminal 0\r\n\r\n from mark_ended all take the raw path. A chunk body queued in out_pending_buf therefore sits behind an in-flight write while the next raw send goes out immediately and reaches the peer first. Chunked framing does not survive that: the client reads the terminal chunk, then parses the stranded body as a chunk-size line, and on a keep-alive connection the desync poisons every pipelined response behind it. TLS has its own version — tls_fsm_send_plaintext_atomic writes straight to SSL while headers went through the ring — plus a high-water mark that reads out_pending_len, a buffer TLS connections never fill.

What is needed

One serialized outbound path per connection for HTTP/1 response bytes — headers, chunks and the terminal chunk through the same writer — with backpressure read from that writer's own depth. A blocking producer then suspends on the drain event rather than on a write completion, and a non-blocking one gets a refusal that means something. That also removes a park from the streaming hot path, where every chunk currently costs one await.

Blocks #177.

Activity

  1. EdmondDantes commented on Aug 20, 2026

    @EdmondDantes
    ContributorAuthor

    Measured, on a release PHP built for the run. Numbers and method in
    dev/BENCHMARKS.md, entry 2026-08-20.

    strace -e trace=write,epoll_pwait on one request of four 16 KiB chunks: three
    write(2) and three zero-timeout epoll_pwait per chunk, whatever the chunk
    size, plus one write for the headers and one for the terminator. The loop turn
    after each write is the coroutine suspending — async_io_req_await returns early
    only on req->completed, and libuv's inline-write fast path does not fire for
    back-to-back writes on one stream.

    Body held at 64 KiB, only the chunk count moved. wrk -t1 -c4 -d6s, three runs
    per cell, median. The second build differs by one hunk: the three pieces are
    copied into one buffer and sent as a single awaited write.

    chunks chunk three writes one write gain
    1 64 KiB 15746 17207 +9.3%
    4 16 KiB 8320 11876 +42.7%
    16 4 KiB 2938 5209 +77.3%
    64 1 KiB 893 1650 +84.8%

    Per-chunk cost is flat in the chunk size and halves: ~18.5 µs to ~8.8 µs. The two
    extra syscalls and two extra loop turns are worth about 10 µs per chunk.

    What it decides

    The whole measured win is reachable by coalescing the frame at the call site. That
    needs no queue on the connection, no second writer to order against and no
    per-response structure — the three things both designs in dev/PLAN.md foundered
    on. So this issue's own proposal is not what the number supports; the coalesced
    frame is, and it should land as its own change.

    What remains genuinely open here is the other half: tryWrite() cannot refuse on
    HTTP/1, and awaitWritable() has nothing to wait for. That has to be argued on its
    own, because nothing in this measurement touches it.

    One obstacle for the coalesced frame: the prototype copies the chunk because the
    ABI has no awaitable vectored write. io_pipe_writev_cb
    (php-src/ext/async/libuv_reactor.c:4947) sends no NOTIFY and frees the request
    itself, so ZEND_ASYNC_IO_WRITEV is fire-and-forget by construction. Dropping the
    copy means adding that op to ext/async; keeping it still wins by the numbers above.

  2. added this to the Next3 milestone on Aug 20, 2026
  3. self-assigned this
    on Aug 20, 2026
  4. added
    bug
    on Aug 20, 2026
  5. EdmondDantes commented on Aug 21, 2026

    @EdmondDantes
    ContributorAuthor

    Measured, on e246dcf with the release PHP at ABI 0.25 and strace -f -c around
    tests/perf/servers/server_stream.php. Two runs per configuration differing only in how
    many chunks a response carries, so the difference divides out startup and the request
    itself. Recorded in dev/BENCHMARKS.md (server#191).

    transport chunk chunks measured submits waits per chunk
    plaintext 4 KiB 960 960 writev 1009 1.00 submit, 1.05 waits
    plaintext 64 KiB 480 480 writev 510 1.00 submit, 1.06 waits
    TLS 4 KiB 480 249 write 69 0.52 writes, 0.14 waits

    This issue was argued from three submits and up to three suspensions per chunk. On
    plaintext both are one, flat in the chunk size — #184 made the frame a single vectored
    write and #186 made it a single awaited one. On TLS the BIO ring already batches: two
    chunks share a socket write and most chunks never park.

    What is left to win is the one park per chunk on plaintext, and a queue can only take it
    by making the write fire-and-forget. That is the same fire-and-forget the body-contract
    work removed from this path: with it, isWritable() cannot report a failed write and
    tryWrite() has no depth to refuse from. Recommend closing this in favour of the
    documented HTTP/1 exception, unless the park itself turns out to cost something a
    throughput measurement can show.

  6. added a commit that references this issue on Aug 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions