Skip to content

Python: preserve idle shutdown after cancelled sends - #14853

Open
A_Zhe (1137423918) wants to merge 1 commit into
microsoft:mainfrom
1137423918:1137423918/fix/cancelled-send-handler-error
Open

A_Zhe (1137423918) wants to merge 1 commit into
microsoft:mainfrom
1137423918:1137423918/fix/cancelled-send-handler-error

Conversation

@1137423918

@1137423918 A_Zhe (1137423918) commented Nov 21, 2026 •

Copy link
Copy Markdown

Motivation and Context

Cancelling the task awaiting InProcessRuntime.send_message() cancels its response future. If the agent handler or asynchronous agent factory subsequently raises an ordinary exception, _process_send() calls set_exception() on that cancelled future. The resulting InvalidStateError prevents task_done() from running, so stop_when_idle() can wait indefinitely.

The regression uses the public runtime API and events to establish this ordering. It covers both an agent handler and an asynchronous factory, with a still-waiting caller as a control for each path. No model or external service is needed.

Description

Check future.cancelled() before setting the exception, following the existing CancelledError and response paths. Queue completion and exception logging continue for the cancelled caller. A still-waiting caller receives the original agent error.

One production guard changes; the four parameterized tests use the same test-file bytes before and after the fix. The branch is based on main aa46eddff989c13fba70c15e0ae8715b75f3fe8d and does not depend on the separate cancellation-callback PR #14845. The earlier merged intervention-accounting fix #14656 addresses a different path.

Server validation: Linux, Python 3.11.4, the same dedicated test environment for both checkouts.

Scope Result
Four new cases against original production code 2 cancelled-caller failures, 2 normal-error controls pass
Four cases with this guard 4 pass
Runtime and group-chat directories 97 pass, zero skips; includes the four cases
Original broader runtime/group-chat/orchestration scope, excluding the four new cases 172 pass, 12 fail, 1 skip
Candidate broader scope, including the four new cases 176 pass, 12 fail, 1 skip; identical failure IDs
Original pre-commit configuration, both changed files All applicable hooks pass; source bytes unchanged
Mypy, both changed files Pass
Wheel build Pass; wheel runtime bytes match the tested checkout

The scopes overlap and are not additive. The 12 broader orchestration failures remain on the unchanged base; this does not claim that the full SDK suite or upstream CI is green. The initial build invocation found no build frontend in the test environment. Packaging then passed in a separate locked build environment (build==1.3.0, flit-core==3.12.0, within the declared backend range). The original test environment and tested source bytes remained unchanged; successful tests were not rerun for that tooling correction.

Focused reproduction after applying only the new test file to the base checkout:

python -m pytest tests/unit/agents/runtime/test_runtime.py::test_late_send_failure_after_sender_cancellation

AI assistance: Codex produced the implementation, tests and server validation. Human author review and upstream review remain pending; this PR is a draft.

Contribution Checklist

@1137423918
A_Zhe (1137423918) marked this pull request as ready for review November 22, 2026 06:44
@1137423918
A_Zhe (1137423918) requested a review from a team as a code owner November 22, 2026 06:44
Copilot AI balanced review requested due to automatic review settings November 22, 2026 06:44

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 idle-shutdown cancellation fix.

No open issues 🎉

What changed in this PR

Fixes InProcessRuntime idle shutdown when a cancelled sender’s agent handler or asynchronous factory later raises an exception.

Changes:

  • Avoids setting an exception on a cancelled response future, allowing queue completion and exception logging to continue.
  • Adds four parameterized regression cases covering handler and factory failures, with cancelled and still-waiting callers.
File Description
python/​tests/​unit/​agents/​runtime/​test_runtime.py Adds event-coordinated tests for late failures, cancellation, and idle shutdown.
python/​semantic_kernel/​agents/​runtime/​in_process/​in_process_runtime.py Guards exception delivery when the response future is cancelled.

Review effort: Balanced

This branch was previously deployed

1 inactive deployment
github-app-auth — 9eadf244 Deployed Nov 21, 2026 by 1137423918 via add_label #29788
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.

2 participants