Skip to content

Commit 0232737

Browse files
committed
FEATURE-ci-v3-runtime-green: own the listener port on Winsock and bound the truncation fixture past stdio readahead
Two CI blockers at 9c9b47a, both first exposures of lanes repaired in stage 16: Windows/mingw64 (acceptance run 46963856090, package run 46963855675): the installed tls-yes http2 matrix died on the rollback one-shot with 'partial H2 bind was retained' in both linkages. open_listener set Winsock SO_REUSEADDR, which forcibly binds a port another socket is actively serving, so the scenario's occupied-port obstacle bind succeeded and listen() returned ok. Microsoft's socket-security guidance (bind matrix) requires SO_EXCLUSIVEADDRUSE for servers: the same-account specific-address second bind fails WSAEADDRINUSE, and a listening socket carries no TIME_WAIT of its own, so the restarted-server rebind property POSIX SO_REUSEADDR provided is preserved. open_listener now sets the platform option (exclusive on _WIN32, reuseaddr elsewhere); io_poll_sys_listener pins the option and adds the live-listener bind refusal on every platform, making the rollback scenarios' precondition a unit contract. The _WIN32 branch is CI-compiled (no mingw toolchain locally, recorded in the task card). FreeBSD (acceptance run 46963856090, test-suite.log from the failure evidence artifact): make check failed 1/235, response_definition file_send_truncated_mid_send_fails, done==1 with an okay outcome and end_calls()==1 -- the stage-16 rewrite verification passed, so the truncation was on disk yet never observed. The reopen_file cursor reads through std::ifstream whose libc++ basic_filebuf buffers the FILE* to st_blksize (32 KiB on FreeBSD UFS) and refills 4096/4092 bytes; the two parked 16 KiB chunks drove ~36.8 KiB of fread refills and staged the remaining 4 KiB of the 40 KiB payload in user space, so the post-truncate pulls were served from the buffer with no EOF. Reproduced with a local probe (setvbuf 32 KiB + libc++ refill schedule + mid-stream truncate: 40960 bytes served, feof==0). Production pull_file behaved correctly for what it observed on every platform, so the repair stays in the fixture: the payload grows to 2 MiB (bytes owed after the parked chunk past any plausible st_blksize) and the bounded wake-and-pull drive to 256 rounds; a follow-up probe proves the short read now surfaces under 4 KiB-1 MiB buffers. PORTABILITY.md documents the readahead bound for future mid-send-mutation tests. Local gates (macOS arm64 scratch worktree at 9c9b47a + these edits): response_definition 311/311 x3, response_sources 304/304 x3, io_poll_sys_listener 46/46 including the new refusal check; cpplint clean on the changed C++ files; git diff --check clean; check-skip-rationales clean. The file-size gate's two over-limit files (connection_engine.cpp, io_poll_backend.cpp) are pre-existing at HEAD and untouched here.
1 parent 9c9b47a commit 0232737

8 files changed

Lines changed: 133 additions & 24 deletions

File tree

‎ChangeLog‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
Version 3.0.0 - 2026-10-10
2+
Bind native listeners with SO_EXCLUSIVEADDRUSE on Winsock instead
3+
of SO_REUSEADDR, which on Windows forcibly shares a live
4+
listener's port: the installed-consumer rollback scenarios
5+
observed the obstacle bind succeed and the partial-bind
6+
rollback contract fail ("partial H2 bind was retained"); the
7+
exclusive option keeps the POSIX restart property (a closed
8+
listening socket carries no TIME_WAIT) while refusing every
9+
bind onto a live listener.
10+
Enlarge the mid-send truncation fixture past any stdio readahead:
11+
the reopen_file cursor's FILE* buffers st_blksize bytes ahead
12+
(32 KiB on FreeBSD UFS), so a 40 KiB payload was fully staged
13+
before the on-disk truncate and the send legally completed;
14+
2 MiB owed after the parked chunk forces the shrink to
15+
surface on every filesystem.
216
Rewrite the file-source test asset with checked direct syscalls
317
and a visible-size verification: a silently failed ofstream
418
open left the asset intact, so the truncated mid-send suites

