Skip to content

fix(test): run --source error test non-interactively to prevent a PTY wedge - #3161

Merged
max-sixty merged 5 commits into
mainfrom
fix-flaky-source-error-pty
Jun 23, 2026
Merged

max-sixty merged 5 commits into
mainfrom
fix-flaky-source-error-pty

Conversation

@max-sixty

@max-sixty max-sixty commented Jun 22, 2026 •

Copy link
Copy Markdown
Owner

Problem

unix_tests::test_source_flag_forwards_errors (rstest over bash/zsh/fish in tests/integration_tests/shell_wrapper.rs) flaked in CI by timing out at the 180s nextest slow-timeout — a hang, not an assertion failure.

Root cause

The test ran bash -i / zsh -i inside a PTY. An interactive shell does job-control initialization at startup: it checks whether it owns the terminal's foreground process group, and if it decides it doesn't, it sends its own process group SIGTTIN and stops — waiting to be brought to the foreground, which the test harness never does. A stopped shell produces no output and never exits, so the PTY read blocks until the harness kills the run.

The evidence fits this and little else: the hang was macOS-only, the shell never exited, and the captured output was empty — so it stopped at job-control init, before running the -c script at all. The trigger is a rare startup race under heavy parallel load; it never reproduced locally across thousands of runs, but CI hit it.

Fix

test_source_flag_forwards_errors asserts exactly one thing: that the --source branch forwards wt's unrecognized subcommand error. It never backgrounds a job and never checks for job-control messages, so it doesn't need an interactive shell. Running it non-interactively (no -i) removes the job-control path — and the wedge — structurally, not probabilistically.

exec_in_pty_interactive gains a sibling exec_in_pty_shell(.., interactive: bool); the interactive wrapper is unchanged for the tests that genuinely verify job-control suppression (e.g. test_zsh_no_job_control), which still pass interactive = true. The error-passthrough assertions and the shared snapshot are unchanged.

The net diff is ~40 lines in one file. (An earlier revision of this PR bounded the PTY read and retried the wedged shell — recovering from the timeout rather than preventing it; that was reverted in favor of this structural fix.)

Tests

All three cases (bash/zsh/fish) pass — verified 15× consecutively, plus the full pre-merge gate (4044 tests, all lints and doctests).

This was written by Claude Code on behalf of max

`test_source_flag_forwards_errors` flaked by timing out at the 180s nextest
slow-timeout (a hang, not an assertion failure). The Unix `read_pty_output`
waited unboundedly: `read_to_string` (master EOF) then `child.wait()`. The
master only EOFs when the session-leader shell exits — on macOS the kernel
revokes the tty on session-leader exit, so a lingering grandchild can't starve
the read; the only way to hang is the interactive shell itself never exiting,
which a job-control race under parallel load can cause (the shell ends up
stopped, `T`).

Detect completion causally via `child.try_wait()`, bounded by a 60s fallback
that kills a wedged shell (exit 124), then a bounded post-exit drain. The
shared `wait_for_child_or_kill` / `drain_remaining` helpers harden both PTY
readers — `read_pty_output` and `prompted_pty_interaction` had the same
unbounded pattern — so neither can hang on a non-terminating shell. Covers all
three shell cases; error-passthrough coverage is unchanged.

Adds `test_pty_read_recovers_from_wedged_shell`, which drives a genuinely
stopped shell (`kill -STOP "$$"`) and asserts bounded recovery with output
captured.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

@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 fix to the two shared readers looks correct: detecting completion by the child's exit (with a try_wait poll + bounded SIGKILL fallback) rather than master EOF is the right inversion, the FIFO channel preserves output ordering, and drain_remaining breaking on Disconnected is strictly safer than the old reader_thread.join() (which blocked forever on the Linux lingering-slave case). The regression test reproduces the wedge deterministically. Nice writeup, too.

One same-pattern-elsewhere observation, not blocking: exec_bash_truly_interactive in tests/integration_tests/shell_wrapper.rs still carries the exact unbounded shape this PR is removing —

let status = child.wait().unwrap();
let buf = reader_thread.join().unwrap();  // read_to_string to EOF

— and it's arguably the most exposed call site, since it deliberately runs bash -i with job control enabled (env bash --norc --noprofile -i) to exercise job-control notifications. That's the same interactive-shell + job-control condition the PR identifies as the SIGTTOU-stop trigger. Its one caller (wt switch --create bash-job-test at the prompt) could wedge the same way and burn the full 180s harness timeout. Routing it through wait_for_child_or_kill + drain_remaining (it would need the reader switched from read_to_string to the channel shape) would close that gap.

The non-interactive inline readers elsewhere in the file (the -c invocations that read_to_string + child.wait()) are lower risk since -c shells don't enable job control, so I'd leave those.

Intentional to scope this PR to the two shared readers, or worth folding exec_bash_truly_interactive in as well?

max-sixty and others added 2 commits June 22, 2026 12:29
The first pass bounded read_pty_output so a wedged shell couldn't hang to
the 180s harness timeout, and CI then reproduced the flake in its honest
form: with the bound in place, test_source_flag_forwards_errors::case_1
(bash) ran exactly 60s and failed on EMPTY output. The interactive shell
wedges *before producing any output* (a job-control startup race leaves it
stopped, T), so bounding the hang isn't enough — the harness must recover.

