Skip to content

sendfile: report the bytes TransmitFile sent, and wait out EAGAIN on Darwin and FreeBSD - #308

Merged
EdmondDantes merged 3 commits into
mainfrom
transmitfile-sent-count
Sep 30, 2026
Merged

EdmondDantes merged 3 commits into
mainfrom
transmitfile-sent-count

Conversation

@EdmondDantes

@EdmondDantes EdmondDantes commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Two defects in the sendfile backend, both found through true-async/server#362 (a file body cut short must end the HTTP/1 connection).

Windows: TransmitFile counted the bytes it was asked for. io_transmitfile_work_cb added the requested chunk to transferred after each call. TransmitFile stops at EOF and still succeeds, so a file truncated after its size was taken reported the full length. The worker now reads each pass's count off the file pointer TransmitFile advanced and stops at the first short pass.

Darwin and FreeBSD: EAGAIN failed the transfer. libuv calls sendfile(2) on the non-blocking socket there and returns -1/EAGAIN when a full socket buffer took nothing (src/unix/fs.c); io_sendfile_zc_cb completed that as an error. On macOS CI (server PR #363) a 2 MiB plaintext body reached the client cut at 1,129,404 bytes (debug) and 998,696 (release). Linux never gets EAGAIN here: libuv falls back to a copy loop that polls. The request now waits for the socket to become writable (uv_poll on an F_DUPFD_CLOEXEC dup, since the stream handle holds the descriptor in the poller) and submits the rest. A dispose during the wait, and a close of the destination io, end the wait with UV_ECANCELED, so the dup never outlives the connection.

Evidence: the wait path, forced on Linux by a temporary hack that turned every other positive sendfile result into EAGAIN, served 176 of 176 server phpt over sendfile, static, h1, h2 and tls. The php-async suite passes locally except the six tests whose ../../../../ includes miss through a symlinked ext/async, as on main. The macOS and Windows proof is the server CI against this branch (true-async/server#363).

@codecov

codecov Bot commented Sep 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@EdmondDantes EdmondDantes changed the title Count the bytes TransmitFile sent, so a file that shrank shows as a short transfer sendfile: report the bytes TransmitFile sent, and wait out EAGAIN on Darwin and FreeBSD Sep 30, 2026
@EdmondDantes
EdmondDantes merged commit 1521d69 into main Sep 30, 2026
9 checks passed
EdmondDantes added a commit to true-async/server that referenced this pull request Sep 30, 2026
…363)

A failed or short HTTP/1 file body kept the connection alive, so the next response was read as the rest of the body. The on_done callbacks honour the engine status and mark the framing lost, plaintext sendfile reports a short count as a failure, a stale cache entry is evicted, and a refused deferred start is answered whole. Adds --enable-fault-injection (libfiu) with _http_fault_* hooks. Needs true-async/php-async#308.

Mutants: S2, hand 10 by 28.2: 9 killed, 1 survived (fault_hooks.c name-length bound, now killed by core/083), 0 not run; base eaa2d57
@EdmondDantes
EdmondDantes deleted the transmitfile-sent-count branch October 1, 2026 09:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant