Fix flaky `test_timeout_interrupts_code_that_keeps_printing` by starting its timeout once the cell runs

Merged
DS
23d ago
+29-16

This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.

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 bodykernel stallrunskernel shut down (the CI failure)
before0.6 s1010
after0.6 s100
before / afternone10 / 100 / 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 afteripykernel 6.29.0 (CI)ipykernel 7.4.0
the send, 0-3 ms (old test's anchor)73 / 400 missed86 / 400 missed
the cell reports it's running, 0-3 ms (new anchor)1 / 400 (shell wedged)0 / 400
the first tick, 0-100 ms0 / 2,8000 / 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 -

2026-11-13 · pre-push review 1 of 3 at 860abad537a289a42e670b1eb3f1987eb950da0e · iter -

2026-11-13 · pre-push review 1 of 3 at 860abad537a289a42e670b1eb3f1987eb950da0e · iter -

2026-11-13 · shared entered fixture · iter -

  • Decision: Option A: extract the frozen-until-entered clock into an entered fixture 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 -

2026-11-13 · pre-push review 2 of 3 at 8de80d3eba07bbb009aa3e4690b91a0dc775b02d · iter -

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.

  • Extracts the shared frozen clock into an entered fixture both tests use.
  • Test-only change; no public API impact.
  • Closes #13344. A separate kernel-shell wedge on Python 3.14 is tracked in #13352.