‎specs/tasks/FEATURE-ci-v3-runtime-green.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,13 @@ Full local `make check -j1` (TLS=no, g++ 15.2, WSL2): 232/235 pass, 2 platform s
216216
- [x] Repair: `temp_file::rewrite` now truncates and writes through checked direct syscalls (POSIX `::open(O_WRONLY|O_TRUNC)`/`::write`/`::close`, Windows `_open(_O_WRONLY|_O_TRUNC|_O_BINARY|_O_NOINHERIT)`/`_write`/`_close`, the rig's `rig_write`/`rig_close` shims hoisted above the class) and verifies the visible post-write size via `std::filesystem::file_size`, failing with the sizes in the message. A platform that cannot truncate now fails the suite immediately with evidence instead of corrupting the scenario. The test assertions and the stage-15 drive loop are unchanged.
217217
- [x] Local gates (macOS arm64 scratch worktree at 0151bdbb + the rig edit): `response_sources` 30/304 green ×3, `response_definition` 32/311 green ×3, `native_http1_parity` green; a guard probe makes the truncating open impossible (chmod 0444) — the new rig throws with the errno evidence (EACCES) while the pre-fix rig silently succeeds, proving both sides of the failure mode; cpplint clean; `git diff --check` clean; the same workflow/platform script-test environmental baseline as stage 15. The Windows `_open` branch was not compiled locally (no mingw toolchain), recorded honestly, CI compiles it.
218218
- [ ] Full exact-source CI on the working branch (mingw64 tls-yes build past the endpoint link, Windows acceptance, FreeBSD make check past the fixture repair, darwin/macos lanes past the runner-capacity failures).
219+
220+
## Stage 17: Winsock exclusive listener ownership and the stdio readahead bound (2026-12-03)
221+
222+
- [x] Evidence (run 46963856090 acceptance + run 46963855675 package, both at 9c9b47ae; FreeBSD failure evidence artifact downloaded to `/Volumes/WS_BLACK_SN7100_1TB/tmp-evidence/run-46963856090-freebsd/`): the Windows acceptance job and the mingw64 package job failed identically inside the tls-yes installed matrix — `check-v3-public-http2.py` ONESHOTS `rollback`: `shared.exe <fixtures> managed rollback` died on `partial H2 bind was retained` (both linkages, the first Windows exposure of the installed tls-yes http2 lane past the stage-16 link repairs). The FreeBSD acceptance job failed `gmake check` tls-no 1/235: `response_definition` test 24 `file_send_truncated_mid_send_fails`, checks at lines 858/860 with the stage-16 signature (done==1, outcome not protocol_error, `end_calls()==1`) — the rewrite itself verified (stage-16 rig check green), so the truncation was visible on disk yet never observed by the cursor.
223+
- [x] Windows root cause: the `rollback` one-shot binds its second listener onto an occupied loopback port and requires the bind to fail (the partial-bind rollback contract). `pollsys::open_listener` set Winsock `SO_REUSEADDR`, which on Windows means "forcibly bind a port another socket is actively serving" — the obstacle bind succeeded, `listen()` returned ok, and the consumer correctly reported the contract violation. On POSIX `SO_REUSEADDR` does not permit an active same-address double bind, so every other platform observed the refusal. Verified against Microsoft's socket-security documentation (bind matrix + "All server applications must set SO_EXCLUSIVEADDRUSE"): same-account specific-address exclusive→exclusive second bind fails `WSAEADDRINUSE`, and a listening socket carries no TIME_WAIT of its own, so the restarted-server rebind property that motivated `SO_REUSEADDR` is preserved (the documented exclusive caveat — a rebinding delay after accepted connections linger — concerns graceful-shutdown residue, not listening sockets).
224+
- [x] Windows repair: `open_listener` sets `SO_EXCLUSIVEADDRUSE` on `_WIN32` and `SO_REUSEADDR` elsewhere; `io_poll_sys_listener` pins the platform option AND adds the live-listener bind refusal (a second `open_listener` on the bound port must return `k_invalid_socket`) on every platform — the rollback scenarios' precondition is now a pinned unit contract, not just an installed-consumer outcome. The hpp contract comment and the Makefile.am suite description updated. The `_WIN32` branch was not compiled locally (no mingw toolchain), recorded honestly; the Windows/mingw64 CI lanes compile and run it.
225+
- [x] FreeBSD root cause: the reopen_file cursor reads through `std::ifstream`, whose libc++ `basic_filebuf` wraps a `FILE*` buffered to the filesystem's `st_blksize` (macOS/Linux 4 KiB; FreeBSD UFS 32 KiB; ZFS up to 128 KiB), refilling 4096 then 4092 bytes per underflow. The two parked 16 KiB chunks drive ~36.8 KiB of `fread` refills, staging the remaining 4 KiB of the 40 KiB payload in user space; after the (verified) truncate to 100 bytes the final pulls are served entirely from the buffer — no error, no EOF, `feof()==0` — and the send legally completes. Reproduced exactly with a local probe (macOS clang++, `setvbuf` 32 KiB, libc++ refill schedule, truncate mid-stream: total served 40960, feof 0). Production `pull_file` behaved correctly for what it observed on every platform — same standard as stage 16; the repair belongs in the fixture.
226+
- [x] FreeBSD repair: the test payload grows to 2 MiB (bytes owed after the parked chunk ≈ 2 MiB, past any plausible `st_blksize`) and the bounded wake-and-pull drive grows to 256 rounds so buffered pulls drain before the shrink must surface; the test comment and PORTABILITY.md document the readahead bound for future mid-send-mutation tests. A permanent stall still fails the same assertions with the same evidence.
227+
- [x] Local gates (macOS arm64 scratch worktree at 9c9b47ae + these edits): `response_definition` 32/311 checks green ×3 (the enlarged fixture included), `response_sources` 30/304 green ×3, `io_poll_sys_listener` green including the new live-listener refusal; cpplint clean on the changed C++ files; `git diff --check` clean; the same workflow/platform script-test environmental baseline as stages 15/16.
228+
- [ ] Full exact-source CI on the working branch (mingw64 and Windows acceptance past the installed http2 rollback, FreeBSD make check past the readahead bound, darwin/macos lanes green).

‎src/detail/io_poll_sys.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,19 @@ native_socket_t open_listener(std::string_view address, std::uint16_t port, std:
325325
return listener_failure(socket);
326326
}
327327
int reuse = 1;
328+
#if defined(_WIN32)
329+
// Winsock's SO_REUSEADDR would forcibly bind a port another socket
330+
// is actively serving (Microsoft's socket-security guidance: "All
331+
// server applications must set SO_EXCLUSIVEADDRUSE"), silently
332+
// splitting one port across two listeners. The exclusive option
333+
// keeps the property POSIX SO_REUSEADDR provides here -- a
334+
// restarted server rebinds its configured port once the previous
335+
// listening socket closed (a listening socket carries no TIME_WAIT
336+
// of its own) -- while refusing every bind onto a live listener.
337+
::setsockopt(socket, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, reinterpret_cast<const char*>(&reuse), sizeof(reuse));
338+
#else
328339
::setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const char*>(&reuse), sizeof(reuse));
340+
#endif
329341
if (!bind_listener(socket, storage)) {
330342
return listener_failure(socket);
331343
}

