Repository navigation
fix(jobs): foreground-acquire attempt in controlling_terminal_available - #3398
Conversation
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.
|
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 — 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 |
Captain review — PASSReviewed at head Measured
Delimitations attached to this review
Merge conditionsRecompute the intersection and merge-tree against the then-live tip |
…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).
Fixes #3321
What was fixed
controlling_terminal_available()only asked whether stderr is a tty (or/dev/ttyopens) 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 withinitialize_job_control: no job control in backgroundand dropsm(jobs.c:4731-4869).The fix makes
controlling_terminal_available()return aTerminalAvailabilityenum (Available / Unavailable / BackgroundBail) running GNU's loop:shell_ttyis dup(stderr) or open(/dev/tty) when forced-interactive and stderr is not a tty (jobs.c:4749-4766); whiletcgetpgrpsays 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_bailskips the second line,jobs.c:4797).establish_interactive_job_controldispatches on the enum; the Unavailable arm keeps the two existing diagnostics untouched.The stop is delivered with
pthread_killto the looping thread, notkill(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 measuredkill(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-directedpthread_killstops 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,
$-stayshimBHc). The fix adds the foreground-acquire attempt the issue asks for.Verification (all run, per-item counts)
tests/issue3321_background_pgrp_job_control.rs: 4/4 pass (new pty harnesstests/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, thenhimBHcafter 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(directhimBHc, no stop). Fail-first on the unpatched tree (supervisor re-ran with the patch method): 1 passed / 3 failed — green was exactly thefg-control. Supervisor re-ran the pin 10 times, 4/4 every time (thekill(0, ...)form was flaky at 2/4 and 3/4, which is how the thread-delivery race was found).bg-stop-then-fgleg alone); blocked-face stderr byte-identical except the program-name prefix (bash:vstarget/debug/rubash:, expected).issue2948_monitor_flag_regressions6/6,issue2731_interactive_monitor_default6/6,issue2774_interactive_no_tty_job_control4/4,issue2487_suspend_job_control_gate11/11.cargo build -j 2clean,cargo fmt --checkclean.src/lexer/continuation.rsuntouched.Owed
bash -i &does this), but rubash previously ran straight through; arun_interactive_stdinlibrary host driving it from a background group stops the same way (GNU stops at the same place).-marm (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.running_in_background(jobs.c:4865) has no rubash counterpart and no consumer; not introduced here.pty_read_probe.py); without that environment the test skips cleanly.