Skip to content

test(source): pin sourced exit N shell-termination semantics (rubash#426) - #457

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/sourced-exit-noexec
Oct 9, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/sourced-exit-noexec

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

closes #426

Summary

Issue #426 (wt92/harvest divergence group 6): a sourced file whose body runs exit 0 did not terminate an interactive niu -i session — the sourced exit surfaced as a script-level status instead of a session-level exit jump, while GNU bash terminates the whole shell (builtins/source.def sources through evalstring.c parse_and_execute in the CURRENT shell; the exit builtin's jump_to_top_level(EXITPROG) unwinds past evalfile into reader_loop, evalstring.c:396-403/:618-619, and exit_shell carries N).

Root cause / state of the engine

The engine-side defect was the grouped runner converting ExecuteError::ExitCode into a plain status without recording the top-level jump, and the interactive reader never consulting it. That invariant was repaired by the rubash#433 work (4b6234c: run_source_impl marks exit_jump_pending on ExitCode/FatalFunctionError at the list top; run_interactive_stdin's accept_line! ends the session on a non-parse jump, and finish_shell carries the jump's status). At this HEAD the sourced-exit path is GNU-correct in every reachable shape (verified: -c rc propagation, piped--i session death with the jump's status, nested ., exit-in-function, EXIT-trap interplay, return locality, ConPTY probe).

This PR therefore pins the contract with a dedicated black-box suite so the sourced-exit path cannot regress into a script-exit again, and closes the issue.

Tests

New tests/issue426_sourced_exit_regressions.rs (11 black-box tests, CARGO_BIN_EXE_rubash, piped--i interactive probes in the #433 harness shape):

  • -c: sourced exit N exits N, trailing text never runs (N = 3 and the issue's exit 0)
  • nested .: the inner file's exit unwinds both source frames (rc 7)
  • exit N inside a function called from a sourced file still ends the shell (rc 9)
  • return N keeps its source-local unwind: sourcing stops, the shell lives on, $? = 4
  • EXIT trap fires at the sourced exit's boundary with the sourced status and cannot resurrect the shell (rc 5 / rc 9)
  • interactive piped--i: sourced exit 0 ends the session before the next line is read (exactly one prompt, rc 0); exit 5 ends with rc 5; return 4 keeps the session alive; nested-source exit and the EXIT-trap shape

Also sweeps two rustfmt-only fixes (src/executor/external_finish.rs, tests/cat_redirect_stream_regressions.rs) left unformatted by a prior merge — the pre-commit cargo fmt gate requires the clean tree.

Related suites green: issue297_300_interactive_init_regressions, issue343_345_trap_regressions, issue419_console_repl_channel_regressions, issue433_prompt_command_exit_regressions, issue70_subshell_trap_regressions, host_internal_trap_suppression, issue312_interactive_noedit_defaults.

…h#426)

Issue rubash#426 (wt92/harvest divergence group 6): a sourced file whose
body runs `exit 0` did not terminate an interactive `niu -i` session —
the sourced exit surfaced as a script-level status instead of a
session-level exit jump, so the REPL kept prompting and read the rest of
stdin while GNU bash dies (builtins/source.def -> evalfile.c
evalfile_internal -> evalstring.c parse_and_execute runs in the CURRENT
shell; the exit builtin's jump_to_top_level(EXITPROG) unwinds past
evalfile into reader_loop, evalstring.c:396-403/:618-619, and exit_shell
carries N).

Root cause and fix: the engine-side defect was the grouped runner
converting ExecuteError::ExitCode into a plain status without recording
the jump, and the interactive reader never consulting it. That invariant
was repaired by the rubash#433 work (4b6234c: run_source_impl marks
exit_jump_pending on ExitCode/FatalFunctionError at the list top;
run_interactive_stdin accept_line ends the session on a non-parse jump
and finish_shell carries the jump's status). At this HEAD the sourced-exit
path is GNU-correct in every reachable shape, so this change pins the
contract with a dedicated black-box suite instead of re-touching the
engine:

tests/issue426_sourced_exit_regressions.rs (11 tests, CARGO_BIN_EXE_rubash,
piped-`-i` interactive probes in the #433 harness shape):
- `-c`: sourced `exit N` exits N, trailing text never runs (N = 3 and the
  issue's `exit 0`);
- nested `.`: the inner file's exit unwinds both source frames (rc 7);
- `exit N` inside a function called from a sourced file still ends the
  shell (rc 9);
- `return N` keeps its source-local unwind: sourcing stops, the shell
  lives on, $? = 4;
- EXIT trap fires at the sourced exit's boundary with the sourced status
  and cannot resurrect the shell (rc 5 / rc 9);
- interactive piped-`-i`: sourced `exit 0` ends the session before the
  next line is read (exactly one prompt, rc 0); `exit 5` ends with rc 5
  and the trailing input is never echoed or run; `return 4` keeps the
  session alive with $? = 4; nested-source exit and the EXIT-trap shape.

Also sweeps two rustfmt-only fixes (src/executor/external_finish.rs,
tests/cat_redirect_stream_regressions.rs) left unformatted by a prior
merge — the pre-commit cargo fmt gate requires the clean tree.

Related suites green: issue297_300_interactive_init_regressions,
issue343_345_trap_regressions, issue419_console_repl_channel_regressions,
issue433_prompt_command_exit_regressions, issue70_subshell_trap_regressions,
host_internal_trap_suppression, issue312_interactive_noedit_defaults.
Closes #426.
@caomengxuan666
caomengxuan666 merged commit 9e40f80 into master Oct 9, 2026
7 of 8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/sourced-exit-noexec branch October 9, 2026 12:45
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.

[bug] sourced 'exit 0' does not terminate niu -i (GNU bash exits)

1 participant