Repository navigation
refactor(daemon): one poll(2) accept engine, so the accept path holds no threads - #7681
Merged
Merged
Conversation
… no threads The daemon had two portable accept engines. `SingleListenerEngine` polled one listener from the accept thread; `MultiListenerEngine` - selected whenever more than one address was bound, which is the Linux dual-stack default - spawned one acceptor thread per listener and relayed accepted sockets back over a bounded mpsc channel. That thread-per-family shape is a correctness problem, not just extra machinery. Per-connection daemon state includes process-wide syscalls - `chroot(2)` and `setuid(2)` above all - so isolating it per connection means forking the session, and `platform::session_fork` documents that callers must fork from a single-threaded accept path: a child forked from a multithreaded parent can deadlock on an allocator lock held by a thread that does not exist in the child. The acceptor threads made that fork illegal on exactly the topology daemons default to. `poll(2)` already takes a slice of fds, so one wait covers every listener from the caller's thread. Both engines collapse into `PollAcceptEngine`, and the mpsc relay, `AcceptItem`, `ACCEPT_RELAY_CAPACITY`, `relay_accept_item`, the acceptor-thread join, the unconstructible `AcceptOutcome::Closed` variant and the per-family accept warning go with them. Upstream has the same single-loop shape: `socket.c start_accept_loop()` runs one `select(2)` over all listener fds in one process. Two behaviours the previous engine got from the OS scheduler are now explicit: - Fairness. The ready-listener scan starts one past the family served last, so a continuously busy family cannot starve its sibling. - One connection per poll. The accept loop reaps finished workers before each poll, so returning several connections from one poll would let stale `ConnectionGuard`s accumulate and spuriously trip `--max-connections`. Accept failures stay transient at every arm, mirroring `socket.c:593`'s `if (fd < 0) continue;`. The new test serves a connection from each of two listeners and asserts the process thread count is unchanged across the whole exchange - the fork precondition asserted directly rather than described. Serving from both is what makes the count meaningful; a count over an engine that accepted nothing would pass vacuously. The thread count is read from `/proc/self/task` on Linux and reported as zero on both sides elsewhere, so the assertion pins nothing rather than pretending to. Mutation-proven: pinning the scan to listener 0 (no rotation) makes it fail with "engine never delivered the pending connection" and leaves the other 56 accept-path tests green. daemon 1819/1819; fmt + clippy --all-targets --all-features clean on the pinned 1.88.0 toolchain.
oferchen
added a commit
that referenced
this pull request
Sep 6, 2026
…ceptor threads (#7717) PR #7681 collapsed the portable accept engines into one `PollAcceptEngine` that polls every listener fd from the accept thread, and deleted `MultiListenerEngine` along with its per-listener acceptor threads. Two comments were left behind describing the mechanism that no longer exists, and they sit on exactly the surface a reader consults to reason about whether the accept path is single-threaded. `bind_listeners_per_family` said the SO_REUSEPORT replicas were "each driven by its own acceptor thread". `accept_engine.rs` says the opposite four files away: "Both engines are single-threaded, so the accept path spawns no threads of [its] own." Only one of those can be true, and the engine's is the live one - `grep -rn "spawn" crates/daemon/src/daemon/sections/server_runtime/` finds exactly one production `thread::spawn`, the per-connection worker at connection.rs:224. `RuntimeOptions::acceptor_threads` carried a second claim that is now wrong in a way an operator can act on: that the replicas distribute "inbound connection load across CPUs". They do not. The kernel still chooses which socket receives each connection, but one thread accepts them all, so the directive spreads accept queues rather than work. Setting it above 1 buys no CPU parallelism today. Stating that plainly is better than leaving a performance promise the engine cannot keep. Both replacements name the reason the engine is single-threaded rather than just asserting it: `platform::session_fork` may only be called from a single-threaded accept path, because a child forked from a multithreaded parent can deadlock on an allocator lock held by a thread that does not exist in the child. That is the constraint the deleted threads violated, and it is why these comments must not drift back. Measured, not inferred - a daemon built from a tree carrying the current engine, dual-stack with `acceptor threads = 3`: /proc/<pid>/task -> 1 thread /proc/<pid>/fd -> 6 listener sockets Six listener fds, one thread. A build predating #7681 reports 4 threads for the same config, which is what makes this a comment defect rather than a behaviour one. Comment-only: no code, no test, and no behaviour changes.
oferchen
added a commit
that referenced
this pull request
Sep 8, 2026
…#7745) The eleven docs that cite SIGNAL_CHECK_INTERVAL were line-accurate when written, but the code moved twice since: PR #5908 lowered the single-listener WouldBlock sleep from 500ms to 50ms and deleted the constant, and PR #7681 replaced accept-side sleep-polling with a readiness wait (poll(2) in PollAcceptEngine, kevent(2) behind the opt-in macos-kqueue feature in KqueueAcceptEngine) in server_runtime/accept_engine.rs. Bring the living documents back in line with the tree: - kqueue-pipeline-audit: mark the two daemon-accept surface rows migrated (KQ-S.1 landed via #6217/#7681) instead of describing the deleted sleep-poll loops as the current implementation. - daemon-tpc-benchmark-plan: bound SIGTERM-to-drain latency by the accept engine's 50ms/100ms signal-check cadence, and mark tracker #1683 completed. - daemon-event-loop-multiplexing: add a status note to the follow-up list and tick #1683, which #5908/#7681 closed. - daemon-thread-per-connection-scalability: mark tracker #1683 completed in the references list. - dis-4a, dis-3, daemon-coldstart-profile, daemon-coldstart-rebench: record that DIS-4.a R1 has since landed, next to the sections that rank it as the top open bottleneck; retarget the DIS-2 file index entry that still pointed the constant at listener.rs. The audit bodies keep their historical line citations: they describe the tree that was measured. daemon-thread-per-connection.md, async-daemon-listener.md (pinned 'Last verified: 2026-05-01'), and daemon-async-listener-rfc.md are left untouched as dated snapshots.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
refactor(daemon): one poll(2) accept engine, so the accept path holds no threads
The daemon had two portable accept engines.
SingleListenerEnginepolled onelistener from the accept thread;
MultiListenerEngine- selected whenever morethan one address was bound, which is the Linux dual-stack default - spawned one
acceptor thread per listener and relayed accepted sockets back over a bounded
mpsc channel.
That thread-per-family shape is a correctness problem, not just extra
machinery. Per-connection daemon state includes process-wide syscalls -
chroot(2)andsetuid(2)above all - so isolating it per connection meansforking the session, and
platform::session_forkdocuments that callers mustfork from a single-threaded accept path: a child forked from a multithreaded
parent can deadlock on an allocator lock held by a thread that does not exist
in the child. The acceptor threads made that fork illegal on exactly the
topology daemons default to.
poll(2)already takes a slice of fds, so one wait covers every listener fromthe caller's thread. Both engines collapse into
PollAcceptEngine, and thempsc relay,
AcceptItem,ACCEPT_RELAY_CAPACITY,relay_accept_item, theacceptor-thread join, the unconstructible
AcceptOutcome::Closedvariant andthe per-family accept warning go with them. Upstream has the same single-loop
shape:
socket.c start_accept_loop()runs oneselect(2)over all listenerfds in one process.
Two behaviours the previous engine got from the OS scheduler are now explicit:
a continuously busy family cannot starve its sibling.
poll, so returning several connections from one poll would let stale
ConnectionGuards accumulate and spuriously trip--max-connections.Accept failures stay transient at every arm, mirroring
socket.c:593'sif (fd < 0) continue;.The new test serves a connection from each of two listeners and asserts the
process thread count is unchanged across the whole exchange - the fork
precondition asserted directly rather than described. Serving from both is what
makes the count meaningful; a count over an engine that accepted nothing would
pass vacuously. The thread count is read from
/proc/self/taskon Linux andreported as zero on both sides elsewhere, so the assertion pins nothing rather
than pretending to.
Mutation-proven: pinning the scan to listener 0 (no rotation) makes it fail
with "engine never delivered the pending connection" and leaves the other 56
accept-path tests green.
daemon 1819/1819; fmt + clippy --all-targets --all-features clean on the pinned
1.88.0 toolchain.