Repository navigation
Python: (fix) Published HandoffRequest message had no cancellation token attached - #13130
Open
Audric Schiltknecht (audricschiltknecht) wants to merge 2 commits into
Conversation
Audric Schiltknecht (audricschiltknecht)
requested a review
from a team
as a code owner
September 16, 2025 15:15
Author
|
I would need a bit of help to add testing (if possible at all). I was looking at inspiration from the |
Collaborator
|
Audric Schiltknecht (@audricschiltknecht) please have a look at this test: @pytest.mark.skipif(
sys.version_info < (3, 11),
reason="Python 3.10 doesn't bound the original function provided to the wraps argument of the patch object.",
)
async def test_invoke_cancel_during_handoff():
"""Test that cancellation propagates through handoff requests.
When agent A hands off to agent B and the orchestration is cancelled,
agent B should not process the handoff request because the cancellation
token should be propagated with the HandoffRequestMessage.
"""
agent_b = MockAgent()
agent_a = MockAgentWithHandoffFunctionCall(agent_b)
with (
patch.object(MockAgent, "invoke_stream", wraps=MockAgent.invoke_stream, autospec=True) as mock_invoke_stream,
):
runtime = InProcessRuntime()
runtime.start()
try:
orchestration = HandoffOrchestration(
members=[agent_a, agent_b],
handoffs={
agent_a.name: {agent_b.name: "test"},
agent_b.name: {agent_a.name: "test"},
},
)
orchestration_result = await orchestration.invoke(task="test_message", runtime=runtime)
# Wait long enough for agent_a to complete its handoff (~0.1s)
# but cancel before agent_b processes the request
await asyncio.sleep(0.15)
orchestration_result.cancel()
finally:
await runtime.stop_when_idle()
# Agent B should NOT have been invoked because the cancellation token
# was propagated with the HandoffRequestMessage and the ActorBase
# checks `ctx.cancellation_token.is_cancelled()` before processing.
# Without the fix, agent B receives a fresh (non-cancelled) token
# and processes the request anyway.
assert mock_invoke_stream.call_count == 0 |
Eduard van Valkenburg (eavanvalkenburg)
had a problem deploying
to
github-app-auth
September 28, 2026 13:53 — with
GitHub Actions
Failure
Copilot started reviewing on behalf of
Audric Schiltknecht (audricschiltknecht)
September 28, 2026 13:54
Contributor
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Add regression coverage verifying cancellation after a handoff is published.
Review effort: Lite
Findings: 1
What changed in this PR
Fixes cancellation-token propagation for Python handoff requests.
Changes:
- Reuses the orchestration cancellation token during handoffs.
- Preserves cancellation behavior across agent transitions.
| File | Description |
|---|---|
python/semantic_kernel/agents/orchestration/handoffs.py |
Propagates the cancellation token during handoff requests. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| await self.publish_message( | ||
| HandoffRequestMessage(agent_name=self._handoff_agent_name), | ||
| TopicId(self._internal_topic_type, self.id.key), | ||
| cancellation_token=cts.cancellation_token, |
Eduard van Valkenburg (eavanvalkenburg)
had a problem deploying
to
github-app-auth
September 28, 2026 14:09 — with
GitHub Actions
Failure
This branch had an error being deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Motivation and Context
When a handoff occurs, the HandoffRequest was not passed the original cancellation token, leading to the creation of a separate token. This made the OrchestrationResult impossible to cancel/stop the runtime as the agent are testing for the wrong token in the
HandoffAgentActor::_handle_request_message()method.Description
Simply pass the cancellation token in the
publish_message()call.Contribution Checklist