Skip to content

docs(fsmonitor): correct the sweep's cost model after the batched lsof - #3852

Merged
max-sixty merged 4 commits into
mainfrom
batch-lsof-sweep
Aug 18, 2026
Merged

max-sixty merged 4 commits into
mainfrom
batch-lsof-sweep

Conversation

@max-sixty

@max-sixty max-sixty commented Aug 18, 2026 •

Copy link
Copy Markdown
Owner

#3581 batched the fsmonitor sweep into a single lsof over the whole PID set, but left two places still describing the per-daemon cost it removed. Both now read as if wt remove still pays N × 50 ms after its last message:

  • run_internal_sweep's docstring in src/commands/process.rs — "one sequential ~50ms lsof per daemon (measured: 115 daemons → ~5.8s of post-output latency)"
  • benches/CLAUDE.md § Recording wt remove / wt step prune staging — the same model, plus a wt-perf recipe pointing at lsof -a -p … child spans that a batched sweep no longer produces

Both are rewritten to what the sweep now does: one pgrep plus one lsof, flat in the machine-wide daemon count. The docstring also names the rest of the exit delay, which the old text attributed entirely to enumeration — a git worktree list for any live daemon, and a SIGTERM→SIGKILL wait bounded by REAP_KILL_DEADLINE for a classified orphan. Both are flat in the daemon count too. The debug line the benches doc quotes is updated to match enumerate_daemons verbatim.

Also adds batched_lsof_drops_lines_outside_a_parsable_record, covering socket lines with no parsable p line above them — leading output, and lines under an unparsable pBAD. Attaching those to a neighbouring record would fabricate a class-1 orphan and signal a live daemon. The neighbouring tests cover per-PID splitting, vanished PIDs, and empty output, but not this one.

This was written by Claude Code on behalf of max-sixty

The internal sweep after `wt remove` ran one `lsof -a -p <pid>` per
candidate PID, and the candidate set is machine-wide: every repo whose
worktrees have been touched contributes a `git fsmonitor--daemon`. At
~50 ms a spawn that appended `N x 50 ms` before exit, while the shell
wrapper waited on the process.

`lsof -p` takes a comma-separated PID list, so one call covers the whole
set. `daemons_from_lsof_batch` splits the `-F` output back into per-PID
records on the `p<pid>` lines and hands each to the existing classifier,
so orphan classification is unchanged. Lines outside a parsable record
are dropped rather than attached to a neighbour: crediting one process's
socket to another would reap a live daemon.

Exit status goes unchecked here. With a multi-PID set `lsof` reports
failure when any one PID vanished mid-scan, while the survivors' records
on stdout stay complete; a vanished PID emits no record and is never
classified.
main landed the same optimization in a0c1b66 ("perf(fsmonitor): resolve
all daemons in one lsof spawn"), so the branch's `src/git/fsmonitor.rs`
implementation is dropped in favour of main's. Main's is the better of the
two: it asks `lsof` for `-F pn` rather than relying on the `p` field being
emitted unrequested, and its splitter streams records instead of buffering
every one.

What main left undone survives:

- The two doc sites still described the old per-daemon `lsof`, promising
  `N x 50 ms` of post-output latency that no longer happens.
- `daemons_from_batched_lsof` handles lines that sit under no parsable `p`
  line, and says so in a comment, but nothing pinned it.

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs now match the batched sweep — enumerate_daemons is a pgrep plus one lsof -a -p <list>, and the debug string is quoted verbatim. The new test's premise checks out against daemons_from_batched_lsof: the leading nfsmonitor--daemon.ipc and the one under pBAD both land with no record open, so pid 301's record holds only n->0x1 and daemon_from_lsof_stdout drops it for want of IPC_SOCKET_NAME — misattributing either line would have made 301 a socket-less class-1 orphan. cargo test --lib git::fsmonitor::tests::batched passes locally (4/4).

One thing the rewrite tightened past what the code does: the enumeration is two spawns, but the exit delay isn't. See the inline note on run_internal_sweep.

Comment thread src/commands/process.rs Resolved
Enumeration is the two spawns; the reap adds a `git worktree list` for any
live daemon and a bounded SIGTERM→SIGKILL wait for a classified orphan.
Both are flat in the daemon count, so the claim the rewrite was making
still holds — it just wasn't the whole exit delay.
@max-sixty
max-sixty merged commit 03ab088 into main Aug 18, 2026
42 checks passed
@max-sixty
max-sixty deleted the batch-lsof-sweep branch August 18, 2026 23:30
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.

2 participants