Skip to content

Python: avoid cancellation callback deadlocks - #14845

Open
A_Zhe (1137423918) wants to merge 1 commit into
microsoft:mainfrom
1137423918:fix/cancellation-callback-deadlock
Open

A_Zhe (1137423918) wants to merge 1 commit into
microsoft:mainfrom
1137423918:fix/cancellation-callback-deadlock

Conversation

@1137423918

Copy link
Copy Markdown

Motivation and Context

Python runtime cancellation can deadlock when an application cleanup callback reads its own token, registers another cleanup callback, or waits for another thread to inspect the token. Both cancel() and registration after cancellation execute application callbacks while holding the token's non-reentrant lock.

This was found through source inspection and reproduced with the public experimental InProcessRuntime: a handler links a pending future, and a cleanup callback calls is_cancelled(). The original runtime enters the callback and blocks reacquiring the token lock. This is a controlled reproduction, not a reported production incident.

token = CancellationToken()
token.add_callback(lambda: token.is_cancelled())
token.cancel()  # Blocks on the original implementation.

Description

Update cancellation state and detach the registered callbacks under the lock, then invoke them after releasing it. Dispatch callbacks registered after cancellation outside the lock as well, and use add_callback() when linking futures. Public signatures, exception propagation, and repeated-cancel behavior remain unchanged.

Tests cover callback reentry before/after cancellation, waiting for a separate reader, future linking, concurrent cancellation, and callback exceptions. Callback code can still block or raise; this does not address arbitrary-thread asyncio future loop affinity or promise ordering among concurrent registrations.

Contribution Checklist

Validation on Linux / Python 3.11.4, against aa46eddff989c13fba70c15e0ae8715b75f3fe8d:

  • The 10 new cases: original 6 failed / 4 passed; candidate 10 passed.
  • Complete tests/unit/agents/runtime and tests/unit/agents/test_group_chat: 103 passed, including the new cases.
  • Adding tests/unit/agents/orchestration: candidate 182 passed / 12 failed / 1 skipped; original in the same environment 176 passed / 18 failed / 1 skipped. Excluding the six fixed regressions, the failing node IDs match exactly. The broader suite is not green; the existing orchestration failures remain unresolved. These scopes overlap.
  • The same real-runtime driver observes the original callback hang with a stack in is_cancelled(), then bounds and reaps its child. The candidate completes cleanup once, cancels the request and handler, and stops/closes the runtime. No model or external service was used.
  • Applicable original pre-commit hooks on the two changed files pass without changing their bytes. Exact original hook-tag source mirrors were used after the server's GitHub dependency fetch failed; the configuration and enabled hooks were unchanged.
  • Mypy with the project's configuration and --follow-imports=silent: no issues in the two changed files. Wheel build succeeds, and the packaged token file matches the tested source; the existing uv prerelease-setting deprecation warning is retained.

The dedicated environment uses the upstream lock's base/development dependencies plus its exact protobuf==5.29.6; it is not an all-extras CI environment. Full repository/platform CI is not claimed. This remains a draft for author and maintainer review.

AI assistance: Codex prepared the patch, regression tests, source review, and server validation. Human author review is pending; no human review or CLA completion is being asserted.

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

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 cancellation-token changes intended to prevent callback deadlocks.

No open issues 🎉

What changed in this PR

Updates Python runtime cancellation tokens to prevent deadlocks when cleanup callbacks access the token or wait for another thread to inspect it.

Changes:

  • Runs callbacks outside the token lock while keeping cancellation state updates protected, including callbacks registered after cancellation.
  • Routes future linking through add_callback() to share cancellation handling.
  • Adds regression tests for callback reentry, cross-thread reads, future cancellation, concurrent cancellation, and exception propagation.
File Description
python/​tests/​unit/​agents/​runtime/​test_cancellation_token.py Adds cancellation deadlock and behavior regression tests.
python/​semantic_kernel/​agents/​runtime/​core/​cancellation_token.py Moves callback execution outside the lock and centralizes future callback registration.

Review effort: Balanced

@1137423918

A_Zhe (1137423918) commented Nov 21, 2026 via email

Copy link
Copy Markdown
Author

@1137423918

A_Zhe (1137423918) commented Nov 21, 2026 via email

Copy link
Copy Markdown
Author

This branch was successfully deployed

1 active deployment
github-app-auth — 891744aa Deployed Nov 20, 2026 by 1137423918 via add_label #29774
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