Skip to content

fix(alias): share parent pgroup so interactive TUIs don't freeze - #2444

Merged
max-sixty merged 5 commits into
mainfrom
fix/alias-tty-pgroup-25024791076
Apr 28, 2026
Merged

max-sixty merged 5 commits into
mainfrom
fix/alias-tty-pgroup-25024791076

Conversation

@worktrunk-bot

@worktrunk-bot worktrunk-bot commented Apr 28, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Aliases with interactive children (wt switch's skim picker) froze with a blank screen because the alias child was put in a new process group; the kernel then raised SIGTTOU when the picker called tcsetattr on /dev/tty, stopping the child mid-render.
  • New Cmd::inherit_stdin() builder both inherits stdin and keeps the child in the parent's process group. execute_shell_command uses it for the interactive (no-stdin-payload) path, which covers aliases.
  • In shared-pgroup mode the signal listener forwards by PID via kill(child.id(), sig) so externally-delivered signals (e.g. kill -TERM <wt-pid>) reach the child — they otherwise stop at wt because the kernel only multicasts tty-initiated signals to the foreground pgroup. Single-shot, no SIGINT→SIGTERM→SIGKILL escalation: the caller chose the signal, and a premature SIGKILL would skip the child's tty restore (raw-mode reset, cursor-show) and leave the terminal wedged. WorktrunkError::ChildProcessExited { signal: Some(_) } accounting is preserved.
  • Module-level docstring at the top of shell_exec.rs summarizes the two Cmd::stream shapes (isolated vs shared-tty), how each interacts with the structured ChildProcessExited channel used by loop callers, and why detached/concurrent spawn paths isolate.

Hooks (which still get JSON-on-stdin via stdin_bytes) are unaffected — they go through the stdin_data/piped branch and never call inherit_stdin().

Test plan

  • New regression test test_alias_child_shares_parent_pgroup: spawns wt as its own pgroup leader, runs an alias that records its shell pgid via ps -o pgid= -p \$\$, asserts the recorded pgid equals wt's pid (i.e. the alias child shares wt's pgroup).
  • New regression tests test_alias_external_sigterm_reaches_child and test_alias_external_sigint_reaches_child: target wt with PID-only kill -<sig> (not the pgroup) and assert the alias child's signal trap fires. Without the PID-forwarding fix the test would block until the child's sleep 30 completes; with it, each settles in ~1.3s. Together they exercise both arms of forward_signal_to_pid.
  • Existing test_alias_concurrent_receives_sigint and test_alias_concurrent_second_sigint_kills still pass — concurrent aliases use a separate executor (output/concurrent.rs) that's unaffected by this change.
  • Full step_alias (70 tests) and user_hooks (108 tests) suites green locally.
  • Manual verification that wt sw (aliased to wt switch {{ args }}) now renders the picker instead of hanging.