‎src/httpserver/detail/io_poll_sys.hpp‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,12 @@ bool bind_listener(native_socket_t socket,
254254
std::uint16_t bound_listener_port(native_socket_t socket);
255255

256256
// A listening stream socket bound to @p address:@p port, left
257-
// nonblocking, with SO_REUSEADDR set (a restarted server rebinds its
258-
// configured port immediately). Address spellings per
257+
// nonblocking, with the platform's port-ownership option set
258+
// (SO_REUSEADDR on POSIX, SO_EXCLUSIVEADDRUSE on Winsock, where
259+
// SO_REUSEADDR would permit hijacking a live listener's port; a
260+
// restarted server still rebinds its configured port immediately --
261+
// the previous listening socket closed and carries no TIME_WAIT of
262+
// its own). Address spellings per
259263
// fill_listener_address; port 0 requests an ephemeral port and the
260264
// resolved host-order port is always reported through @p bound_port.
261265
// Any failure closes the handle and returns k_invalid_socket.

‎test/Makefile.am‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,9 @@ http1_response_outbox_LDADD = $(LDADD)
259259

260260
# TASK-108 step 1: loopback/any-address listener bind helper
261261
# (pollsys::open_listener): v4/ANY/v6 literals, resolved port report,
262-
# SO_REUSEADDR, nonblocking accept, name rejection. TASK-119 adds the
262+
# the port-ownership option (SO_REUSEADDR; SO_EXCLUSIVEADDRUSE on
263+
# Winsock) plus the live-listener bind refusal, nonblocking accept,
264+
# name rejection. TASK-119 adds the
263265
# fill_peer battery (accept-time peer capture) cross-checked against
264266
# net::parse_address over the shared byte decode in v3core
265267
# (detail/net_address.cpp), so the suite uses the default LDADD and

‎test/PORTABILITY.md‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,47 @@ blocks.
226226
pipe sources verify streaming and one-shot semantics, not
227227
throughput.
228228

229+
### `response_definition_test.cpp` — mid-send truncation vs stdio readahead (all platforms)
230+
231+
- **Symptom**: `file_send_truncated_mid_send_fails` passed on
232+
Linux/macOS but completed as a legal success on FreeBSD
233+
(2026-12-02: `done==1`, outcome okay, `end_calls()==1` after the
234+
truncation).
235+
- **Root cause**: the reopen_file cursor reads through `std::ifstream`,
236+
whose underlying `FILE*` buffers up to the filesystem's `st_blksize`
237+
ahead of the pulled position (4 KiB on ext4/APFS, 32 KiB on FreeBSD
238+
UFS, up to 128 KiB on ZFS). Bytes already staged in that user-space
239+
buffer survive an on-disk `ftruncate` — `fread` keeps serving them
240+
with no error and no EOF — so a 40 KiB payload whose both parked
241+
chunks drove ~36.8 KiB of refills was fully covered by a 32 KiB
242+
buffer. Production `pull_file` behaved correctly for what it
243+
observed on every platform.
244+
- **Repair in force**: the payload is 2 MiB, leaving ~2 MiB owed after
245+
the parked chunk — past any plausible `st_blksize` — and the
246+
wake-and-pull drive bound is 256 rounds so buffered pulls drain
247+
before the shrink must surface. Any future mid-send-mutation test of
248+
a stream-buffered source must keep the bytes-owed-after-park larger
249+
than the largest plausible stdio buffer.
250+
251+
## Listener port ownership (`pollsys::open_listener`)
252+
253+
`open_listener` sets the platform's port-ownership socket option
254+
before binding: `SO_REUSEADDR` on POSIX and `SO_EXCLUSIVEADDRUSE` on
255+
Winsock. On Winsock, `SO_REUSEADDR` means "forcibly bind a port another
256+
socket is actively serving" (Microsoft's socket-security guidance is
257+
that server applications must use `SO_EXCLUSIVEADDRUSE` instead): two
258+
listeners could silently share one port with nondeterministic
259+
connection delivery, and every same-address bind-conflict scenario —
260+
including the installed-consumer `rollback` one-shots that verify
261+
partial-bind rollback — would observe the second bind succeed
262+
(2026-12-02 mingw64/Windows acceptance failures,
263+
`partial H2 bind was retained`). The exclusive option preserves the
264+
restart property POSIX `SO_REUSEADDR` provides (a restarted server
265+
rebinds its port once the previous listening socket closed — a
266+
listening socket carries no TIME_WAIT of its own) while refusing every
267+
bind onto a live listener. `io_poll_sys_listener` pins both the option
268+
and the live-listener bind refusal on every platform.
269+
229270
## Adding a new entry
230271

