Skip to content

FIX Handle Ctrl+C cancellation in CLI scenario polling (#3747) - #3813

Merged
Roman Lutz (romanlutz) merged 2 commits into
microsoft:mainfrom
Rohitpro1:fix/3747-cli-sigint-cancellation
Nov 4, 2026
Merged

Roman Lutz (romanlutz) merged 2 commits into
microsoft:mainfrom
Rohitpro1:fix/3747-cli-sigint-cancellation

Conversation

@Rohitpro1

@Rohitpro1 Rohit Kumar P (Rohitpro1) commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #3747.

When Ctrl+C arrives during scenario polling, asyncio.run() cancels the main task and raises asyncio.CancelledError at the polling await. The polling handler only caught KeyboardInterrupt, so the server cancellation endpoint was skipped and the accepted run continued.

Handle both cancellation forms, run the server cancellation request in its own task, and shield that request from the cancellation already delivered to the polling task. Report that the request was sent only after it returns successfully; report a warning if it fails.

Tests and Documentation

  • .venv/bin/pytest -q tests/unit/cli/test_pyrit_scan.py — 157 passed.
  • Ruff lint and format checks on both changed files — passed.
  • ty check on pyrit/cli/pyrit_scan.py and tests/unit/cli/test_pyrit_scan.py — passed.
  • tests/unit/build_scripts/test_codeql_merge_group.py::test_codeql_merge_group_scenarios with Node.js v22.22.0 — 1 passed.
  • Full unit diagnostic (.venv/bin/pytest -q -n 4 tests/unit) — 32,159 passed, 478 skipped, 139 subtests passed; 2 failures. The CodeQL test initially could not find Node and passed when rerun with Node installed. The other failure is in the unrelated test_assert_generator.py::TestAssertGenerator::test_separate_research_target_receives_research_stages_async, which raises UnboundLocalError for local call; it also fails in isolation.
  • Documentation changes: none needed for this CLI bug fix.

@Rohitpro1

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@romanlutz Roman Lutz (romanlutz) self-assigned this Nov 4, 2026
Copilot AI balanced review requested due to automatic review settings November 4, 2026 07:54

Copilot AI 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.

🟢 Approval recommended

No active review issues are reported for the CLI polling cancellation change.

No open issues 🎉

What changed in this PR

Fixes #3747 by handling Ctrl+C cancellation during CLI scenario polling so the CLI requests server-side cancellation instead of leaving the accepted run executing.

Changes:

  • Catches both KeyboardInterrupt and asyncio.CancelledError during polling.
  • Sends cancellation in a shielded task and reports success only after the request returns successfully.
  • Adds a regression test verifying that polling cancellation sends the server request and returns exit code 1.
File Description
tests/​unit/​cli/​test_pyrit_scan.py Adds regression coverage for cancellation during polling.
pyrit/​cli/​pyrit_scan.py Handles polling cancellation and shields the server cancellation request.

Review effort: Balanced

@romanlutz
Roman Lutz (romanlutz) added this pull request to the merge queue Nov 4, 2026
Merged via the queue into microsoft:main with commit 56c401e Nov 4, 2026
65 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLI Ctrl+C bypasses server cancellation and leaves the scenario running

3 participants