Skip to content

refactor(daemon): one poll(2) accept engine, so the accept path holds no threads - #7681

Merged
oferchen merged 1 commit into
masterfrom
fix/daemon-chroot-per-connection-fork
Sep 5, 2026
Merged

oferchen merged 1 commit into
masterfrom
fix/daemon-chroot-per-connection-fork

Conversation

@oferchen

@oferchen oferchen commented Sep 5, 2026

Copy link
Copy Markdown
Owner

refactor(daemon): one poll(2) accept engine, so the accept path holds 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
    ConnectionGuards 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.

… 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
oferchen merged commit acaa977 into master Sep 5, 2026
75 checks passed
@oferchen
oferchen deleted the fix/daemon-chroot-per-connection-fork branch September 5, 2026 03:05
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant