run_semantic_tests: stop active subprocesses and cancel queued modules on interrupt (SEMANTIC-RUNNER-INTERRUPT) #1936

Manually merged
github merged 2 commits from gh-pr/1936/head into gh-pr/1936/base 2026-11-26 10:11:50 +00:00
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-25 16:12 UTC · original
abnalstlll-byte:feat/semantic-runner-interrupt → main · +261 −21 · 2 files · 2 commits · merged 2026-11-25 19:21 UTC

Summary

In scripts/run_semantic_tests.py, catch KeyboardInterrupt during both parallel module execution and serial test execution, cancelling queued test modules via pool.shutdown(wait=False, cancel_futures=True) and terminating active child subprocesses so that no orphaned zombie processes or queued modules run after Ctrl+C.

Changes

  • Active Subprocess Tracking: Track active child subprocesses in _active_processes with a thread-safe lock; register before execution and unregister upon completion.
  • Immediate Termination: On KeyboardInterrupt (or BaseException), invoke _terminate_active_processes() which terminates and kills registered child processes and cleans up immediately.
  • Future Cancellation: Empty remaining queued futures from ThreadPoolExecutor with pool.shutdown(wait=False, cancel_futures=True) and cancel all uncompleted futures so no pending tests run.
  • Responsive Wait Loop: In parallel execution, poll with concurrent.futures.wait(pending, timeout=0.2, return_when=FIRST_COMPLETED) to ensure sub-second signal delivery across Windows and POSIX.
  • Serial Mode: In -j 1 mode, run discover subprocess with responsive proc.wait(timeout=0.2) polling and clean interrupt termination.
  • Exit Code & Output: Return exit code 130 and output 'interrupted' to sys.stderr without printing a false success summary.
  • Regressions: Add parallel cancellation, serial interrupt, direct process termination, and POSIX signal regression tests in tests/test_run_semantic_tests.py.

Item: SEMANTIC-RUNNER-INTERRUPT

AI-assisted (Claude Code)

**@⁠abnalstlll-byte** on GitHub · 2026-11-25 16:12 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1936) `abnalstlll-byte:feat/semantic-runner-interrupt` → `main` · +261 −21 · 2 files · 2 commits · merged 2026-11-25 19:21 UTC ### Summary In `scripts/run_semantic_tests.py`, catch `KeyboardInterrupt` during both parallel module execution and serial test execution, cancelling queued test modules via `pool.shutdown(wait=False, cancel_futures=True)` and terminating active child subprocesses so that no orphaned zombie processes or queued modules run after Ctrl+C. ### Changes - **Active Subprocess Tracking**: Track active child subprocesses in `_active_processes` with a thread-safe lock; register before execution and unregister upon completion. - **Immediate Termination**: On `KeyboardInterrupt` (or `BaseException`), invoke `_terminate_active_processes()` which terminates and kills registered child processes and cleans up immediately. - **Future Cancellation**: Empty remaining queued futures from `ThreadPoolExecutor` with `pool.shutdown(wait=False, cancel_futures=True)` and cancel all uncompleted futures so no pending tests run. - **Responsive Wait Loop**: In parallel execution, poll with `concurrent.futures.wait(pending, timeout=0.2, return_when=FIRST_COMPLETED)` to ensure sub-second signal delivery across Windows and POSIX. - **Serial Mode**: In `-j 1` mode, run discover subprocess with responsive `proc.wait(timeout=0.2)` polling and clean interrupt termination. - **Exit Code & Output**: Return exit code 130 and output `'interrupted'` to `sys.stderr` without printing a false success summary. - **Regressions**: Add parallel cancellation, serial interrupt, direct process termination, and POSIX signal regression tests in `tests/test_run_semantic_tests.py`. Item: `SEMANTIC-RUNNER-INTERRUPT` AI-assisted (Claude Code)
Catch KeyboardInterrupt in run_semantic_tests.py, cancelling queued test modules via pool.shutdown(wait=False, cancel_futures=True) and terminating active child subprocesses so that no orphaned zombie processes or queued modules run after Ctrl+C. In serial mode (-j 1), terminate the discover subprocess on interrupt.

Return exit code 130 and output 'interrupted' to stderr without printing a false success summary.

Adds regression and lifecycle tests in tests/test_run_semantic_tests.py.

