Skip to content

✨ feat(run): add interrupt_post_commands for cleanup after Ctrl-C#3882

Merged
gaborbernat merged 2 commits intotox-dev:mainfrom
gaborbernat:3858
Mar 17, 2026
Merged

✨ feat(run): add interrupt_post_commands for cleanup after Ctrl-C#3882
gaborbernat merged 2 commits intotox-dev:mainfrom
gaborbernat:3858

Conversation

@gaborbernat
Copy link
Copy Markdown
Member

@gaborbernat gaborbernat commented Mar 16, 2026

Users pressing Ctrl-C during test runs had no way to execute cleanup tasks in commands_post. While the finally block ensures commands_post starts, the interrupt check in execute_async() immediately exits with SystemExit(-2) before any commands execute. This prevents cleanup like stopping Docker containers or removing temp files when tests are interrupted.

Add interrupt_post_commands config option (default false). When enabled, first Ctrl-C interrupts commands and commands_pre but allows commands_post to proceed for cleanup. Second Ctrl-C during commands_post forces immediate termination. Implementation uses a context manager allow_post_commands_after_interrupt() that temporarily sets _allow_interrupted_execution flag to bypass the interrupt check, tracks interrupt levels with _fully_interrupted for second Ctrl-C detection, and wraps commands_post execution in the context manager when the config is enabled.

Tests verify cleanup runs after single Ctrl-C when enabled, doesn't run by default, and stops on second Ctrl-C. Cross-platform support uses CTRL_C_EVENT on Windows and SIGINT on Unix.

Fixes #3858

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@gaborbernat gaborbernat force-pushed the 3858 branch 2 times, most recently from 483ae33 to 0541f19 Compare March 16, 2026 23:34
@gaborbernat gaborbernat changed the title ✨ feat(run): add interrupt_post_commands to run cleanup after SIGINT ✨ feat(run): add interrupt_post_commands for cleanup after Ctrl-C Mar 16, 2026
Users pressing Ctrl-C during test runs had no way to execute cleanup
tasks in commands_post. The finally block ran, but commands_post
commands were skipped because execute_async checks _interrupted and
exits immediately. This prevented cleanup like stopping containers
or removing temp files when tests were interrupted.

Add interrupt_post_commands config option (default false). When
enabled, first Ctrl-C interrupts commands but allows commands_post
to run for cleanup. Second Ctrl-C interrupts commands_post. Track
interrupt state with _fully_interrupted and _allow_interrupted_execution
flags, add helper methods to toggle execution allowance, and modify
run_commands to enable allowance for commands_post when configured.

Fixes tox-dev#3858
@gaborbernat gaborbernat merged commit bf4e4e8 into tox-dev:main Mar 17, 2026
27 checks passed
gaborbernat added a commit to gaborbernat/tox that referenced this pull request Mar 30, 2026
…x-dev#3882)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

commands_post should have option to run after SIGINT

1 participant