Skip to content

test(repo): the two Ctrl-C lifecycle tests wait for the cancel and the main thread's wait, not for time - #949

Merged
antonwolfy merged 3 commits into
intel:mainfrom
antonwolfy:test/ctrl-c-events
Nov 19, 2026
Merged

antonwolfy merged 3 commits into
intel:mainfrom
antonwolfy:test/ctrl-c-events

Conversation

@antonwolfy

@antonwolfy antonwolfy commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Two Ctrl-C tests in tests/test_repo_batch_lifecycle.py depend on timing, not on the thing they check.

Test On main today
test_an_interrupt_during_submission_starts_no_queued_cell (#918) each running cell sleeps 0.5 s and the test assumes the main thread cancels the queue within that time. With time.sleep(10) in the fake submit it fails: assert [1, 2, 3] == [1, 2]. (With 1 s, as in the issue, it still passes here: the cells take longer than that.)
test_ctrl_c_makes_the_repo_run_process_exit_nonzero (#948) the driver sends SIGINT with signal.pthread_kill from the first cell once submit ran five times. 2 of 64 runs of the test (8 in parallel) failed with AssertionError: (4, 'building dokimi/toy:... (tasks removed), the CI failure; 5 of 240 runs of the bare driver ended in exit 4. A traceback dump of the stuck driver shows the main thread in Future.result (Condition.wait) and both cells in cancelled.wait(30): the SIGINT arrived just before the main thread blocked in the wait, and CPython runs the handler on the next bytecode, not inside the lock wait, so the interrupt stayed pending until a cell ended.

#948 is a test-timing flake, not a defect of the batch's Ctrl-C handling: the same window exists for a real Ctrl-C, where the interrupt is delayed until a cell ends, not lost.
Closes #918
Closes #948

Changes

  • test_an_interrupt_during_submission_starts_no_queued_cell: wraps ThreadPoolExecutor.shutdown to set an event after a cancel_futures=True call returns; the two running cells wait on it (cancelled.wait(30)) instead of sleeping 0.5 s, and the test asserts both waits returned true; the time.sleep(0.2) before the interrupt in submit and the final time.sleep(1) are gone; started is asserted after both running cells have ended, as test_ctrl_c_starts_no_queued_cell does; each running cell signals entry and the fourth submit waits (30 s bound) for both signals before it raises KeyboardInterrupt, so a worker is never cancelled while still queued. The now unused import time is removed.
  • _CTRL_C_DRIVER of test_ctrl_c_makes_the_repo_run_process_exit_nonzero: wraps Future.result; when the main thread calls it, it waits until both cells run, then calls signal.raise_signal(SIGINT) in that thread, so the handler runs at once and the KeyboardInterrupt comes out of the wait in _run_cells. The submit counter and pthread_kill are gone. The assertions (exit code 130/-SIGINT, KeyboardInterrupt on stderr, the two finished cells) and the 30 s waits are unchanged.
  • No package code and no changelog fragment.

Risk

None for users: only tests change. The driver now raises the signal in the main thread instead of sending it from a worker, so it no longer covers a SIGINT that is sent from another thread while the main thread blocks; test_ctrl_c_starts_no_queued_cell still uses pthread_kill.

Testing

On Linux, at 65c8ea9c (on main 2ab246c):

  • make ci-local: 36 passed, 0 failed, 14 skipped (steps that need tools or jobs the host does not have, such as semgrep, micromamba and the other Python versions). 23691 tests passed, 19 skipped.
  • test_an_interrupt_during_submission_starts_no_queued_cell waits 0.5 s for the main thread to cancel the queue #918 on main's test with time.sleep(10) in submit: 1 failed, assert [1, 2, 3] == [1, 2]. The new test with the same 10 s sleep: 1 passed.
  • Flaky on CI: tests/test_repo_batch_lifecycle.py::test_ctrl_c_makes_the_repo_run_process_exit_nonzero #948 on main's test, 64 runs, 8 in parallel: 2 failed with exit 4 (the CI message); the new test, same load: 0 of 64 failed. The bare driver, 240 runs, 8 in parallel: main's 5 ended in exit 4, the new one 0.
  • Mutants, in a copy: dropping cancel_futures=True from _stop_on_interrupt (dokimi/repos/runner.py) fails both tests (test_ctrl_c_makes_the_repo_run_process_exit_nonzero: exit 4; test_an_interrupt_during_submission_starts_no_queued_cell: assert [False, False] == [True, True]). Dropping it and making _drop_if_halted a no-op fails assert [1, 2, 3] == [1, 2].
  • Entry handshake, in a copy: a second cell that never signals entry makes test_an_interrupt_during_submission_starts_no_queued_cell fail at the fourth submit instead of interrupting early.
  • Merged together with test/growth-bounds on main 2ab246c: pytest -n auto 23691 passed, 19 skipped, mypy clean, ruff check clean.
  • Not run: the matrix legs on Python 3.11, 3.12 and 3.14 (the failure was reported on 3.11; the host runs 3.13).

Open questions

None.

Revert

A plain revert restores the two tests as they are on main. Nothing is stored.

Checklist

  • Tests added/updated and pytest passes
  • ruff, mypy, and schema checks pass
  • Docs updated (or N/A) — see the docs-drift checklist in CONTRIBUTING.md
  • Self-reviewed per "Before opening a PR" in AGENTS.md
  • Each new raise/exit and test names the doc, contract or issue it traces to
  • No ad-hoc run artifacts committed
  • New files carry the SPDX license header (or are exempt in .github/.licenserc.yaml)

….5 s (intel#918)

test_an_interrupt_during_submission_starts_no_queued_cell had each running cell sleep 0.5 s and assumed the main thread raised the KeyboardInterrupt from the fourth submit and cancelled the queue within that time. With a main thread slower than the cells (a 10 s sleep in submit) the worker that ended first took cell 3 from the queue: assert [1, 2, 3] == [1, 2].

The test now wraps ThreadPoolExecutor.shutdown to set an event after a cancel_futures=True call returns, the two running cells wait on it and the main thread asserts both waits returned true, the sleep in submit and the final time.sleep(1) are gone, and the assertion on the started cells runs after both running cells have ended, as test_ctrl_c_starts_no_queued_cell does.

Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
…intel#948)

The driver of test_ctrl_c_makes_the_repo_run_process_exit_nonzero sent its SIGINT with signal.pthread_kill from the first cell, once ThreadPoolExecutor.submit had run five times. That could arrive just before the main thread blocked in Future.result: CPython runs the handler on the next bytecode, not inside the lock wait it was about to enter, so the interrupt stayed pending while both cells waited for a cancel that never came, and the driver ended with exit 4 after its 30 s wait (the CI failure; 5 of 240 driver runs and 2 of 64 runs of the test, 8 in parallel).

The driver now wraps Future.result: when the main thread calls it, it waits until both cells run, then calls signal.raise_signal(SIGINT) in that thread, so the handler runs at once and the KeyboardInterrupt comes out of the wait in _run_cells. The count of submits is gone. The assertions are unchanged: the exit code, KeyboardInterrupt on stderr and the two finished cells.

Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
@antonwolfy
antonwolfy requested a review from a team as a code owner November 18, 2026 23:39
@antonwolfy antonwolfy self-assigned this Nov 18, 2026
@exai-bot

exai-bot commented Nov 18, 2026 •

Copy link
Copy Markdown

✅ OpenCodeReview: Review complete: 0 finding(s) across 1 selected item(s).

CI at 65c8ea9: 24 passing, 2 skipped.

Review details
  • Commit reviewed: 65c8ea9 against merge base 2ab246c
  • Coverage: 1 files in 1 groups
  • Engine: OpenCodeReview v1.13.5-exai.4.8f73eada, us.openai.gpt-6.1-sol
  • Usage: 90k tokens (53k from cache), 40s
  • Tool calls: 9 (4 file_read, 3 code_search, 1 ci_status, 1 local_check_results)
  • dokimi/tests/test_repo_batch_lifecycle.py: 1 file

@codecov-commenter

codecov-commenter commented Nov 19, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.84%. Comparing base (2ab246c) to head (65c8ea9).
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #949   +/-   ##
=======================================
  Coverage   95.84%   95.84%           
=======================================
  Files         442      442           
  Lines       58843    58843           
  Branches    10148    10148           
=======================================
  Hits        56397    56397           
  Misses       1521     1521           
  Partials      925      925           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…nter run_cell before it interrupts

Settles the review point that the fourth submit could interrupt before the second worker had started, so rep 2 was cancelled while still queued and the test failed after its 30 s wait. Each run_cell now signals entry, and the fourth submit waits (bounded at 30 s) for both signals before it raises KeyboardInterrupt.

Signed-off-by: Anton Volkov <antonwolfy@gmail.com>

@vlad-perevezentsev vlad-perevezentsev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

I checked 65c8ea9: both Ctrl-C tests now wait on events (the cancel, both workers inside run_cell, the main thread's result() wait) instead of sleeps, and still check that no queued cell starts.

Merge: safe now: it merges cleanly into current main, 1 commit behind. Once it is in, #957 conflicts with it in tests/test_repo_batch_lifecycle.py and needs a rebase.

What I ran, at 65c8ea9:

  • make ci-local --offline: 0 failed, 25 passed, 20 skipped.
  • The two changed tests 8 times in a row: 16 passed.

Checked, no issues

  • Submission race: the entered handshake answers the bot's thread; rep 2 can no longer be cancelled while queued before the interrupt.
  • Driver: _run_cells calls result() in the main thread only after every cell is submitted, so the SIGINT lands in the wait; _stop_on_interrupt does not call result() again, so the patch raises it once.

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

Labels

None yet

Projects

None yet

4 participants