Skip to content

Windows: deliver error responses + byte-range bodies (issue #82 follow-up)#88

Merged
EdmondDantes merged 2 commits into
mainfrom
windows-response-delivery
Jun 6, 2026
Merged

Windows: deliver error responses + byte-range bodies (issue #82 follow-up)#88
EdmondDantes merged 2 commits into
mainfrom
windows-response-delivery

Conversation

@EdmondDantes
Copy link
Copy Markdown
Contributor

Two distinct Windows response-delivery bugs from the #82 follow-up suite, both confirmed by instrumentation/code analysis. Each is a real correctness fix (Linux passed by luck), and the full server phpt suite shows no regressions.

1. Lingering close — error responses survive close-during-upload (h1/005)

Rejecting a request mid-upload (e.g. 413 on an over-limit chunked body) sent the 4xx and then immediately closed the socket. On Windows, closesocket() with unread recv data (the client's in-flight upload) forces an abortive RST, which discards the just-sent response — the client saw an empty status. Linux delivered it by luck of softer RST-on-unread timing.

Fix: after emit_parse_error sends the 4xx, shutdown(SD_SEND) flushes it + FIN, then the connection enters a lingering-close state — keep reading and discarding the peer's remaining upload until its FIN (EOF → destroy) or a 5 s budget (bounded by the periodic deadline_tick). The final close is then a clean FIN, not an RST. Cross-platform (SHUT_WR on POSIX). Verified: the existing out_in_flight/request_in_flight paths are guarded so only lingering connections change behaviour.

2. Byte-range bodies via read+writev, not sendfile (static/011-range)

A byte-range body was delivered through ZEND_ASYNC_IO_SENDFILEuv_fs_sendfile(out = socket). On Windows a TCP socket is a Winsock SOCKET, not a CRT fd, and libuv's win uv_fs_sendfile writes to the out fd via the CRT — so the body never reached the socket (206 + Content-Range arrived, body empty). Non-range files ≤ 64 KiB already dodged this via the slurp+writev fast path; ranges were gated out.

Fix: let the slurp fast path handle ranges too — read the file (≤ 64 KiB) and cut the [first, first+len) slice from the in-memory buffer, sent via the normal writev path.
Known gap: a range/file larger than the 64 KiB slurp threshold still uses uv_fs_sendfile and stays broken on Windows — tracked as a follow-up (Windows large-file streaming).

Verification (Windows, Debug_TS)

  • h1/005: PASS · static/011-range: PASS
  • Full suite (excl. sendfile): 136 pass / 7 fail / 45 skip — was 133/10 before this branch + the merged test-portability PR. The 7 remaining fails are pre-existing and unrelated (peer-RST cancellation #2, ThreadPool crash #3, precompressed UAF / static-h2 #5). No regressions.

…load (Windows)

When the server rejects a request mid-upload (e.g. 413 on an over-limit
chunked body), it sent the 4xx then immediately closed the socket. On
Windows, closesocket() with unread recv data (the client's in-flight
upload) forces an abortive RST that discards the just-sent response, so
the client received nothing (empty status). Linux delivered it by luck of
softer RST-on-unread timing.

Fix: after emit_parse_error sends the 4xx, shutdown(SD_SEND) flushes it +
FIN, then the connection enters a lingering-close state — keep reading and
DISCARDING the peer's remaining upload until its FIN (EOF -> destroy) or a
5s budget (bounded by the periodic deadline_tick). The final close is then
a clean FIN, not an RST. Cross-platform (SHUT_WR on POSIX).

Fixes h1/005 on Windows. Full server suite: no regressions (135 pass / 8
fail, all pre-existing; was 133/10).
…sendfile)

A byte-range response delivered its body through ZEND_ASYNC_IO_SENDFILE ->
uv_fs_sendfile(out = socket). On Windows a TCP socket is a Winsock SOCKET,
not a CRT fd, and libuv's win uv_fs_sendfile writes to the out fd via the
CRT _write() -- so the body never reaches the socket (206 headers arrive,
body empty). Non-range files within the 64 KiB slurp threshold already
dodged this via the slurp+writev fast path; ranges were gated out of it.

Fix: let the slurp fast path handle ranges too -- read the file (<= 64 KiB)
and cut the [first, first+len) slice from the in-memory buffer, sent via
the normal writev path. Cross-platform.

Fixes static/011-range on Windows. (A range/file larger than the slurp
threshold still uses uv_fs_sendfile and remains broken on Windows --
tracked as a follow-up.) Full suite: no regressions (136 pass / 7 fail,
all pre-existing).
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 6, 2026

Coverage

Total lines: 81.05% → 79.78% (-1.27 pp)

File Baseline Current Δ Touched
src/core/http_connection.c 68.09% 70.40% +2.31 pp
src/core/http_connection.h 81.82% 81.82% +0.00 pp
src/http2/http2_session.c 87.40% 86.71% -0.68 pp
src/http2/http2_static_response.c 71.85% 20.99% -50.86 pp
src/send_file.c 87.09% 87.20% +0.12 pp

@EdmondDantes EdmondDantes merged commit a2f3352 into main Jun 6, 2026
8 checks passed
@EdmondDantes EdmondDantes deleted the windows-response-delivery branch June 6, 2026 08:40
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