AI-assisted (Claude Code)
- On Windows, terminate child processes using taskkill /F /T /PID to ensure
  launcher processes (e.g. venv python.exe wrappers) terminate all child workers.
- Increase synthetic sleep duration in tests from 5s to 10s and timeout margin to
  eliminate timing flakiness under high machine load.

AI-assisted (Claude Code).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:11:49 +00:00
github manually merged commit 30a2a1862c into gh-pr/1936/base 2026-11-26 10:11:50 +00:00
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-25 16:55 UTC · original

Updated in commit \30a2a1862\ (cc @⁠Zalthor re verification feedback):

  1. Windows process tree termination:
    • Added _kill_process_tree\ using \ askkill /F /T /PID\ on Windows. This guarantees that if Python is invoked via a launcher/wrapper (such as \ env\Scripts\python.exe\ or Windows sandbox wrappers), the entire descendant process tree (including the worker interpreter) is forcefully terminated immediately rather than leaving orphaned child processes running.
  2. Test robustness under load:
    • Increased synthetic sleep duration in \ est_parallel_interrupt_stops_active_processes_and_cancels_queued_jobs\ and \ est_serial_interrupt_returns_130_and_prints_interrupted\ from 5 s to 10 s and relaxed elapsed ceiling to 5.0 s (with trigger wait up to 8.0 s).
    • This prevents machine load during Python interpreter startup from racing against the sleep interval while still definitively asserting that interruption completes in ~0.2 s–0.8 s (well below 5 s).
    • Verified 26/26 tests pass on Windows.

AI-assisted (Claude Code).

**@⁠abnalstlll-byte** on GitHub · 2026-11-25 16:55 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1936#issuecomment-7168748338) Updated in commit \30a2a1862\ (cc @⁠Zalthor re verification feedback): 1. **Windows process tree termination**: - Added \_kill_process_tree\ using \ askkill /F /T /PID\ on Windows. This guarantees that if Python is invoked via a launcher/wrapper (such as \ env\Scripts\python.exe\ or Windows sandbox wrappers), the entire descendant process tree (including the worker interpreter) is forcefully terminated immediately rather than leaving orphaned child processes running. 2. **Test robustness under load**: - Increased synthetic sleep duration in \ est_parallel_interrupt_stops_active_processes_and_cancels_queued_jobs\ and \ est_serial_interrupt_returns_130_and_prints_interrupted\ from 5 s to 10 s and relaxed elapsed ceiling to 5.0 s (with trigger wait up to 8.0 s). - This prevents machine load during Python interpreter startup from racing against the sleep interval while still definitively asserting that interruption completes in ~0.2 s–0.8 s (well below 5 s). - Verified 26/26 tests pass on Windows. AI-assisted (Claude Code).
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-25 19:21 UTC · original

Merged, thanks! The new tests pass here (26/26), and on main the test file stops on an uncaught KeyboardInterrupt. The Windows paths are guarded (taskkill under os.name == "nt", the killpg test skipped off POSIX).

  • Checked on main 205bda4a4 + 30a2a1862: the semantic suite passes (491 files, 6,312 tests, 0 files failed); test_run_semantic_tests pass; the 15 docs and writer tests pass; check_no_game_data.py is clean on the 2 changed files.
  • Optional follow-up: a stop flag checked before Popen would close the small window where a worker that already took a job still starts it after Ctrl+C.
  • Heads-up: #1749 and #1700 also change scripts/run_semantic_tests.py, so they now need main merged in.

AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠aslan2142** on GitHub · 2026-11-25 19:21 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1936#issuecomment-7172188516) Merged, thanks! The new tests pass here (26/26), and on `main` the test file stops on an uncaught `KeyboardInterrupt`. The Windows paths are guarded (`taskkill` under `os.name == "nt"`, the `killpg` test skipped off POSIX). - **Checked on `main` `205bda4a4` + `30a2a1862`:** the semantic suite passes (491 files, 6,312 tests, 0 files failed); `test_run_semantic_tests` pass; the 15 docs and writer tests pass; `check_no_game_data.py` is clean on the 2 changed files. - **Optional follow-up:** a stop flag checked before `Popen` would close the small window where a worker that already took a job still starts it after Ctrl+C. - **Heads-up:** #1749 and #1700 also change `scripts/run_semantic_tests.py`, so they now need `main` merged in. AI-assisted (Claude Code, Claude Opus 5.5).
This discussion has been locked. Commenting is limited to contributors.
No description provided.