Skip to content

fix(jobs): foreground-acquire attempt in controlling_terminal_available - #3398

Merged
caomengxuan666 merged 4 commits into
unixwin:masterfrom
Uarz:fix/issue-3321
Nov 19, 2026
Merged

caomengxuan666 merged 4 commits into
unixwin:masterfrom
Uarz:fix/issue-3321

Conversation

@Uarz

@Uarz Uarz commented Nov 11, 2026

Copy link
Copy Markdown
Member

Fixes #3321

What was fixed

controlling_terminal_available() only asked whether stderr is a tty (or /dev/tty opens) and never attempted the foreground acquisition, so an interactive shell started in a background process group ran straight through with job control on ($- = himBHc) where GNU stops on SIGTTIN until foregrounded or, with SIGTTIN blocked, bails after 18 attempts with initialize_job_control: no job control in background and drops m (jobs.c:4731-4869).

The fix makes controlling_terminal_available() return a TerminalAvailability enum (Available / Unavailable / BackgroundBail) running GNU's loop: shell_tty is dup(stderr) or open(/dev/tty) when forced-interactive and stderr is not a tty (jobs.c:4749-4766); while tcgetpgrp says background, restore SIGTTIN to DFL and stop, bailing after the 18th kill (tty_sigs++ > 16, jobs.c:4798) with only the first diagnostic (goto just_bail skips the second line, jobs.c:4797). establish_interactive_job_control dispatches on the enum; the Unavailable arm keeps the two existing diagnostics untouched.

The stop is delivered with pthread_kill to the looping thread, not kill(0, SIGTTIN): rubash's main logic runs on a spawned worker thread (main.rs, 512MiB stack) while the OS thread parks in join, so a group-directed kill lands on an arbitrary thread and the group stop reaches the looping thread only asynchronously — supervisor verification measured kill(0, ...) returning in 2-3us while the loop spun up to all 18 iterations before any stop took effect, making the pin flaky (2/4, 3/4). A thread-directed pthread_kill stops the looping thread synchronously inside the call (270-680us round-trip through fg+CONT in a C model, one iteration, deterministic over 10 runs); the stop still arrests the whole process group, so the session leader's waitpid sees it exactly as with GNU's kill.

What was deleted

Nothing. No files, features, or tests were removed.

A correction to the issue text (please do not quote it verbatim)

The issue says the shape "would be misjudged as no ctty", but the measured direction is the opposite: unpatched rubash misjudges UNAVAILABLE job control as available (never stops, never errors, $- stays himBHc). The fix adds the foreground-acquire attempt the issue asks for.

