Skip to content

fix(tui): hold the screen's stop pending so a quick fg is never lost (red main) - #4752

Merged
devswha merged 4 commits into
mainfrom
fix/tui-fg-race
Nov 25, 2026
Merged

devswha merged 4 commits into
mainfrom
fix/tui-fg-race

Conversation

@devswha

@devswha devswha commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Fixes the race that turned main red in https://github.com/devswha/ovis/actions/runs/45634721151 (tui/host/restore.contract.test.ts "comes back after an interactive shell stopped the whole job and fg continued it"). The same race keeps failing other PRs' Integration job.

Cause (a real product race, not a test problem)

At a stop of the whole job (SIGTSTP to the process group, as a shell's job control sends), the launcher, the job's first process, stops at once by the default action. This is either the Bun launcher or the /bin/sh waiter it hands itself over to. bash reports the job stopped as soon as the launcher is. The screen, the launcher's child, stops a moment later: its handler first gives the terminal back and then sends itself SIGSTOP. An fg typed in between continues a screen that has not stopped yet. The screen's own SIGSTOP comes after it and leaves the screen stopped for good, while the job stays in the foreground. The failing CI logs show [1]+ Stopped and the prompt before the screen's leave bytes.

Fix (screen only; the launcher and the waiter are unchanged)

  • The screen now asks for its stop first and holds it pending: SIGTSTP is blocked at its default action and raised to the screen's own thread (pthread_sigmask, signal, raise through bun:ffi). Then it gives the terminal back and unblocks SIGTSTP. POSIX discards a pending stop signal when SIGCONT is sent. So an fg that comes any time after the terminal was given back cancels the stop instead of being lost to it. The screen hears that continue next and enters again.
  • The screen may run its handler only after the shell already continued the job, for example on a loaded machine. To cover that, a screen started by a launcher (it was given a hold file) checks whether that launcher, which stopped at the same signal, is still stopped or has a stop pending. On Linux this is read from /proc: the pending mask first, then each thread's state. On macOS it is read from proc_pidinfo. When the launcher runs again, the screen still gives the terminal back (the shell has reset the terminal meanwhile), discards its pending stop, and enters again at the continue it already got.
  • Without the C library the screen stops with SIGSTOP as before. So does a screen whose process group leads its session (a pty entry run on its own, as the herdr-core live contract runs it). Such a group is orphaned, and the system drops SIGTSTP's default stop there. Only SIGSTOP stops it, and no job-control shell sends an fg there. This was found by the herdr-core live contract job on af8d0bc: tui/loop.live.test.ts "a stop from outside ... a continue enters again" times out without it and passes with it (locally with hc 0.1.18).

The first version of this PR made the screen stop its whole process group and taught the launcher and the waiter to wait for it. Both reviews blocked it:

  • the waiter's TSTP trap could hang inside stty;
  • the waiter could report a trap's status instead of the screen's;
  • kill(0, SIGSTOP) could stop an invoking shell that shares the group.

That approach is fully reverted.

Tests

  • The restore contract test runs through both the shell waiter (the path ovis tui really takes) and the Bun launcher.
  • New deterministic cases hold the host fixture on a FIFO at an exact point, and fg is typed while it waits. The first point is after the leave bytes and before the stop. The second is after the job's SIGTSTP was sent and before the screen's handler ran. Each point is covered for both launchers. All 4 fail on the old code every time (runner image: 0 pass, 4 fail) and pass with the fix. The fresh(...) waits are for output and do not depend on speed.
  • Unit cases for the unix host: the terminal is given back before the stop, and the launcher check decides only whether the screen stops.

Validation

All runs were in the CI runner image (herdr-ci-runner:local, --cpus 8, 10 parallel workers × 10 runs, on a loaded P620):

runs failures
main d435813, the test as on main 100 3
main product code + new waiter-variant test 50 1
main product code + new FIFO-gated tests 1 run of 4 cases 4 of 4
this PR, all 6 related cases per run 100 (600 cases) 0
  • bun run check fast: green on f93fe81.
  • tui/loop.live.test.ts and tui/testing/with-core.contract.test.ts against hc 0.1.18: 62 pass.
  • bunx tsc --noEmit -p tui/tsconfig.json: clean.
  • bun test tui/host scripts/plugin-tui.test.ts tui/conventions.test.ts tui/loop.test.ts: 392 pass.
  • macOS (eamac, arm64, Bun 1.4.2, /bin/bash 3.2): all 6 related contract cases pass, including both FIFO-gated points, so the macOS pthread_sigmask constants and proc_pidinfo are exercised. tui/host/unix.test.ts and host.test.ts: 81 pass.

… quick fg is not lost

At a stop of the whole job (SIGTSTP from the shell), the launcher (or the shell
waiter it hands over to), the job's first process, stopped at once, while the
screen stopped itself a moment later, after giving the terminal back. The shell
reported the job stopped as soon as the launcher was, and an fg typed in between
reached the screen before its own stop, which then left it stopped for good.

The screen now says 'stopping' in its hold file and stops its whole process
group in one kill; the launcher and the waiter stop on SIGTSTP only when the
hold file says the screen will not do it (it holds nothing). The restore
contract test now runs through both the waiter and the Bun launcher.
@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Explain Change →

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Fixed a freeze that could occur when quickly resuming the TUI after stopping a shell job.
    • Improved terminal restoration and job-stop handling across launcher and waiter modes.

The host now records a stopping hold state before terminal restoration and can stop the whole process group. The launcher and waiter coordinate SIGTSTP handling with the hold-file state. The changelog records a fix for a freeze after an immediate fg following a shell stop.

Changes: TUI job-control stop coordination

Layer / File(s) Summary
Host stop state and terminal restoration
tui/host/guard.ts, tui/host/hold.ts, tui/host/unix.ts, tui/host/host.test.ts, tui/host/unix.test.ts
The host adds a stopping hold state, writes it during external-stop handling, restores the terminal, and stops process group 0 when a launcher hold is active. The tests check the written state and stop signal.
Launcher and waiter signal coordination
scripts/plugin-tui.ts, scripts/plugin-tui-waiter.ts, scripts/plugin-tui.test.ts, tui/testing/launcher-fixture.ts, tui/host/restore.contract.test.ts, changelog.d/tui-fg-after-stop.md
The launcher stops itself on SIGTSTP only when the screen does not stop the job. The waiter trap checks the hold-file state before stopping the waiter shell. Tests cover launcher and waiter modes and signal-listener cleanup. The changelog records the freeze fix.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix · Severity of issue fixed: Low

Possibly related PRs

  • devswha/ovis#3855: This PR changes job-control behavior in the TUI launcher introduced by #3855.
Merge Risk: Minimal · previous review be04c

This change fixes the job-control race where a quick fg after a shell stop could leave the TUI frozen. No concrete merge-blocking defect remains. A very narrow timing window for a second stop immediately after fg is an awareness item and does not block merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description follows the template's two required sections, Change and Validation. The Change section explains the race, the cause, the fix, and why the earlier approach was reverted. The Validation…
Title check Passed The title names the main change: a fix in the tui job-control path so a quick fg is not lost. The changeset adds a 'stopping' hold state, a SIGTSTP handler in the launcher and waiter, and a process-gr…
Full details: Description check

Explanation

The description follows the template's two required sections, Change and Validation. The Change section explains the race, the cause, the fix, and why the earlier approach was reverted. The Validation section lists the checks run, with run counts, results, and platforms (CI runner image and macOS). It does not list skipped checks, and it does not explicitly say whether the changelog entry (changelog.d/) was added, although the raw summary shows changelog.d/tui-fg-after-stop.md exists. The description is detailed and matches the diff. The check passes.

Full details: Title check

Explanation

The title names the main change: a fix in the tui job-control path so a quick fg is not lost. The changeset adds a 'stopping' hold state, a SIGTSTP handler in the launcher and waiter, and a process-group SIGSTOP. The title's 'hold the screen's stop pending' phrasing is close to the mechanism, though 'red main' is a branch reference that adds noise.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between d435813 and be04c25.

Files selected for processing: 11

  • changelog.d/tui-fg-after-stop.md
  • scripts/plugin-tui-waiter.ts
  • scripts/plugin-tui.test.ts
  • scripts/plugin-tui.ts
  • tui/host/guard.ts
  • tui/host/hold.ts
  • tui/host/host.test.ts
  • tui/host/restore.contract.test.ts
  • tui/host/unix.test.ts
  • tui/host/unix.ts
  • tui/testing/launcher-fixture.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 3 remain after this review.

…stead of a group stop

Review round on the first approach (both reviewers blocked it): the shell waiter's
TSTP trap could hang inside stty, report a trap's status instead of the screen's,
and the screen's kill(0, SIGSTOP) could stop an invoking shell sharing its group.

The launcher and the waiter are back as they were (they stop at once at SIGTSTP).
The screen now asks for its stop first and holds it pending (SIGTSTP blocked at its
default action), gives the terminal back, then unblocks it. POSIX discards a pending
stop when SIGCONT is sent, so the shell's fg, however soon after the terminal came
back, cancels the stop instead of being lost to it. Started by a launcher, the
screen also checks whether that launcher (stopped by the same SIGTSTP) runs again:
then the shell continued the job before the screen's handler ran, and the screen
does not stop; it enters again at that continue.

The restore contract test runs through the waiter and the Bun launcher, and two new
cases hold the fixture on a FIFO to put fg exactly before the stop (after the leave
bytes, and before the handler ran): all four fail on the old code every time.
@devswha

devswha commented Nov 25, 2026

Copy link
Copy Markdown
Owner Author

Reviews on be04c25 (the first approach): a gate review (omo) and an adversarial Codex review (gpt-6.2-sol). Both returned BLOCK. The fix round, af8d0bc, replaced the approach. How each finding was resolved:

Finding Severity Resolution
The waiter's TSTP trap can leave stty -g stopped while sh waits on it, so the shell never gets a prompt HIGH Gone: the waiter is unchanged from main, with no trap.
An interrupted wait returns 148 instead of the screen's status MEDIUM Gone: the waiter is unchanged.
kill(0, SIGSTOP) stops an invoking shell that shares the process group (set +m) HIGH Gone: the screen stops only itself again.
Ctrl+Z right after fg leaves the screen unresponsive (hold byte still 3) HIGH Gone: there is no hold-byte protocol, and the launcher stops at SIGTSTP as before.
The handler runs only after fg (startup window or a loaded machine), so the job is stopped twice HIGH/MEDIUM Fixed: the screen checks whether its launcher runs again (from /proc on Linux, proc_pidinfo on macOS). If it does, the screen does not stop. Covered by the deterministic fg at stop test.
A delayed trap stops the waiter after the screen exits MEDIUM Gone: the waiter is unchanged.
The test passed on broken code 49/50 times (luck) MEDIUM Fixed: the FIFO-gated tests put fg at the exact adverse point. On the old code they fail 4/4 every time.
A launcher-only SIGTSTP is dropped; a stop is lost during the Bun-to-waiter handoff LOW Gone with the launcher changes.

There was one fix round, as the merge rules ask. No further review round was run on af8d0bc.

A process group that leads its session (a pty entry run on its own, as the
herdr-core live contract supervises the screen) is orphaned, and the system drops
SIGTSTP's default stop there: the pending stop never stopped the screen, and a stop
from outside left it given back but running. Only SIGSTOP stops such a process, as
before; no job-control shell sends an fg there.
@devswha devswha changed the title fix(tui): stop the launcher only after the screen at a job stop, so a quick fg is not lost (red main) fix(tui): hold the screen's stop pending so a quick fg is never lost (red main) Nov 25, 2026
@devswha
devswha merged commit 267319e into main Nov 25, 2026
21 of 23 checks passed
@devswha
devswha deleted the fix/tui-fg-race branch November 25, 2026 16:36
@devswha

devswha commented Nov 25, 2026

Copy link
Copy Markdown
Owner Author

Merged through pr-merge at head 429b6a53e. Seen before the merge was asked for: required checks green, no unresolved thread, no conflict, no hold, main not red, no release in progress. CodeRabbit said it did not review (a limit or a skip) since this head.

main moved but untouched: merged without update. Its new commits up to 1530ebee6 change no file of this pull request, none imported by one, and nothing that decides which checks run.

Red-main fix for https://github.com/devswha/ovis/actions/runs/45634721151 (lead's brief, lane tui-fg-race). Reviews: omo gate reviewer + Codex adversarial (gpt-6.2-sol) on be04c25, both BLOCK; one fix round (af8d0bc, then f93fe81 for the orphaned-group case the live contract caught), findings and resolutions in #4752 (comment). CodeRabbit did not review this head. Load test in the CI runner image: 3/100 failures before, 0/100 (600 cases) after; new FIFO-gated tests fail 4/4 on the old code. herdr-core live contract (not required): its remaining failure, with-core 'dump WASM failure', also fails on hosted runners for other PRs (run 45638535379) and passes on main and this branch in the runner image. Integration's server/account/lock.contract.test.ts 403 at the updated head (R8 peer uid check, a known flake in the 2026-11-24 CI flake report) was rerun and passed.

devswha added a commit that referenced this pull request Nov 26, 2026
…nued (red main) (#4776)

Fixes the race that turned main red again in
https://github.com/devswha/ovis/actions/runs/45735443845
(`tui/host/restore.contract.test.ts`, "comes back after an interactive
shell stopped the whole job and `fg` continued it: the screen did not
stop before fg"), after #4752.

## Cause (product)

At a stop of the whole job, the screen holds its own stop pending, gives
the terminal back, then asks whether its launcher (the job's first
process) already stopped at the same SIGTSTP. A launcher read as running
meant "the shell continued the job already", so the screen dropped its
stop.

Linux lets go of the signal lock between taking SIGTSTP off the queue
and stopping (`get_signal` checks for an orphaned process group there:
"signals can be posted during this window", kernel/signal.c). A launcher
in that moment shows neither a pending stop nor a stopped thread in
`/proc`, then stops right after. The screen then kept running in a
stopped job. On a loaded machine the moment is long enough to hit.

Evidence from the failing runs, with the test's state read added: the
screen stayed `S` for 10 more seconds while its launcher was `T`.

## Fix

- A launcher seen running no longer settles the stop at once. The stop
stays pending until one of the two shows: the launcher stops (the screen
stops too), or the job's continue is heard (the stop is dropped; the
guard enters again at it). `LAUNCHER_STOP_LOOK_MS` (1 ms) is the look
interval. `LAUNCHER_STOP_WAIT_MS` (2 s) bounds the wait with the old
answer.
- The guard writes nothing between the stop and the continue: frames,
resizes, the colour-scheme watch and display queries wait, and the
continue repaints. Without this, a frame queued during the wait reached
the shell's screen after the leave bytes (found by the gate review).
- A SIGTERM, SIGINT or SIGHUP during the wait drops the pending stop, so
the shutdown is never frozen.
- SIGTSTP stays at its default action while the stop is held. Putting
Bun's handler back during the wait was tried: the held stop was then
taken from the thread and never landed (5 of 100 runs).

## Known limits (reviewed, not changed)

- A launcher that stays between taking SIGTSTP and stopping for more
than 2 s still reads as continued.
- A second stop inside the synchronous gap between the guard's re-entry
and the stop being settled at a continue is taken at the default action.
- A continue that reaches the screen before it holds its stop while the
launcher still reads stopped (older than this PR, not reproduced).

## Validation

CI runner image (`herdr-ci-runner:local`, `--cpus 8`), 10 parallel
workers x 10 runs of the six related contract cases, on the loaded P620
(load average 115-225):

| code | runs | failures |
|---|---|---|
| main bb90514 | 100 | 3 (screen `S`, launcher `T`) |
| first commit (wait for the launcher) | 100 + 100 | 0 |
| with Bun's handler kept during the wait (dropped) | 100 | 5 |
| this PR's head | 100 + 100 | 0 |

- `bun run check fast`: green on 8f59c6a (28443 pass, 0 fail).
- `bunx tsc --noEmit -p tui/tsconfig.json`: clean.
- `bun test tui/host tui/conventions.test.ts tui/loop.test.ts
scripts/plugin-tui.test.ts`: 397 pass on the first commit;
`tui/host/unix.test.ts`, `host.test.ts`, conventions: 100 pass on the
head.
- New unit cases in `tui/host/unix.test.ts`: the stop is held until the
continue, the stop lands once the launcher stops, a SIGTERM during the
wait ends without stopping, and nothing is written while suspended. They
fail on main.
- Reviews: omo gate review (one HIGH, fixed here) and a Codex review (no
HIGH; MEDIUMs fixed or listed above).
- Not run: macOS (eamac). The macOS path changes only in the same shared
code; `proc_pidinfo` is read as before.
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.

1 participant