Repository navigation
test(repo): the two Ctrl-C lifecycle tests wait for the cancel and the main thread's wait, not for time - #949
Conversation
….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>
|
✅ OpenCodeReview: Review complete: 0 finding(s) across 1 selected item(s). CI at Review details
|
|
Codecov Report✅ All modified and coverable lines are covered by tests. 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:
|
…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
left a comment
There was a problem hiding this comment.
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
enteredhandshake answers the bot's thread; rep 2 can no longer be cancelled while queued before the interrupt. - Driver:
_run_cellscallsresult()in the main thread only after every cell is submitted, so theSIGINTlands in the wait;_stop_on_interruptdoes not callresult()again, so the patch raises it once.
Summary
Two Ctrl-C tests in
tests/test_repo_batch_lifecycle.pydepend on timing, not on the thing they check.maintodaytest_an_interrupt_during_submission_starts_no_queued_cell(#918)time.sleep(10)in the fakesubmitit 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)signal.pthread_killfrom the first cell oncesubmitran five times. 2 of 64 runs of the test (8 in parallel) failed withAssertionError: (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 inFuture.result(Condition.wait) and both cells incancelled.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: wrapsThreadPoolExecutor.shutdownto set an event after acancel_futures=Truecall 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; thetime.sleep(0.2)before the interrupt insubmitand the finaltime.sleep(1)are gone;startedis asserted after both running cells have ended, astest_ctrl_c_starts_no_queued_celldoes; each running cell signals entry and the fourthsubmitwaits (30 s bound) for both signals before it raisesKeyboardInterrupt, so a worker is never cancelled while still queued. The now unusedimport timeis removed._CTRL_C_DRIVERoftest_ctrl_c_makes_the_repo_run_process_exit_nonzero: wrapsFuture.result; when the main thread calls it, it waits until both cells run, then callssignal.raise_signal(SIGINT)in that thread, so the handler runs at once and theKeyboardInterruptcomes out of the wait in_run_cells. Thesubmitcounter andpthread_killare gone. The assertions (exit code130/-SIGINT,KeyboardInterrupton stderr, the two finished cells) and the 30 s waits are unchanged.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_cellstill usespthread_kill.Testing
On Linux, at
65c8ea9c(onmain2ab246c):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.main's test withtime.sleep(10)insubmit: 1 failed,assert [1, 2, 3] == [1, 2]. The new test with the same 10 s sleep: 1 passed.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.cancel_futures=Truefrom_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_halteda no-op failsassert [1, 2, 3] == [1, 2].test_an_interrupt_during_submission_starts_no_queued_cellfail at the fourthsubmitinstead of interrupting early.test/growth-boundsonmain2ab246c:pytest -n auto23691 passed, 19 skipped,mypyclean,ruff checkclean.Open questions
None.
Revert
A plain revert restores the two tests as they are on
main. Nothing is stored.Checklist
pytestpassesruff,mypy, and schema checks passAGENTS.mdraise/exit and test names the doc, contract or issue it traces to.github/.licenserc.yaml)