Verification (all run, per-item counts)

  • Regression pin tests/issue3321_background_pgrp_job_control.rs: 4/4 pass (new pty harness tests/interactive/support/pty_background_pgrp_probe.py, built per the issue's gate note: one fresh pty session per leg, session leader holds the ctty, shell forked into a new background group). Legs: bg-stop-then-fg (must SIGTTIN-stop, then himBHc after fg, no diagnostics), bg-sigttin-blocked (no stop, only the background diagnostic, no second diagnostic, hiBHc), bg-stderr-pipe (same stop-then-fg with piped stderr), fg-control (direct himBHc, no stop). Fail-first on the unpatched tree (supervisor re-ran with the patch method): 1 passed / 3 failed — green was exactly the fg-control. Supervisor re-ran the pin 10 times, 4/4 every time (the kill(0, ...) form was flaky at 2/4 and 3/4, which is how the thread-delivery race was found).
  • GNU 5.3.20 oracle: 4/4 on the same harness (5/5 on the bg-stop-then-fg leg alone); blocked-face stderr byte-identical except the program-name prefix (bash: vs target/debug/rubash:, expected).
  • Adjacent: issue2948_monitor_flag_regressions 6/6, issue2731_interactive_monitor_default 6/6, issue2774_interactive_no_tty_job_control 4/4, issue2487_suspend_job_control_gate 11/11.
  • cargo build -j 2 clean, cargo fmt --check clean. src/lexer/continuation.rs untouched.

Owed

  1. Behaviour change for the captain to know about: an interactive rubash started in a background process group now stops like GNU until foregrounded — this is GNU behaviour itself (bash -i & does this), but rubash previously ran straight through; a run_interactive_stdin library host driving it from a background group stops the same way (GNU stops at the same place).
  2. The non-interactive -m arm (main.rs) still only looks at whether stderr is a tty — that is GNU's force path, a different face, and monitor flag m in $- does not track job-control availability (set without a tty, never set for library one-shots) #2948 already pins its current behaviour. Out of scope here.
  3. running_in_background (jobs.c:4865) has no rubash counterpart and no consumer; not introduced here.
  4. The pin needs python3 + the pty module (same pattern as pty_read_probe.py); without that environment the test skips cleanly.

controlling_terminal_available() only asked whether stderr is a tty (or /dev/tty opens) and never attempted the foreground acquisition, so an interactive shell started in a background process group ran straight through with job control on ($- = himBHc) where GNU stops on SIGTTIN until foregrounded or, with SIGTTIN blocked, bails after 18 attempts with 'initialize_job_control: no job control in background' and drops m (jobs.c:4731-4869). The fix makes controlling_terminal_available() return a TerminalAvailability enum (Available / Unavailable / BackgroundBail) running GNU's loop: shell_tty is dup(stderr) or open("/dev/tty") when forced-interactive and stderr is not a tty (jobs.c:4749-4766); while tcgetpgrp says background, restore SIGTTIN to DFL and stop, bailing after the 18th kill (tty_sigs++ > 16, jobs.c:4798) with only the first diagnostic (goto just_bail skips the second line, jobs.c:4797). establish_interactive_job_control dispatches on the enum; the Unavailable arm keeps the two existing diagnostics untouched. The stop is delivered with pthread_kill to the looping thread, not kill(0, SIGTTIN): rubash's main logic runs on a spawned worker thread while the OS thread parks in join, so a group-directed kill lands on an arbitrary thread and the group stop reaches the looping thread only asynchronously — supervisor verification measured kill(0, ...) returning in 2-3us while the loop spun up to all 18 iterations before any stop took effect, making the pin flaky (2/4, 3/4). A thread-directed pthread_kill stops the looping thread synchronously inside the call (270-680us round-trip through fg+CONT in a C model, one iteration, deterministic over 10 runs); the stop still arrests the whole process group, so the session leader's waitpid sees it exactly as with GNU's kill.

Refs unixwin#3321

Regression pin tests/issue3321_background_pgrp_job_control.rs (4 tests, new pty harness tests/interactive/support/pty_background_pgrp_probe.py; fail-first 1 passed / 3 failed on the unpatched tree, 4/4 pass after; supervisor verified both rounds with the patch method and re-ran the pin 10 times, 4/4 every time). GNU oracle 4/4. Adjacent: issue2948 6/6, issue2731 6/6, issue2774 4/4, issue2487 11/11. cargo fmt clean. Note: the issue text says the shape 'would be misjudged as no ctty', but the measured direction is the opposite — unpatched rubash misjudges UNAVAILABLE job control as available (never stops, never errors); the fix adds the foreground-acquire attempt the issue asks for.
@caomengxuan666 caomengxuan666 added bug Something isn't working gnu-baseline Tracked by the GNU Bash 5.3.0 true-baseline ledger labels Nov 11, 2026
@caomengxuan666 caomengxuan666 self-assigned this Nov 11, 2026
@caomengxuan666 caomengxuan666 added P3-low 低:优化、文档、远期规划,有机会再做 area:jobs jobs, signals, process ownership, coproc labels Nov 11, 2026
@caomengxuan666

Copy link
Copy Markdown
Member

Thanks, @Uarz — received. This is queued for captain review (per the owner's rule, external PRs are reviewed by the captain first); no merge or change request from intake.

First pass on the diff: the change matches the body — controlling_terminal_available() now returns a TerminalAvailability enum running GNU's foreground-acquire loop (tcgetpgrp check, SIGTTIN restore-to-DFL + stop, bail after the 18th attempt with only the first diagnostic), delivered via pthread_kill to the looping thread, and establish_interactive_job_control dispatches on the enum with the two existing Unavailable diagnostics untouched. The new pin tests/issue3321_background_pgrp_job_control.rs (4 legs, new pty harness pty_background_pgrp_probe.py) covers stop-then-fg, SIGTTIN-blocked bail, piped-stderr, and the foreground control, with fail-first 1 passed / 3 failed on the unpatched tree as claimed.

CI is fully green here (10 checks passed + 1 skipping).

One thing flagged for the captain's review, from your Owed §1: an interactive rubash started in a background process group now stops like GNU until foregrounded, where it previously ran straight through — a behaviour change for hosts driving run_interactive_stdin from a background group as well. No action needed from you unless the review asks for it.

@caomengxuan666

Copy link
Copy Markdown
Member

Captain review — PASS

Reviewed at head b20430db53b4904af55c354600aaa2daed41776f; true fork point
f5f65d35 (git merge-base). All legs ran on the Simular box in a clean-fd
context (old-server route failed 5x; box fallback drivers). Full dossier:
~/workspace/review-3398/CAPTAIN-VERIFY.md.

Measured

  • Pin issue3321_background_pgrp_job_control: 4/4 on the head, 5
    consecutive runs, zero flake
    ; on the fork base with only the pin
    topped up, 1 passed / 3 failed, the failures exactly the three
    behaviour legs (fg-control passes). Clean fail-first.
  • Adjacent stems unchanged on the head: issue2948 6/6, issue2731 6/6,
    issue2774 4/4, issue2487 11/11.
  • lib: 1245 passed / 0 failed / 4 ignored on both trees, failure name
    sets identical (empty). fmt, marker-literal and UTF-8 boundary hygiene
    legs clean.
  • Warnings: scoped -D warnings check on the head (189 rustc
    invocations, all carrying -D warnings) stops at exactly one error
    point, tests/issue2584_compgen_job_state_actions_regressions.rs:23:9
    (needless mut) — the file is byte-identical between the fork base and
    this head, so the point is pre-existing, not introduced here; none in
    src/, none in this PR's files. The full-target --keep-going
    enumeration was completed on the merged tree (1b264bfc…): 1467/1467
    flagged invocations, 0 errors (see below).
  • Behaviour section (script form, controlled PTY/session orchestration;
    GNU bash 5.3.20 oracle, fork base, this head): the head matches GNU
    byte-for-byte, modulo program name, on blocked, blocked-pipe and stop
    shapes (background diagnostic once + m dropped; stop on SIGTTIN
    until foregrounded). The base differs on all three (no diagnostic, m
    kept; exits instead of stopping).
  • Library-host form (minimal Session-based host, same orchestration):
    host-bg-stop PASS, host-fg PASS (FLAGS=himBHs after foregrounding);
    host-bg-blocked behaves identically to GNU bash 5.3.20 run in the same
    orchestration (diagnostic once, no input processed afterwards — a
    background group reading the ctty with SIGTTIN blocked gets EIO, for
    GNU as well, so the probe's FLAGS expectation for that leg is void as
    a product requirement, not a divergence).
  • Merged tree vs live tip ffc09fb7 (merge-tree 1b264bfc…, zero text
    conflict; src/script_driver.rs sha256 380ff46d…): build rc=0,
    pin 4/4 twice, adjacent issue2948 6/6 and issue2774 4/4; full-target
    -D warnings forced recheck: 1467 rustc invocations, all carrying
    -D warnings, 0 error lines (the head tree's single pre-existing
    point was fixed in the interval by test(executor): correct pwd expectation formula and clear -D warnings dead code #3369).
  • Head CI: 12 check-runs finished, 10 success + 2 skipping, zero
    failure.

Delimitations attached to this review

  • The issue text's "would be misjudged as no ctty" is corrected by
    measurement (author's correction, independently confirmed): unpatched
    rubash misjudges UNAVAILABLE job control as available.
  • The pthread_kill thread-directed stop (instead of GNU's group
    kill(0, SIGTTIN)) is disclosed in the code with its measured basis;
    the observable contract (group stops, session leader's waitpid sees
    it) is identical — no立案.
  • The tcgetpgrp == -1 corner keeps the pre-existing monitor flag m in $- does not track job-control availability (set without a tty, never set for library one-shots) #2948 arm's text
    (first-line number differs from GNU's -1); pre-existing, not
    introduced here, no filing.
  • Harness header comments misstate the bail count (17 vs 18) and the
    second diagnostic; the pin's assertions themselves are correct.
    Comment-only, no action required.

Merge conditions

Recompute the intersection and merge-tree against the then-live tip
(currently 3ef22b86, tree 4740c06d…; the PR's files are byte-identical
in that tree and in the reviewed 1b264bfc… tree, see dossier §0f),
update-branch, then on the merged head: pin 4/4, lib failure set ∅,
adjacent stems unchanged, full-target -D warnings with --keep-going
(the review already ran the full enumeration on the reviewed merge tree:
0 errors; re-run only if the live tip moved the PR's files), merged-shape
spot checks (blocked/stop/fg, script form + library-host form),
final-head CI finished green.

caomengxuan666 added a commit that referenced this pull request Nov 18, 2026
…ly exec (Refs #3893)

fix(executor): bind {var} heredoc at its list position in redirect-only exec (Refs #3893)

Root cause: in `execute_stdio_only_exec_redirect` (src/executor/trap_exec.rs),
the walk consumed a `{var}<<` / `{var}<<<` heredoc twin and then called
`execute_dynamic_fd_var_redirect`, which has no HereDoc/HereString arm —
a pure no-op apart from the readonly check. The real bind happened in the
tail loop, after every later entry's undo-save had already claimed its slot
(`exec 14<<X {v}<<A 14<<B` gave `v=11`, GNU gives `v=10`). The walk now
binds the heredoc at its list position via `execute_dynamic_fd_var_heredoc`
and the tail loop binds only twins the walk did not consume.

GNU anchors (bash 5.3, redir.c): :237 `do_redirections` single ordered walk;
:1065 heredoc/here-string arm, `here_document_to_fd` at :1072; :1082/:1113
REDIR_VARASSIGN F_DUPFD + bind; :1091 later undo-save lands after the var's
allocation. Oracle matrix (6 shapes, vendored 5.3.20) all match.

Verification (captain-adjudicated, REPORT ~/workspace/fix-3893/REPORT.md):
- Pins tests/issue3893_exec_var_heredoc_fd_allocation.rs (6 pins): fail-first
  3 red / 3 green on base bb27eb2 (`v=11` vs expected `v=10`), 6/6 green
  after the fix — on server legs AND on this head's CI census (6/6 PASS).
- Adjacent stems green: issue3578 14/14, issue3578-dual 6/6, issue1313 2/2,
  issue3195 19/19; lib 1280/0; RUSTFLAGS="-D warnings" rc=0; rustfmt clean;
  marker-literals + utf8-boundary hygiene clean.
- Head census total 82 (era band 79–88), net −3 vs tip (85). New cells vs tip:
  2× issue3640 (exec-3/4 fd landing) — PROVEN census jitter by captain
  isolation: the stem passes 7/7 on both the base tree and this head's tree
  on the server; 1× issue2333 carrier + 1× issue2883 prompt, both on faces
  this diff cannot reach (flake standard). issue3316 cell still red on the
  head census — known fixture defect, reopened as #3316, separate lane.
- Goldens job: exactly the 2 baseline names. CLI job: exactly the one
  pre-existing cell (compat_issue78, #3899 — fixed by pending #3903).
- Guard: no file collision with open PRs (#3538/#3398 touch script_driver.rs
  only; #3902 also touches trap_exec.rs but different hunks — it rebases
  after this merge).

本 PR 删除了什么:
- 删除了 `execute_stdio_only_exec_redirect` 里 `{var}` heredoc 只在 tail loop
  滞后绑定的旧路径(walk 已消费的 twin 不再走到 tail 绑定)。
- 删除了 fd_var 分支里对 heredoc 条目调用无 HereDoc/HereString 臂的
  `execute_dynamic_fd_var_redirect` 所产生的无操作调用(该调用对 heredoc
  除只读检查外什么都不做)。
- 未删除任何公开 API、测试或产品行为;funnel 面
  `apply_dynamic_fd_var_redirects` 的同类分裂限制未动,已另立 #3906 跟进。

UNVERIFIED-BY-CAPTAIN — captain review: PASS. Merge by captain; #3893 to be
hand-closed by the captain after merge (Refs-only body, no auto-close).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:jobs jobs, signals, process ownership, coproc bug Something isn't working gnu-baseline Tracked by the GNU Bash 5.3.0 true-baseline ledger P3-low 低:优化、文档、远期规划,有机会再做

Projects

None yet

Development

Successfully merging this pull request may close these issues.

controlling_terminal_available 只试 stderr tty:后台进程组下 ctty 误判

2 participants