231272
1. Add a `// reason: <one-liner>` comment within the 5 lines immediately

‎test/unit/io_poll_sys_listener_test.cpp‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
// (pollsys::open_listener). Pins: the three accepted address spellings
2424
// (IPv4 literal, any-address "" and "*", IPv6 literal), the resolved
2525
// port report (0 -> ephemeral, nonzero -> the requested port), the
26-
// SO_REUSEADDR option, nonblocking accept behavior (the poll backend's
27-
// adopt contract), and the typed rejection of a non-literal address.
26+
// platform's port-ownership socket option (SO_REUSEADDR on POSIX,
27+
// SO_EXCLUSIVEADDRUSE on Winsock), nonblocking accept behavior (the
28+
// poll backend's adopt contract), and the typed rejection of a
29+
// non-literal address.
2830
// TASK-119 adds the fill_peer battery (the accept-time peer capture)
2931
// over synthesized transport address storages, cross-checked against
3032
// net::parse_address so the byte path and the text path of one host
@@ -45,16 +47,19 @@ namespace {
4547
namespace pollsys = httpserver::detail::pollsys;
4648
namespace net = httpserver::net;
4749

48-
// True iff SO_REUSEADDR is enabled on @p handle (both platforms spell
49-
// the option identically; only the option-value pointer type differs).
50-
bool reuseaddr_enabled(pollsys::native_socket_t handle) {
50+
// True iff the platform's port-ownership option is enabled on @p
51+
// handle: SO_REUSEADDR on POSIX, SO_EXCLUSIVEADDRUSE on Winsock (both
52+
// spell the option-value pointer type differently; see open_listener).
53+
bool port_ownership_enabled(pollsys::native_socket_t handle) {
5154
int value = 0;
5255
#if defined(_WIN32)
5356
int length = sizeof(value);
57+
const int option = SO_EXCLUSIVEADDRUSE;
5458
#else
5559
socklen_t length = sizeof(value);
60+
const int option = SO_REUSEADDR;
5661
#endif
57-
return ::getsockopt(handle, SOL_SOCKET, SO_REUSEADDR,
62+
return ::getsockopt(handle, SOL_SOCKET, option,
5863
reinterpret_cast<char*>(&value), &length) == 0
5964
&& value != 0;
6065
}
@@ -118,13 +123,23 @@ LT_BEGIN_AUTO_TEST(io_poll_sys_listener_suite, requested_port_reported)
118123
pollsys::close_socket(listener);
119124
LT_END_AUTO_TEST(requested_port_reported)
120125

121-
// SO_REUSEADDR is set so a restarted server can rebind immediately.
126+
// The port-ownership option is set so a restarted server can rebind
127+
// immediately -- and on Winsock, so a second bind onto the live
128+
// listener is refused instead of hijacking the port.
122129
LT_BEGIN_AUTO_TEST(io_poll_sys_listener_suite, reuseaddr_set)
123130
std::uint16_t port = 0;
124131
pollsys::native_socket_t listener =
125132
pollsys::open_listener("127.0.0.1", 0, port);
126133
LT_CHECK(listener != pollsys::k_invalid_socket);
127-
LT_CHECK(reuseaddr_enabled(listener));
134+
LT_CHECK(port_ownership_enabled(listener));
135+
// A second bind of the same specific address while the first
136+
// listener is live must fail on every platform: the listener owns
137+
// its port (the installed-consumer rollback scenarios and the
138+
// partial-bind rollback contract depend on the refusal).
139+
std::uint16_t collision = 0;
140+
pollsys::native_socket_t squatter =
141+
pollsys::open_listener("127.0.0.1", port, collision);
142+
LT_CHECK(squatter == pollsys::k_invalid_socket);
128143
pollsys::close_socket(listener);
129144
LT_END_AUTO_TEST(reuseaddr_set)
130145

‎test/unit/response_definition_test.cpp‎

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -810,10 +810,20 @@ LT_BEGIN_AUTO_TEST(response_definition_suite, file_send_short_of_declared_length
810810
LT_END_AUTO_TEST(file_send_short_of_declared_length_fails)
811811

812812
// (S3.4b) The same diagnosis covers a file that shrinks MID-send:
813-
// after the send's own prepare, the read bound stays pinned, so the
814-
// next pull meets a clean EOF with bytes still owed.
813+
// after the send's own prepare, the read bound stays pinned, so a
814+
// later pull meets a clean EOF with bytes still owed. The payload must
815+
// dwarf any stdio FILE* readahead: the reopen_file cursor reads
816+
// through std::ifstream whose underlying FILE* buffers up to the
817+
// filesystem's st_blksize ahead of the pulled position (4 KiB on
818+
// Linux/macOS ext4/APFS, 32 KiB on FreeBSD UFS, up to 128 KiB on ZFS),
819+
// and bytes already staged in that user-space buffer legally survive
820+
// the on-disk truncation (FreeBSD 2026-12-02: both parked chunks drove
821+
// ~36.8 KiB of fread refills, the 32 KiB buffer covered the rest of a
822+
// 40 KiB payload, and the send completed as success — correct for what
823+
// pull_file observed). 2 MiB leaves ~2 MiB owed after the parked
824+
// chunk, past any plausible st_blksize, so the shrink must surface.
815825
LT_BEGIN_AUTO_TEST(response_definition_suite, file_send_truncated_mid_send_fails)
816-
const std::string payload = repeating(40 * 1024);
826+
const std::string payload = repeating(2 * 1024 * 1024);
817827
fake::temp_file asset(payload);
818828

819829
response_definition def;
@@ -822,8 +832,9 @@ LT_BEGIN_AUTO_TEST(response_definition_suite, file_send_truncated_mid_send_fails
822832
def).ok());
823833

824834
capturing_sink sink;
825-
// Capacity fits the first 16 KiB chunk exactly: the second chunk
826-
// parks, and the truncation lands between the send's pulls.
835+
// Capacity fits the first 16 KiB chunk exactly: the send parks
836+
// after one pull, and the truncation lands between the send's
837+
// pulls.
827838
fake::scripted_body_sink out(k_file_chunk_capacity);
828839
exchange x(make_head(), &sink, 0, nullptr, &out);
829840
manual_executor ex;
@@ -843,14 +854,14 @@ LT_BEGIN_AUTO_TEST(response_definition_suite, file_send_truncated_mid_send_fails
843854
LT_CHECK_EQ(out.drain(1u << 20),
844855
static_cast<std::size_t>(k_file_chunk_capacity));
845856
ex.run_pending();
846-
// One wake-and-pull round normally completes the failed send. The
847-
// bounded drive tolerates a platform where the truncation surfaces
848-
// one pull later (a stream-buffer tail still holds pre-truncation
849-
// bytes and the next pull parks the exactly-full queue once more)
850-
// or where the completion lands one executor round later. A
851-
// permanent stall still fails the checks below with the same
852-
// evidence (FreeBSD 2026-10-30/2026-11-30, checks 846/847).
853-
for (int rounds = 1; rounds < 8 && done == 0; ++rounds) {
857+
// Each round drains one parked 16 KiB chunk and pulls the next: a
858+
// platform whose stdio readahead staged pre-truncation bytes serves
859+
// those pulls from the buffer, and the drive continues until a pull
860+
// finally reaches the kernel past the shrunk file (128 pulls for
861+
// this payload, plus slack). A permanent stall still fails the
862+
// checks below with the same evidence (FreeBSD
863+
// 2026-10-30/2026-11-30/2026-12-02).
864+
for (int rounds = 1; rounds < 256 && done == 0; ++rounds) {
854865
out.drain(1u << 20);
855866
ex.run_pending();
856867
}

0 commit comments

Comments
 (0)