Files
Martin Vogel dd304b53e4 fix(daemon): keep an inline rejection that beats the client's HELLO
daemon_runtime_connection_cap_covers_slow_hello_and_stopping_is_terminal
failed ASSERT(capacity_rejected) seven times (TSan macos-14 x3, LSan
macOS x1, macos-15-intel x1, local plain macOS x2) and passed on rerun.
Two interleavings fail it; each was forced and fails 3 of 3.

1. Production race. At capacity (and while stopping) the accept loop
   answers a new connection WITHOUT reading its HELLO and closes it at
   once; on POSIX the reject drain is a no-op. A Unix-socket connect
   completes from the listen backlog, so the client can still be between
   connect() and its HELLO send when that close lands. The send then
   fails with EPIPE, cbm_daemon_runtime_client_connect returned a zeroed
   result (status ERROR, empty message), and the complete 810-byte
   rejection sat unread in the client's receive buffer. Forced by
   holding the client before its send until the daemon had closed:
   overflow status=0 message="", 810 bytes pending,
   active_connections=2, active_clients=1. A standalone AF_UNIX probe
   on macOS shows the same: send rc=-1 errno=EPIPE, then recv returns
   the 810 bytes, then EOF. Bootstrap then
   classified the attempt by lock state instead of by the daemon's own
   "capacity" / "stopping" answer, and the user-visible reason was lost.

2. Test window. The slow-hello connection held its slot only until the
   daemon's first-frame deadline (request_timeout_ms, 2000 ms in the
   fixture). A runner stalled that long freed the slot, and the
   active_connections == 2 conjunct read 1. Forced by waiting for that
   expiry before the overflow probe: freed after 2002 ms, capacity
   still reported, active_connections=1.

Fix: when the HELLO send fails, the client reads the answer anyway
through the new cbm_daemon_ipc_receive_frame_after_failed_send. A failed
send consumes no inbound bytes, so the inbound frame boundary is intact.
The function lifts the send-side poison for that one receive and
restores it afterwards. Only a REJECTED answer is accepted in that
state; anything else stays the plain send failure it always was. On
Windows a server close discards unread pipe data, so this path just
fails there as before. The bounded drain-before-close remains that
platform's mechanism.

Test rebuild (no timeout raised, no retry):
- cbm_daemon_runtime_hold_peer_waits_for_testing: the slow hello keeps
  its slot until the test closes it, and the Windows reject drain ends
  on the peer's close, not after 250 ms. Both counts are held states
  when they are read.
- cbm_daemon_runtime_set_hello_hook_for_testing: four points
  (client connected / sent, daemon rejecting / rejected) let a test pin
  the order. The existing test now pins "daemon answers after the HELLO
  is on the wire", which every platform delivers. The new
  daemon_runtime_capacity_rejection_survives_daemon_close_before_hello
  pins "daemon answered and closed before the HELLO" (POSIX only; it is
  compile-gated because Windows discards that data by design). Both
  assert that the pin really happened, and print the overflow result on
  failure.
- The waits end on guaranteed events (HELLO sent; rejection closed or a
  third slot admitted, the latter held by the seam), never on a bound.

Proof (macOS arm64, ASan/UBSan test-runner):
- forced late HELLO, current client: RED 3/3, identical signature;
  with the fix GREEN 3/3; fix reverted: RED 3/3 again;
- forced 6000 ms stall before the overflow probe (3x the deadline that
  freed the slot): slot still held, both tests GREEN 3/3;
- both pinned orders, 200 iterations each in one process: 400 passed;
- daemon_runtime suite alone, 50 sequential runs: 50 x 51 passed,
  0 failed;
- daemon_runtime under TSan (the venue of 3 sightings): 51 passed, no
  ThreadSanitizer report;
- daemon_ipc 54 passed (2 Linux-only platform skips), daemon_application
  67 passed; lint-ci clean.
Not run here: the Linux and Windows legs (the Windows path is
compile-gated and reasoned from the drain's documented contract).

Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
2026-10-02 00:52:13 +02:00
..