Skip to content

test(trap): pin GNU RETURN-trap contract for source and function returns (closes #204) - #244

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/return-trap-func
Oct 10, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/return-trap-func

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Closes #204.

Root cause

The issue's symptom (RETURN trap fires when a sourced script completes but not when a function returns) traces to the embedded rubash engine: the function-return dispatch lives in rubash's run_function_return_trap (src/executor/trap_exec.rs, the rubash#343/#345 family). niubash 1.3.3 shipped a rubash revision predating that work.

Fix location: the GNU-aligned engine fix has already landed on rubash master; niu builds against it via the sibling-clone [patch."https://github.com/unixwin/rubash.git"] override (CI clones the sibling at latest master, same path). No niubash runtime code needed changing, so this PR delivers the niubash-layer half: an oracle-locked regression suite so the contract cannot regress silently again. If the release pipeline pins a rubash rev, it must be bumped to a revision containing run_function_return_trap (post rubash#343/#345) for shipped binaries to pick the behavior up.

GNU semantics verified against the oracle (GNU bash 5.2.37 msys, D:\Git\usr\bin\bash.exe, byte-compared)

  • Source completion fires the RETURN trap unconditionally (evalfile.c source_file).
  • Function return fires it only when the function inherits the trap: global functrace (set -T) / shopt -s extdebug (implies functrace), or the per-function trace attribute (declare -ft f / declare -f -t f). A plain trap ... RETURN; f does not fire (execute_cmd.c:5291-5295).
  • declare -t f without -f goes down the variable-attribute path: no trace attribute, no fire.
  • Under set -T, each nested call level fires exactly once, inner-first.
  • A trap armed inside a function fires at that function's return and stays armed after (replacing any outer trap), matching GNU scope behavior.

Test matrix (tests/issue204_return_trap_function_return.rs, 9 tests, all green)

scenario GNU 5.2.37 niu (this tree)
plain trap … RETURN; f no fire same
declare -ft f / declare -f -t f fires same
declare -t f (variable path) no fire same
shopt -s extdebug fires same
set -T, nested calls once/level same
source completion fires same
trap set inside function fires at return, persists same

Regression runs

  • tests/issue204_return_trap_function_return.rs: 9/9 pass.
  • smoke, smoke_1_3_0, defaults_floor, startup_files, rubash_revision_banner: green.
  • compat: 18 ignored by design (needs real winuxcmd command links, per suite header).
  • Pre-existing, unrelated local failure noted: host_contract::closed_stdout_pipe_does_not_print_broken_pipe_error ("niu: line 1: Broken pipe" spam / does not exit after stdout pipe closes) reproduces on this branch with zero source changes and is outside the RETURN-trap scope — filed separately rather than mixed into this PR.
  • cargo fmt clean.

…on returns (closes #204)

GNU bash 5.2 fires the RETURN trap on sourced-script completion
unconditionally, and on shell-function return only when the function
inherits it: global functrace (set -T / extdebug) or the per-function
trace attribute (declare -ft f). A plain function call does not fire it.

The function-return dispatch itself lives in the embedded rubash engine
(run_function_return_trap, src/executor/trap_exec.rs, rubash#343/#345
family); rubash master carries the GNU-aligned fix and niu builds against
it via the sibling-clone [patch] override, so the niubash 1.3.3 symptom
(engine revision predating that work) is resolved upstream. This locks
the oracle-verified contract on the niubash side so the class cannot
regress silently: source completion, trace-attribute and extdebug
inheritance, declare -t variable-path non-inheritance, per-level firing
on nested calls under set -T, and in-function trap arming scope.
@caomengxuan666
caomengxuan666 merged commit 80d9cc9 into master Oct 10, 2026
3 of 9 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/return-trap-func branch October 10, 2026 05:01
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] RETURN trap 只在 source 完成时触发,函数返回不触发(GNU bash 两者都触发)

1 participant