Repository navigation
feat(priority): use taskpolicy -b on macOS for I/O throttling - #2133
Merged
Merged
Conversation
`wt remove`'s background `rm -rf` and `sweep_stale_trash` were competing with foreground work for CPU/IO. Extends the treatment already applied to `step_copy_ignored` (see `src/copy.rs::lower_process_priority`) to internal detached spawns: `spawn_detached` and `spawn_detached_exec` now wrap the child in `nice -n 19 --` when `hook_log` is `HookLog::Internal`. User hooks run at normal priority. `nice` is POSIX and shipped in base macOS/Linux; a missing binary would fail the spawn, matching the existing `renice` assumption. Windows is unchanged — priority there is a separate mechanism. Co-authored-by: Claude <[email protected]>
Wrap `sh` / the target binary in `nice -n 19` when `spawn_detached` and `spawn_detached_exec` are called for `HookLog::Internal`. Covers the background `rm -rf` behind `wt remove` and the trash sweep. User hooks are unaffected — they continue to run at normal priority so their visible work isn't slowed. Co-Authored-By: Claude <[email protected]>
On Darwin, `nice`/`renice` only affect Mach CPU scheduling priority — disk and network I/O throttling require `PRIO_DARWIN_BG`, set via `setpriority(PRIO_DARWIN_PROCESS, …)` or `taskpolicy -b`. For the workloads we lower priority on (reflink-fallback copy in `step_copy_ignored`, background `rm -rf` behind `wt remove`, trash sweep), disk I/O is the dominant cost — the previous `nice 19` / `renice 19` calls left it un-throttled on APFS. - `copy.rs::lower_process_priority`: `taskpolicy -b -p <pid>` on macOS, `renice -n 19 -p <pid>` + best-effort `ionice -c 3 -p <pid>` on Linux. - `process.rs::low_priority_command`: wraps the child in `/usr/sbin/taskpolicy -b` on macOS, `nice -n 19 --` elsewhere. Policy is inherited by children so the detached shell + its grandchildren (the actual `rm -rf`) all run at lowered priority. Shelled out rather than calling the syscalls directly to stay within `forbid(unsafe_code)`. `taskpolicy` ships at `/usr/sbin/taskpolicy` in every macOS base install; a missing binary fails the spawn, matching the existing tolerance model. See `man 2 setpriority` (PRIO_DARWIN_BG section) and `man taskpolicy` for the underlying mechanism. Co-Authored-By: Claude <[email protected]>
# Conflicts: # src/commands/process.rs
worktrunk-bot
approved these changes
Apr 12, 2026
worktrunk-bot
previously approved these changes
Apr 12, 2026
`low_priority_command` is `#[cfg(unix)]`, so the intra-doc link in `spawn_detached`'s docstring (which is platform-neutral) fails rustdoc resolution on Windows with `-Dwarnings`. Drop the link; the surrounding text already names the function. Co-Authored-By: Claude <[email protected]>
worktrunk-bot
left a comment
Collaborator
There was a problem hiding this comment.
The Windows doc build fails with error: unresolved link to \low_priority_command`atsrc/commands/process.rs:262—low_priority_commandis#[cfg(unix)], so on Windows the function doesn't exist and rustdoc can't resolve the intra-doc link from the unconditionally-compiled spawn_detached`. Dropping the brackets (keeping it as inline code) is the least-invasive fix.
Happy to push this fix if you'd like.
worktrunk-bot
dismissed
their stale review
April 12, 2026 22:25
CI failed — Windows rustdoc cannot resolve the new [low_priority_command] intra-doc link (function is cfg(unix))
worktrunk-bot
approved these changes
Apr 12, 2026
max-sixty
added a commit
that referenced
this pull request
Apr 13, 2026
…2140) Collapses the two priority-lowering helpers (`copy::lower_process_priority` and `commands::process::low_priority_command`) into a single `worktrunk::priority` module. The module-level doc-comment now documents the policy (macOS `taskpolicy -b` vs Linux `ionice`+`nice`), the `forbid(unsafe_code)` rationale for shelling out, and the two forms in which we apply it. Follow-up to #2133. Two small behaviour notes: - The Linux child-wrapping path now does best-effort `ionice -c 3 -- nice -n 19 --` (was just `nice -n 19`), matching what `taskpolicy -b` already covers on macOS. `ionice` availability is probed once per process via `which`. `ionice` ships in `util-linux` on every mainstream distro and is enabled in Alpine's busybox, so the `nice`-only fallback is only hit on stripped-down environments (distroless, minimal busybox, etc.). - `copy.rs`'s self-lowering path now also gates `ionice` behind the same `which` probe, so we don't unconditionally fork `ionice` on hosts without it. The `command()` function delegates Linux branching to a small `linux_low_priority_command(program, has_ionice)` helper so both `has_ionice` arms are unit-testable regardless of whether the CI runner has `ionice` installed. Tests cover the macOS `taskpolicy` wrap, both Linux arms, the non-unix no-op, and `lower=false` passthrough. One canonical policy in one place. > _This was written by Claude Code on behalf of Maximilian_ --------- Co-authored-by: Claude <[email protected]>
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.
On Darwin,
nice/reniceonly affect Mach CPU scheduling priority — disk and network I/O throttling requirePRIO_DARWIN_BG, set viasetpriority(PRIO_DARWIN_PROCESS, …)ortaskpolicy -b. For the workloads we lower priority on (reflink-fallback copy instep_copy_ignored, backgroundrm -rfbehindwt remove, trash sweep), disk I/O is the dominant cost — the previousnice 19/renice 19calls left it un-throttled on APFS.Changes
copy.rs::lower_process_priority—taskpolicy -b -p <pid>on macOS;renice -n 19 -p <pid>plus best-effortionice -c 3 -p <pid>on Linux.process.rs::low_priority_command— wraps the child in/usr/sbin/taskpolicy -bon macOS,nice -n 19 --elsewhere. Policy is inherited by children, so the detached shell and its grandchildren (the actualrm -rf) all run at lowered priority.Shelled out rather than calling the syscalls directly to stay within
forbid(unsafe_code).taskpolicyships at/usr/sbin/taskpolicyin every macOS base install; a missing binary fails the spawn, matching the existing tolerance model.Evidence
man 2 setpriorityon macOS: "When a thread or process is in a background state the scheduling priority is set to the lowest value, disk IO is throttled (with behavior similar to using setiopolicy_np(3) to set a throttleable policy), and network IO is throttled for any sockets opened after going into background state." The Darwin-specificPRIO_DARWIN_BGbit is what makes the difference — the POSIXPRIO_PROCESSpath thatnice/reniceuse doesn't touch I/O policy.man taskpolicy: "uses the setiopolicy_np(3) and setpriority(2) APIs … All children of the specified program also inherit these policies."This builds on #2130 (which added
nice 19wrapping for internal detached ops); that baseline is preserved for Linux while macOS gets the I/O-throttling upgrade.