Skip to content

fix(repl): end the session on top-level exit jumps (command lists, aliases, functions) - #544

Merged
Catapult291 merged 1 commit into
unixwin:masterfrom
Catapult291:fix/interactive-exit-jump-bubble
Oct 24, 2026
Merged

Catapult291 merged 1 commit into
unixwin:masterfrom
Catapult291:fix/interactive-exit-jump-bubble

Conversation

@Catapult291

@Catapult291 Catapult291 commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

After #541, a line that was exactly exit N ends the REPL, but a top-level exit inside a compound/alias/function form only unwound the engine and kept prompting:

Input Before After
true; exit 3 session stays alive ends, rc=3
false; exit 5 stays alive ends, rc=5
alias bye=exit + bye 3 stays alive ends, rc=3
f(){ exit 4; } + f stays alive ends, rc=4
(exit 3) stays alive stays alive (matches bash: subshell exit only)
echo exit / exit abc / exit --help stays alive stays alive

Root cause: the grouped-reader route (every ;/&&/alias line) computed the exit-shell verdict and dropped it; the tokenize+parse route swallowed ExecuteError::ExitCode into a plain status. The script path (niu -c 'true; exit 3' → rc=3) was already correct.

Changes

  • Shell::session_exit_jump: Option<i32> + take_session_exit_jump(); entry points clear stale values so a leftover jump cannot kill the next line.
  • execute_interactive_reader_batch consumes the new rubash tracked grouped verdict (run_script_with_history_at_line_offset_tracked) and records the jump.
  • execute_line_with_options / execute_script_inner record a jump on ExitCode / FatalFunctionError; take_parse_error() screens out parse aborts so an interactive syntax error only drops its own group and keeps prompting (eval.c:202-205).
  • execute_host_synced_simple_ast propagates ExitCode instead of flattening it to Ok(code), so cd X; exit N records the jump on both the interactive and script routes (the script side is what niubash#402 wanted).
  • The line-editor loop, the --noediting loop, and the widget-submit path take the jump after each submission and end via the existing end_repl_session (EXIT trap → history flush → process::exit(code)).

Companion engine PR (must land together): unixwin/rubash#1453 (fix/grouped-reader-exit-jump-verdict) — tracked grouped verdict + exit --help GNU continue (without it, exit --help would kill the session once jumps bubble).

Test plan

  • cargo test -p niubash-runtime: 586 + 10 + 8 + 3 passed, 0 failed (includes 4 new jump unit tests).
  • ConPTY probe (pywinpty, build and installed binaries, 14/14): whole-table above; niu -c 'true; exit 3' / -c 'exit 3' / -c 'echo hi; exit 7' → 3/3/7; -c 'exit --help; echo AFTER' prints AFTER rc=0; D4 streaming | cat still first-line ~0.03s.

Follow-up to niubash defect log R1 (niubash-remaining-defects-2026-10-24). cherry-picked from local f8d4206, rebased onto master after #542 landed: the interactive ExitCode arms keep #542's set_last_exit_code so $? matches 130 after Ctrl-C, and additionally call note_session_exit_jump so the session ends.

…efects 2026-10-24)

D1 made a whole-line `exit N` end the REPL, but a top-level exit inside
a command list, alias, or function body only unwound the engine: the
grouped reader route (every `;`/`&&`/alias-bearing line) computed the
exit-shell verdict and discarded it, and the tokenize+parse route
swallowed ExecuteError::ExitCode into a plain status — so `true; exit 3`
printed and the REPL kept prompting (R1, niubash-remaining-defects
2026-10-24).

- execute_interactive_reader_batch takes the new tracked grouped verdict
  (rubash run_script_with_history_at_line_offset_tracked) and records it.
- execute_line_with_options and execute_script_inner record a session
  jump for ExitCode/FatalFunctionError; take_parse_error() screens out
  the parse abort, so an interactive syntax error still discards only
  its own group and keeps prompting (eval.c:202-205).
- execute_host_synced_simple_ast propagates ExitCode instead of
  flattening it to Ok(code), so `cd X; exit N` shapes record the jump on
  both the interactive and script routes (the script side is what
  niubash#402 wanted all along).
- The line-editor loop, the --noediting loop, and the widget-submit path
  take the jump right after each submission and end the session through
  end_repl_session (EXIT trap, history flush, host exit status). Taken
  into a `let` first: an if-let scrutinee would hold its borrow_mut()
  temporary through the body and panic on the second RefCell borrow
  (observed as fail-fast rc 0xC0000409 before the fix).
- execute_interactive_line/script drop a stale jump recorded outside the
  submission (mid-session rc re-source, niubash#180).

Verified: 4 new unit tests + full niubash-runtime suite (582+ lib, 0
failed); ConPTY probe (pywinpty) on both the built and installed
niu.exe: exit 3 / exit / logout 3 / exit 5; keep rc, `true; exit 3`=3,
`false; exit 5`=5, `echo hi; exit 7`=7, function=4, alias bye 3=3 all
end the session with the right status, while `(exit 3)`, `echo exit`,
`exit abc`, `exit 3 4`, `exit --help` stay alive; `niu -c` exit codes
unchanged; D4 streaming probe unchanged (cat first line 0.034s).
@Catapult291
Catapult291 force-pushed the fix/interactive-exit-jump-bubble branch from 2548c15 to e04721c Compare October 24, 2026 02:05
@Catapult291
Catapult291 merged commit bce460a into unixwin:master Oct 24, 2026
8 of 9 checks passed
caomengxuan666 added a commit that referenced this pull request Oct 24, 2026
… rustfmt)

Master's fmt gate is red since #544 on the current stable toolchain;
CI's rustfmt wants this call wrapped. Mechanical normalization so the
PR's merge preview passes cargo fmt --check.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
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