- exec_in_pty_interactive now re-runs on a wedge (exit PTY_WEDGE_EXIT_CODE
  / 124 from the bounded kill), via retry_pty_on_wedge: 3 attempts, each
  bounded at 30s, well under the 180s harness limit. A startup wedge means
  nothing ran, so re-running is safe and a fresh attempt starts cleanly —
  the detect-and-recover feedback path the harness lacked.
- The bash arm now passes --norc --noprofile, matching the zsh arm. bash
  was the only interactive shell here not isolating its rc files (it
  sourced the host ~/.bashrc under bash -i) — a hermeticity bug and a
  plausible startup-hang source, and bash is the arm that flaked.
- read_pty_output takes an explicit wedge_timeout (30s interactive, 60s
  non-interactive); PTY_WEDGE_EXIT_CODE names the kill marker.

Adds test_retry_pty_on_wedge to pin the retry contract.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
exec_bash_truly_interactive still did `child.wait()` after sending `exit`,
the same unbounded wait the rest of this change removed — and it runs
`bash --norc --noprofile -i` with job control, so it's exposed to the same
startup wedge. Route it through the now-public wait_for_child_or_kill so a
wedged shell is killed and the run returns instead of hanging; the reader
thread EOFs once the child is gone, so the join still completes.

No unbounded wait on a PTY child remains in the harness.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…ing from it

Supersedes the bound-read + retry approach in the previous three commits on
this branch. Those recovered from the wedge (kill the stuck shell, re-run)
rather than preventing it, and added ~325 lines plus new public PTY API for a
flaky-test fix. This fully reverts tests/common/pty.rs to its prior state.

Root cause of the flake: `test_source_flag_forwards_errors` ran `bash -i` /
`zsh -i` in a PTY. An interactive shell does job-control initialization at
startup and, if it decides it isn't the terminal's foreground process group,
sends its own process group SIGTTIN and stops — producing no output and never
exiting. Under parallel load that startup race occasionally wedged the shell
until the 180s nextest slow-timeout killed the run (macOS-only, empty output,
never exits: it stopped before running the script at all).

The test only checks that the `--source` branch forwards wt's "unrecognized
subcommand" error; it never asserts on job-control output, so it doesn't need
an interactive shell. Run it non-interactively and the job-control path — and
the wedge — is gone structurally, not bounded.

`exec_in_pty_interactive` gains a sibling `exec_in_pty_shell(.., interactive)`;
the interactive wrapper is unchanged for the tests that do verify job-control
suppression (e.g. test_zsh_no_job_control).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@max-sixty max-sixty changed the title fix(test): bound PTY read on child exit so a wedged shell can't hang fix(test): run --source error test non-interactively to prevent a PTY wedge Jun 22, 2026
@worktrunk-bot

Copy link
Copy Markdown
Collaborator

This flake just recurred on main — the collect affected coverage (macos) job in run 27983873806 timed out on exactly this test:

TERMINATING [>180.000s] integration_tests::shell_wrapper::unix_tests::test_source_flag_forwards_errors::case_1
TIMEOUT     [ 180.004s] integration_tests::shell_wrapper::unix_tests::test_source_flag_forwards_errors::case_1

Same shape as this PR's diagnosis: macOS-only, 180s nextest slow-timeout (a hang, not an assertion). The triggering commit (9e8d461, the actions/checkout v6→v7 bump) is unrelated to the test, confirming this is the PTY job-control wedge rather than a regression. Landing this PR should remove the flake.

@max-sixty
max-sixty merged commit 3fb96de into main Jun 23, 2026
34 checks passed
@max-sixty
max-sixty deleted the fix-flaky-source-error-pty branch June 23, 2026 00:35
max-sixty added a commit that referenced this pull request Jun 23, 2026
…3165)

Bounds the `wt config show` zsh compinit probe with a 2s kill-on-timeout (the
only un-bounded interactive shell spawn in production), matching its twin
`shell::detect_zsh_compinit`. Also isolates several tests from host environment:
`TZ=UTC` in the statusline helper, `--no-config-file` on the nu probes, and the
`wt --help` / `git log` spawns routed through the isolated `wt_command()` /
`repo.git_command()` helpers.

The macOS PTY-test hang that prompted the audit is fixed structurally in #3161.

> _This was written by Claude Code on behalf of max_

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
max-sixty added a commit that referenced this pull request Jun 23, 2026
## Problem

The bash arm of `exec_in_pty_shell`
(`tests/integration_tests/shell_wrapper.rs`) was the only shell arm
without rc-file isolation. zsh sets `ZDOTDIR=/dev/null` + `--no-rcs`, nu
sets `--no-config-file`, and the sibling `exec_bash_truly_interactive`
already passes `--norc --noprofile` — but the bash arm did not. Because
the PTY harness sets `HOME` to the real home, interactive `bash -i`
sourced the host's `~/.bashrc`, leaking host-specific startup output
into the captured PTY snapshots.

## Fix

Add `--norc --noprofile` to the bash arm, mirroring the zsh/nu arms and
`exec_bash_truly_interactive`. It's orthogonal to job control (that's
driven by `-i`), so the job-control leak-detection tests
(`test_*_no_job_control*`) still exercise the interactive path; it's a
no-op for the non-interactive `bash -c` path used by
`test_source_flag_forwards_errors`.

Follow-up to #3161 / #3165, which fixed the macOS PTY-test hang and
isolated several other tests but left this arm untouched.

## Tests

All 88 `shell_wrapper` tests pass, plus the full pre-merge gate (4102
tests, all lints and doctests).

> _This was written by Claude Code on behalf of max_

Co-authored-by: Claude Opus 4.8 (1M context) <[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