Fix flaky `test_timeout_interrupts_code_that_keeps_printing` by starting its timeout once the cell runs
This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.
- Closes #13344
test_timeout_interrupts_code_that_keeps_printing failed in test on 3.14 (lowest-versions, shard 2/2) on #13323 with "did not stop when interrupted, so the kernel was shut down" (call 5.62 s), and passed on the rerun.
Root cause. The test's 0.5 s timeout runs on the real clock and counts from when the cell is sent (IPythonKernel._run_cell starts _CellDeadline). ipykernel ignores SIGINT until it starts the cell: IPKernelApp.init_signal sets SIG_IGN, and Kernel.pre_handler_hook installs default_int_handler only while a message handler runs. When a loaded runner starts the cell more than 0.5 s late, the interrupt is dropped, the 5 s loop outlives the 5 s grace period, and the kernel is shut down.
Fix. The timeout's clock stands still until the cell calls entered(), as test_timeout_is_reported_when_the_code_catches_the_interrupt already did; that clock is now an entered fixture both tests share. After entered() it runs on the real clock, so the test still checks that a cell that keeps printing gets interrupted.
The other possible cause of the CI failure, a kernel shell that wedges on Python 3.14 when the interrupt lands inside print, is tracked in #13352.
New public surface: none.
Verification. Forced the failure against a real kernel by stopping it (SIGSTOP) for 0.6 s right after the timed cell is sent, the way a loaded runner deschedules it:
| test body | kernel stall | runs | kernel shut down (the CI failure) |
|---|---|---|---|
| before | 0.6 s | 10 | 10 |
| after | 0.6 s | 10 | 0 |
| before / after | none | 10 / 10 | 0 / 0 |
The whole test module passes.
What changes for existing users: nothing, it's a test-only change.
Interrupt sweep against a real kernel
interrupt_kernel() called a random delay after an anchor, with the test's loop cut to 10 iterations. A miss is a cell that ran to its end, or one ipykernel's own handler swallowed (KeyboardInterrupt caught in kernel), or one whose shell stopped answering.
| interrupt after | ipykernel 6.29.0 (CI) | ipykernel 7.4.0 |
|---|---|---|
| the send, 0-3 ms (old test's anchor) | 73 / 400 missed | 86 / 400 missed |
| the cell reports it's running, 0-3 ms (new anchor) | 1 / 400 (shell wedged) | 0 / 400 |
the first tick, 0-100 ms | 0 / 2,800 | 0 / 400 |
The new test's interrupt comes 0.5 s after entered(), not 0-3 ms, so the cell is already in its loop. The stall probe ran the new clock inline, before it moved into the fixture unchanged.
PR decisions
2026-11-13 · simple path, implement directly · iter -
- Decision: initialize-worktree Step 4: simple, implement directly; flake-fix path of pushing-commits-to-the-repo
- Why: Manager dispatch settled it: one test, flake fix
- Source: https://github.com/pydantic/pydantic-ai/issues/13344
- Session: cd7f91fe-3a6b-45d0-8b8d-f7743263e83f
2026-11-13 · flake claim · iter -
- Decision: Claimed #13344 for branch ipython-printing-timeout-flake; issue was assigned to dsfaccini with no claim comment (David's own claim, delegated)
- Why: Flake claims rule; fix PR must open within 1 h of claim (by 2026-11-13T09:15Z)
- Source: https://github.com/pydantic/pydantic-ai/issues/13344#issuecomment-6847738455
- Session: cd7f91fe-3a6b-45d0-8b8d-f7743263e83f
2026-11-13 · pre-push review 1 of 3 at 860abad537a289a42e670b1eb3f1987eb950da0e · iter -
- Decision: reserved; dispatching fresh Haiku reviewer (headless claude -p from policy-base checkout, read-only tools)
- Why: tree 31734653d02e3438bf78a09f4caabea637740341; full branch e65f5e9ac8ca01ffd1ad8c91d6f5a006d10d250a..860abad537a289a42e670b1eb3f1987eb950da0e; policy-base e65f5e9ac8ca01ffd1ad8c91d6f5a006d10d250a; first review
- Source: https://github.com/pydantic/pydantic-ai/tree/ipython-printing-timeout-flake
- Session: cd7f91fe-3a6b-45d0-8b8d-f7743263e83f
2026-11-13 · pre-push review 1 of 3 at 860abad537a289a42e670b1eb3f1987eb950da0e · iter -
- Decision: 1 finding: frozen-clock block duplicates test_timeout_is_reported_when_the_code_catches_the_interrupt; extract a shared fixture. Push paused: extraction changes shared code, which the dispatch requires reporting first
- Why: reviewed tree 31734653d02e3438bf78a09f4caabea637740341; full branch e65f5e9ac..860abad53; agent_docs/index.md rule 2 (extract after 2+ occurrences)
- Source: https://github.com/pydantic/pydantic-ai/tree/ipython-printing-timeout-flake
- Session: cd7f91fe-3a6b-45d0-8b8d-f7743263e83f
2026-11-13 · shared entered fixture · iter -
- Decision: Option A: extract the frozen-until-entered clock into an
enteredfixture shared by test_timeout_interrupts_code_that_keeps_printing and test_timeout_is_reported_when_the_code_catches_the_interrupt; re-review under budget - Why: agent_docs/index.md: extract logic used twice; patch leaves #12633's test and #12751's lines alone
- Source: https://github.com/pydantic/pydantic-ai/issues/13344
- Session: cd7f91fe-3a6b-45d0-8b8d-f7743263e83f
2026-11-13 · close #13344; ipykernel mid-print wedge as follow-up · iter -
- Decision: This PR closes #13344; file the ipykernel 6.29-7.0 mid-print interrupt wedge via /file-followup-issue (no labels, no assignee); PR body names it in one line as the other possible CI cause
- Why: PR removes the race reproduced 10/10 under a stall; the wedge is ipykernel's and needs its own floor-raise decision
- Source: https://github.com/pydantic/pydantic-ai/issues/13344
- Session: cd7f91fe-3a6b-45d0-8b8d-f7743263e83f
2026-11-13 · pre-push review 2 of 3 at 8de80d3eba07bbb009aa3e4690b91a0dc775b02d · iter -
- Decision: reserved; dispatching fresh Haiku reviewer (headless claude -p from policy-base checkout, read-only tools)
- Why: tree a613c8b06bb17829b54340275bea074bf9553401; follow-up scope 860abad537a289a42e670b1eb3f1987eb950da0e..8de80d3eba07bbb009aa3e4690b91a0dc775b02d (last reviewed tree 31734653d02e3438bf78a09f4caabea637740341) plus full branch as context; policy-base e65f5e9ac8ca01ffd1ad8c91d6f5a006d10d250a; verifies review-1 duplication finding
- Source: https://github.com/pydantic/pydantic-ai/tree/ipython-printing-timeout-flake
- Session: cd7f91fe-3a6b-45d0-8b8d-f7743263e83f
2026-11-13 · pre-push review 2 of 3 at 8de80d3eba07bbb009aa3e4690b91a0dc775b02d · iter -
- Decision: no findings: current at 8de80d3eba07bbb009aa3e4690b91a0dc775b02d; review-1 duplication finding verified fixed
- Why: reviewed tree a613c8b06bb17829b54340275bea074bf9553401; delta 860abad53..8de80d3eb plus full branch context
- Source: https://github.com/pydantic/pydantic-ai/tree/ipython-printing-timeout-flake
- Session: cd7f91fe-3a6b-45d0-8b8d-f7743263e83f
2026-11-13 · defer: kernel shell wedge on Python 3.14 when an interrupt lands inside print · iter -
- Decision: Filed as #13352 instead of fixing here (no labels, no assignee; manager routes). The ipykernel floor raise was dropped as the open decision: 7.4.0 also wedges on Python 3.14 (12/300); only Python 3.15 was clean
- Why: Out of scope for this PR: the wedge is in the kernel process and its remedy needs its own investigation and decision
- Source: https://github.com/pydantic/pydantic-ai/issues/13352
- Session: cd7f91fe-3a6b-45d0-8b8d-f7743263e83f
2026-11-13 · final metadata check · iter -
- Decision: Added topic labels codemode and cancellation/interruption; title and body current
- Why: Push skill: add existing topic labels for every subject materially covered; precedent #12146 carries cancellation/interruption
- Source: https://github.com/pydantic/pydantic-ai/pull/13353
- Session: cd7f91fe-3a6b-45d0-8b8d-f7743263e83f
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
- PR title is fit for the release changelog.
https://claude.ai/code/session_01HhQjZ9XCfiBUzQLKoDBG97
Summary by cubic
Fixes the flaky test_timeout_interrupts_code_that_keeps_printing by starting the timeout only after the cell reports it is running.
ipykernel ignores SIGINT arriving before a cell starts, so on a loaded kernel the 0.5 s timeout could pass early, drop the interrupt, and let the loop outlive the grace period until the kernel was shut down. The timeout's clock now stands still until the cell calls entered(), then runs on the real clock, matching what test_timeout_is_reported_when_the_code_catches_the_interrupt already does.