Skip to content

Run internal detached ops at nice 19 - #2130

Merged
max-sixty merged 3 commits into
mainfrom
renice-internal-ops
Apr 12, 2026
Merged

max-sixty merged 3 commits into
mainfrom
renice-internal-ops

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

wt remove's background rm -rf and sweep_stale_trash were competing with foreground work for CPU/IO. This extends the renicing already done for step_copy_ignored (see src/copy.rs::lower_process_priority) to the detached-spawn paths: spawn_detached and spawn_detached_exec now wrap the child in nice -n 19 -- when hook_log is HookLog::Internal. User hooks keep running at normal priority.

The nice value is inherited across fork/exec, so wrapping the outer sh (for the shell-based path) or the target binary directly (for the exec path) is enough — the backgrounded work inherits it.

nice is POSIX and shipped in base macOS/Linux; a missing binary would fail the spawn, matching the existing renice assumption in copy.rs. Windows is left on its own priority mechanism (not wired up here).

A follow-up is under investigation: macOS has taskpolicy -b which also throttles I/O (IOPOL_THROTTLE) rather than just CPU priority. If the research lands in favor of it, the single low_priority_command helper is the one place to swap the primitive.

max-sixty and others added 2 commits April 12, 2026 14:18
`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]>
worktrunk-bot
worktrunk-bot previously approved these changes Apr 12, 2026

@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.

codecov/patch dropped below the project floor (95.45% < 96.12%) because low_priority_command is untested — the existing tests don't exercise spawn_detached_unix directly, so the helper's two branches are unobserved. The surrounding spawn_detached_unix body is already uncovered on main, so any new executable line in there sinks the patch average.

A small unit test against Command::get_program() / Command::get_args() closes the gap cheaply:

#[cfg(unix)]
#[test]
fn test_low_priority_command() {
    use std::ffi::OsStr;

    // low_priority=true wraps in `nice -n 19 --`
    let cmd = low_priority_command("sh", true);
    assert_eq!(cmd.get_program(), "nice");
    let args: Vec<&OsStr> = cmd.get_args().collect();
    assert_eq!(
        args,
        &[OsStr::new("-n"), OsStr::new("19"), OsStr::new("--"), OsStr::new("sh")]
    );

    // low_priority=false runs the program directly
    let cmd = low_priority_command("sh", false);
    assert_eq!(cmd.get_program(), "sh");
    assert_eq!(cmd.get_args().count(), 0);
}

Happy to push this as a follow-up commit if you'd like.

@worktrunk-bot
worktrunk-bot dismissed their stale review April 12, 2026 21:38

codecov/patch failed — posted coverage analysis

Cover both branches via `Command::get_program()` and `Command::get_args()`
to restore codecov/patch above the project floor.

Co-authored-by: Claude <[email protected]>

@max-sixty max-sixty left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Added the helper test as suggested. Thanks for catching the coverage gap.

@max-sixty
max-sixty merged commit 26242f4 into main Apr 12, 2026
26 checks passed
@max-sixty
max-sixty deleted the renice-internal-ops branch April 12, 2026 21:51
max-sixty added a commit that referenced this pull request Apr 12, 2026
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.

## Changes

- **`copy.rs::lower_process_priority`** — `taskpolicy -b -p <pid>` on
macOS; `renice -n 19 -p <pid>` plus 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 and 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.

## Evidence

`man 2 setpriority` on 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-specific
`PRIO_DARWIN_BG` bit is what makes the difference — the POSIX
`PRIO_PROCESS` path that `nice`/`renice` use 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 19` wrapping for internal
detached ops); that baseline is preserved for Linux while macOS gets the
I/O-throttling upgrade.

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

---------

Co-authored-by: Claude <[email protected]>
max-sixty added a commit that referenced this pull request Apr 13, 2026
The runtime check (WORKTRUNK_NU_WRAPPER_VERSION, nu_wrapper_status,
warn_outdated_nushell_wrapper_once) only catches drift for wrappers that
already export WORKTRUNK_SHELL=nu — i.e. anyone who has installed a
version-1+ wrapper. Pre-versioning wrappers (everyone before this branch
lands) don't set WORKTRUNK_SHELL at all, so they're invisible to the
check. That's exactly the cohort that most needs nudging after the
split-directive-file migration.

Strip it in favor of the TODO(2026-05) in compute_directive_mode, which
catches pre-versioning nushell wrappers via the legacy env-var path.
For ongoing drift detection after May, `wt config show` already does
content comparison on wrapper-based shells (fish and nushell) and
prints "Outdated shell extension" — user-initiated, but sufficient.

Keeps the merge of #2118 / #2130.

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