Skip to content

Ctrl-C does not run tear_down_after_script for the file in flight #1323

Description

@Chemaclass
Q A
OS macOS 26.5.2 (arm64)
Shell & version bash 3.2.57
bashunit version 0.50.0

Summary

Ctrl-C runs bashunit::main::cleanup, which kills the children, sweeps the temp directories and exits. It never runs tear_down_after_script for the file in flight, so a file-scoped resource acquired in set_up_before_script is left behind.

Same class as #1318. The per-test half of the pair already survives this: pkill sends SIGTERM to the test subshell, its EXIT trap fires, and tear_down runs. The file-scoped half does not.

Current behavior

● set_up_before_script                                                      11ms
Caught Ctrl-C, killing all child processes...

tear_down ran. tear_down_after_script did not. The resource survives.

How to reproduce

sigint_test.sh:

function set_up_before_script() { : >"$CLEANUP_MARKER.setupfile"; }
function tear_down_after_script() { : >"$CLEANUP_MARKER.RAN_FILE_TEARDOWN"; }
function set_up() { : >"$CLEANUP_MARKER.setuptest"; }
function tear_down() { : >"$CLEANUP_MARKER.RAN_TEST_TEARDOWN"; }
function test_slow() { sleep 10; assert_true true; }

A shell that backgrounds a job sets SIGINT to SIG_IGN in the child, and bash refuses to trap a signal that was ignored on entry. So the repro has to reset the disposition before exec, or the signal is swallowed and the run finishes normally:

$ CLEANUP_MARKER=/tmp/bu-si.marker \
    perl -e '$SIG{INT}="DEFAULT"; exec @ARGV' \
    ./bashunit --no-parallel sigint_test.sh &
$ sleep 3; kill -INT $!

$ ls /tmp/bu-si.marker.*
/tmp/bu-si.marker.RAN_TEST_TEARDOWN
/tmp/bu-si.marker.setupfile
/tmp/bu-si.marker.setuptest

RAN_FILE_TEARDOWN is absent.

Expected behavior

Ctrl-C runs tear_down_after_script for the file whose set_up_before_script already ran, then exits 1 as it does today.

Where it happens

  • src/main/run.sh:82: trap 'bashunit::main::cleanup' SIGINT
  • src/main/run.sh:382: bashunit::main::cleanup runs pkill -P $$, bashunit::cleanup_script_temp_files, bashunit::parallel::cleanup, bashunit::env::cleanup_run_output_dir, exit 1
  • --watch is the same path: src/main/watch.sh:38 traps INT in the parent and exec_tests runs in a ( ) subshell, so fixing cleanup covers both

Acceptance criteria

  • bashunit::main::cleanup runs tear_down_after_script for the current file when one is defined and its set_up_before_script already ran.
  • It runs before bashunit::cleanup_script_temp_files, so a hook that reads a bashunit::temp_file still finds it.
  • It runs after pkill -P $$, matching the normal order: per-test tear_down first, then the file hook.
  • Nothing runs for a file whose setup never ran, and nothing runs twice.
  • Ctrl-C still prints "Caught Ctrl-C, killing all child processes..." and still exits 1.
  • A hook that blocks does not hang the interrupt. Bound it, or write down in a comment why it is safe not to.
  • A test covering it. The SIG_IGN inheritance above has to be handled inside the test, and the reason belongs in a comment so the next reader does not delete the workaround.
  • CHANGELOG.md gets a Fixed entry.

Notes for the implementer

  • The trap cannot see the loop variable in bashunit::runner::load_test_files. Record the current file in a global when the loop starts one, and clear it after the file's teardown, so the trap knows both which file and whether the hook is still owed.
  • The same global answers the --stop-on-failure question in --stop-on-failure skips tear_down_after_script in sequential runs #1321. Worth building once.
  • Bash 3.0+ only. See .claude/rules/bash-style.md.
  • make sa, make lint, ./bashunit tests/, ./bashunit --parallel tests/.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions