Skip to content

perf(shell-exec): replace 10ms poll with signal listener thread - #2537

Merged
max-sixty merged 2 commits into
mainfrom
poll-fix
May 3, 2026
Merged

max-sixty merged 2 commits into
mainfrom
poll-fix

Conversation

@max-sixty

@max-sixty max-sixty commented May 3, 2026 •

Copy link
Copy Markdown
Owner

Cmd::stream's forward_signals=true path was running a try_wait + thread::sleep(10ms) + signals.pending() loop, imposing a ~13ms wall floor on every alias and hook subprocess dispatch — ~70% of execute_shell_command time for short aliases.

Replace the loop with an event-driven shape: a listener thread blocks on Signals::forever() and forwards SIGINT/SIGTERM to the child; the main thread blocks on child.wait(). After wait returns, handle.close() unblocks the listener and we join() it (always, even on wait error — no thread leak).

First-signal-only semantics preserved via an Arc<AtomicI32> sentinel (0 = none, since POSIX signals start at 1) with compare_exchange. Memory ordering is Relaxed since JoinHandle::join provides the happens-before edge. Re-press escalation (SIGINT → SIGTERM → SIGKILL inside forward_signal_with_escalation) is unchanged, and the shared-pgroup branch keeps its tty-restore rationale.

Second commit consolidates child.wait() and its map_err so both forwarding and non-forwarding paths share one wait + one error-mapping line, and Windows takes the same line as Unix.

Empirical: wt step <trivial-alias> 30.4ms → 20.4ms via hyperfine (1.49× faster).

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

`Cmd::stream`'s `forward_signals=true` path was running a `try_wait` +
`thread::sleep(10ms)` + `signals.pending()` loop, imposing a ~13ms wall
floor on every alias and hook subprocess dispatch — ~70% of
`execute_shell_command` time for short aliases.

Replace with an event-driven shape: a listener thread blocks on
`Signals::forever()` and forwards SIGINT/SIGTERM to the child; the main
thread blocks on `child.wait()`. After wait returns, `handle.close()`
unblocks the listener and we `join()` it (always, even on wait error).

First-signal-only semantics preserved via an `Arc<AtomicI32>` sentinel
(0 = none, since POSIX signals are >= 1) with `compare_exchange`.
Memory ordering is `Relaxed` since `JoinHandle::join` provides the
synchronization edge. Re-press escalation logic
(`forward_signal_with_escalation`) is unchanged.

Empirical: `wt step <trivial-alias>` 30.4ms → 20.4ms (1.49× faster).

Co-Authored-By: Claude <[email protected]>
worktrunk-bot
worktrunk-bot previously approved these changes May 3, 2026
@worktrunk-bot
worktrunk-bot dismissed their stale review May 3, 2026 00:31

codecov/patch failing — likely false positive (the uncovered branches existed on main before the refactor, just at different line numbers), but per CLAUDE.md dismissing until the coverage gap is either addressed or confirmed as a false positive.

Extract `child.wait()` and its error mapping out of the
forward_signals if/else, then fold the listener-state setup into
`signals.map(|...|)` and teardown into `.and_then()`. One wait, one
map_err, no duplication. As a bonus this drops the separate
`#[cfg(not(unix))] let status = child.wait()...` arm — Windows now
takes the same `wait_result.map_err(...)?` line as Unix.

Co-Authored-By: Claude <[email protected]>
@max-sixty
max-sixty merged commit e8629d1 into main May 3, 2026
27 checks passed
@max-sixty
max-sixty deleted the poll-fix branch May 3, 2026 00:49
max-sixty added a commit that referenced this pull request May 3, 2026
Follow-up to #2537. `output/concurrent.rs::spawn_signal_forwarder` was
running the same kind of `pending()` + 25 ms sleep ticker that #2537
removed from `Cmd::stream`. Replace it with the same event-driven shape:
the listener thread blocks on `Signals::forever()`, and
`SignalForwarder::stop()` closes the signal-hook handle to unblock it
before joining.

Drops the `Arc<AtomicBool>` stop flag — the signal-hook handle now
serves both shutdown signaling and signal delivery. Two-press SIGKILL
semantics (impatient escalation on the second SIGINT) are preserved via
the local `seen_once` flag in the listener.

Latency under Ctrl-C improves from "up to 25 ms after the press" to "as
soon as signal-hook delivers the signal" — most useful for the second
SIGINT in the kill-all path. `test_alias_concurrent_second_sigint_kills`
still completes within its 3 s budget.

> _This was written by Claude Code on behalf of @max-sixty_

Co-authored-by: Claude <[email protected]>
@max-sixty max-sixty mentioned this pull request May 3, 2026
max-sixty added a commit that referenced this pull request May 3, 2026
Cuts v0.47.0. Highlights:

- **Integration detection fix** across `wt list`, `wt remove`, `wt
merge`, `wt step prune` for the local/upstream-diverged case (#2507,
#2513, #2515).
- **Faster shell-command dispatch** (~10ms per invocation, 1.49× on
trivial aliases) via event-driven signal forwarding (#2537, #2538), plus
parallel config load (#2543) and merged cold-start prewarm rev-parse
(#2541).
- **`wt switch <number>` suggests `pr:N` / `mr:N`** instead of just
`--create` (#2516).
- **Library API breaking changes**: `RefSnapshot` cutover (#2528, #2530)
and the `for-each` direct-exec change (#2465). Full list in
`CHANGELOG.md`.

See `CHANGELOG.md` for the complete entry.

> _This was written by Claude Code on behalf of @max-sixty_

---------

Co-authored-by: Claude <[email protected]>
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