Aliases that exec interactive children (e.g. `wt switch`'s skim picker)
froze with a blank screen: the alias child was put in a new process group
via `cmd.process_group(0)`, so when skim's tuikit called `tcsetattr` on
`/dev/tty` to enter raw mode the kernel raised SIGTTOU on the
background pgroup and stopped the child mid-render.

`Cmd::inherit_stdin()` now both inherits stdin and keeps the child in
the parent's process group. `execute_shell_command` uses it for the
interactive (no-stdin-payload) path, which covers aliases. The kernel
already delivers terminal signals to every process in the shared
foreground pgroup, so the new-pgroup isolation only mattered for
`killpg`-based teardown — redundant for interactive aliases.

The signal listener still runs in the shared-pgroup case so a
signal-derived child exit surfaces as `WorktrunkError::ChildProcessExited`
with `signal: Some(_)`; we just skip the no-op `killpg` call.

Closes the freeze reported in #406.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@worktrunk-bot worktrunk-bot mentioned this pull request Apr 28, 2026

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The fix is correct and the regression test directly exercises the invariant (alias child shares wt's pgroup). One concern with the description, no code blockers.

Wrong issue reference. "Closes the freeze reported in #406" — issue #406 is "Support shorthand" (closed, unrelated to TTY freezes). Merging will silently no-op because the issue is already closed, but the reference is misleading and pollutes the commit message in git log. Either drop the line or replace it with the actual report (e.g., a link to the comment thread or describe the user-facing symptom without an issue number).

Optional doc nit (not blocking). The doc comment for inherit_stdin and the stream() skip-block describe terminal signals as "already delivered to every process in the foreground pgroup." That is true for tty-initiated signals (Ctrl-C → SIGINT, Ctrl-Z → SIGTSTP, hangup → SIGHUP). It is not true for an external kill -TERM <wt-pid> — only wt receives it, and with the new code we no longer forward it (and can't, since the child isn't a pgroup leader so killpg(child_pid, …) would target a non-existent group or, worst case, an unrelated recycled pgid). Practical impact is small (interactive aliases aren't usually kill-ed externally), but the comment slightly overstates the equivalence — saying "tty-initiated signals" instead of "terminal signals" would be more precise.

The kernel only auto-delivers signals to every process in the foreground
pgroup for tty-initiated signals (SIGINT from Ctrl-C, SIGTSTP from
Ctrl-Z, SIGHUP on hangup). An external `kill -TERM <wt-pid>` only
reaches wt; with the new shared-pgroup path we no longer forward it.
Tighten the doc comments accordingly.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Both fair — addressed:

  • Wrong issue reference. Right, Support shorthand #406 is "Support shorthand", unrelated. I checked and there isn't an actual issue tracking this freeze that I can substitute, so I dropped the line (and the unverified KieranP attribution) from the PR description rather than pointing at something else. The commit message still has the bogus reference; I'll leave the published commit alone, but the PR body is the cleaner record.
  • "terminal" → "tty-initiated". Good precision call — kill -TERM <wt-pid> doesn't propagate, only the kernel's tty-initiated signals (SIGINT/SIGTSTP/SIGHUP) do. Tightened the three relevant doc/inline comments in 0516e52, and the inherit_stdin doc now also calls out the externally-delivered case explicitly.

max-sixty and others added 2 commits April 27, 2026 22:26
In shared-pgroup mode (`Cmd::stream()` with `.inherit_stdin()`), the
listener was skipping signal forwarding entirely on the assumption the
kernel would broadcast tty signals to the whole foreground pgroup.
That holds for Ctrl-C and SIGHUP, but `kill -TERM <wt-pid>` only
delivers to wt — leaving the alias child running indefinitely while
wt sat latched on `seen_signal`.

Forward via `kill(child.id(), sig)` instead of skipping. Single-shot,
no SIGINT→SIGTERM→SIGKILL escalation: the caller chose the signal,
and an unsolicited SIGKILL would skip the child's tty restore (raw-
mode reset, cursor-show) and leave the terminal wedged.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
The rationale for the two `Cmd::stream` shapes (isolated vs shared-tty)
and how each interacts with the kernel's foreground-pgroup signal
broadcast was scattered across three field/method docstrings. Pull a
single summary up to the module head so a reader can pick up the
model without reconstructing it from the call site.

No code change; refactors comments only.

Co-Authored-By: Claude Opus 4.7 <[email protected]>

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

codecov/patch failed at 95.34% (target 96.73%, the project baseline) — 41/43 patch lines hit, 2 uncovered.

Likely culprit: the SIGINT arm of forward_signal_to_pid in src/shell_exec.rs. test_alias_external_sigterm_reaches_child exercises the SIGTERM path (the SIGTERM match arm and the kill() call show as covered), but no test sends SIGINT through the shared-pgroup path, so the SIGINT match arm and likely one neighboring line stay at zero hits.

Practically, the SIGINT branch is narrow: kernel-broadcast Ctrl-C already reaches the child via the foreground pgroup, so forward_signal_to_pid(child_pid, SIGINT) only matters for an externally-delivered kill -INT <wt-pid> — uncommon for interactive aliases. Two reasonable responses:

  • Mirror test_alias_external_sigterm_reaches_child with a SIGINT variant to close the gap.
  • Accept the 2-line miss as the cost of carrying a defensive branch for a rare external-signal case.

Happy to push the SIGINT variant test if you prefer the first option. No prior approval to dismiss.

Mirrors test_alias_external_sigterm_reaches_child for SIGINT. PID-targeted
`kill -INT <wt-pid>` bypasses kernel pgroup broadcast, so the alias child
only receives SIGINT via wt's re-delivery path — closes the codecov gap on
the SIGINT match arm of `forward_signal_to_pid`.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Went with option 1 — added test_alias_external_sigint_reaches_child in b0479dd. Mirrors the SIGTERM variant: kill -INT <wt-pid> (PID-targeted, no pgroup broadcast) against an alias whose shell traps INT, asserts the trap marker appears. Passes locally alongside the existing SIGTERM and concurrent SIGINT tests; should land both uncovered lines.

@max-sixty
max-sixty merged commit 6f1974c into main Apr 28, 2026
23 checks passed
@max-sixty
max-sixty deleted the fix/alias-tty-pgroup-25024791076 branch April 28, 2026 14:23
max-sixty added a commit that referenced this pull request Apr 28, 2026
## Summary

Follow-up to #2444. The `forward_signals` docstring mentioned that the
listener "still runs" in the shared-pgroup case but didn't say what it
does — leaving readers to infer that signal-derived child exits surface
but no explicit forwarding happens. With #2444's PID forwarding for
externally-delivered signals, the doc lagged the implementation.

Two extra lines spelling out the actual behavior. No code change.

## Test plan

- [x] `cargo check` clean.
- [x] No code paths affected; doc-only.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 4.7 <[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