← Back to Pull requests

Keep simulated Ctrl+C inside the interrupt tests #2427

Open
unknown opened this pull request with 595 lines diff across 7 files
View on GitHub
unknown unknown commented on Nov 12, 2026 20:44 UTC
Author

What
----
The interrupt tests in tests/test_Wakeable.py, and two in tests/test_ShareConsumer.py, start a thread that calls _thread.interrupt_main() after a fixed delay, and expect the KeyboardInterrupt inside a try/except around the blocking call. When the call returns first, or the thread wakes up late, the KeyboardInterrupt is raised later: in cleanup, in another test, or in pytest itself, which then aborts the whole session. This happened in the first CI run of #2416 on the macOS x64 agent (job): pytest stopped with KeyboardInterrupt at <frozen posixpath>:457 after 16 tests of test_Wakeable.py, and the rerun passed.

The 17th test, test_can_be_interrupted[producer-poll], never interrupted poll(). Its producer has bootstrap.servers: localhost:9092, and with no broker there, Producer.poll() returns 1 or 2 within a few milliseconds, as soon as it has served the "connection refused" error. The test only passed because the interrupt landed in the 0.2 s sleep of its finally block; an interrupt more than about 0.1 s late escaped.

This adds TestUtils.interrupt_main_after(delay), a context manager for the blocking call. Leaving the block waits for the interrupt, so the KeyboardInterrupt is always raised inside the block; a call that returns before it is interrupted fails with AssertionError: Not interrupted: ... instead. All 23 simulated interrupts use it. The producer in test_can_be_interrupted no longer sets bootstrap.servers, like the producers of the other interrupt tests, so poll() blocks until it is interrupted, and the sleeps that waited for late interrupts are gone. The two 1 s polls interrupted after 0.4 s get the 2 s timeout of the others, so an interrupt can be up to 1.4 s late everywhere (4.6 s in test_ShareConsumer.py).

Checklist
------------------

  • Contains customer facing changes? Including API/behavior changes

  • - No, tests only.
  • Did you add sufficient unit test and/or integration test coverage for this PR?

  • - Test-only change; see below for how it was checked.

    References
    ----------

  • First CI run of #2416, "Source package verification with Python 3 (OSX x64) +docs": https://confluentinc.semaphoreci.com/jobs/9e8ccc9a-8367-41f5-a9f3-5dd00854e6c7

  • Same failure in the first CI run of #2426, on master at the time: pytest stopped with KeyboardInterrupt at <frozen posixpath>:457 after the same 16 tests of test_Wakeable.py: https://confluentinc.semaphoreci.com/jobs/04d2d52e-aecf-402f-8c9d-6274e3e56078
  • Test & Review
    ------------

  • test_can_be_interrupted[producer-poll] on master, traced outside pytest (Python 3.11.16): poll() returned after 1 to 6 ms in all 11 runs, 8 of them under a 10% CPU quota, and the interrupt was always caught by the finally sleep. On this branch, with only the context manager and the old producer config, the test fails every time with AssertionError: Not interrupted: the block ended before the interrupt sent after 0.1s.

  • Late interrupts, injected with a pytest plugin that delays TestUtils.send_sigint_after_delay(), running all 27 interrupt tests (Python 3.11.16, Linux x86_64):

  • - master, 0.3 s late: all 4 runs broken. test_can_be_interrupted[producer-poll] failed in 3, and 1 run aborted with KeyboardInterrupt in obj.close() of the next test, after "1 failed, 16 passed", as in CI.
    - this branch, 0.3 s late: 4 runs, all passed. In 4 more runs made while the full unit suite ran in parallel, one failed an elapsed-time bound: poll(timeout=1.0) took 2.51 s in test_calculate_chunk_timeout_utility_function[consumer].
    - this branch, 1.0 s late: 2 runs, all passed.
    - this branch, 2.0 s late, beyond the 1.4 s margin: the 11 tests that interrupt a call with a finite timeout fail with AssertionError: Not interrupted: ..., the other 16 pass, and the session runs to the end.
  • CPU throttling alone did not reproduce the failure on Linux: master passed 30 runs of test_can_be_interrupted under a 5% CPU quota with 500 ms periods. Throttled threads wake together, and the interrupting thread, which wakes first, also runs first.

  • Without injected delays, the 27 tests pass on Python 3.9, 3.11, 3.14 and 3.14t, and test_can_be_interrupted passes 30 runs out of 30 under the 5% CPU quota.

  • tools/style-format.sh on the changed files and flake8 pass.
  • Open questions / Follow-ups
    --------------------------

  • The tests also assert elapsed times, such as poll(timeout=1.0) returning within 2 s. Those bounds can still fail on a loaded machine; this PR does not change them.
  • 🤖 Generated with Claude Code

    https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN

    Diff: 595 lines (+446 / -149) across 7 files Reviewer: pylaterreur Review duration: 12391m
    All Vouch review confidence checks passed
    Residual risk 0.27 = Change Risk (0.50) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 595 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.382
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #2427"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50% (MEDIUM) · Higher = larger blast radius
    50%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 26.7% · Safe to merge
    26.7%