Skip to content

docs: add CancellationToken propagation guide - #8148

Closed
MOHAMMED WASIM KHAN (wasim-builds) wants to merge 1 commit into
microsoft:mainfrom
wasim-builds:fix/8091-cancellation-docs
Closed

MOHAMMED WASIM KHAN (wasim-builds) wants to merge 1 commit into
microsoft:mainfrom
wasim-builds:fix/8091-cancellation-docs

Conversation

@wasim-builds

Copy link
Copy Markdown

Summary

Fixes #8091

Added documentation showing how to propagate CancellationToken through nested tools using cancellation_token parameter and cancellation_token.link_future().

Changes

  • Added CancellationToken propagation section to migration guide
  • Shows two methods: using cancellation_token parameter and link_future()
  • Includes code examples for both approaches

Test plan

  • Documentation builds correctly

Added documentation showing how to propagate CancellationToken
through nested tools using cancellation_token parameter and
cancellation_token.link_future().

Fixes microsoft#8091
Copilot AI lite review requested due to automatic review settings September 2, 2026 05:14

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.

🟡 Changes recommended

The new docs section includes a misleading first example and a non-runnable second example, and it does not yet demonstrate a deterministic cancellation result as required by the linked issue.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Adds a new documentation section to the AgentChat migration guide explaining how to propagate autogen_core.CancellationToken so cancellation is observed across nested tool operations.

Changes:

  • Added a “CancellationToken Propagation Through Nested Tools” section to the migration guide.
  • Documented two propagation patterns: accepting a cancellation_token parameter and using cancellation_token.link_future().
  • Included example snippets illustrating both approaches.
File summaries
File Description
python/docs/src/user-guide/agentchat-user-guide/migration-guide.md Adds a dedicated section describing CancellationToken propagation and providing example code patterns.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 3
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +317 to +320
async def slow_operation(duration: int, cancellation_token: CancellationToken) -> str:
# Forward the cancellation token to nested async operations
await asyncio.sleep(duration) # This will be cancelled when token is cancelled
return "Operation completed"
Comment on lines +328 to +332
async def custom_operation(cancellation_token: CancellationToken) -> str:
future = asyncio.ensure_future(some_async_work())
cancellation_token.link_future(future)
result = await future
return result
Comment on lines +335 to +336
When the cancellation token is cancelled, the linked future will be cancelled,
causing the `await` to raise `CancelledError`.
@wasim-builds

Copy link
Copy Markdown
Author

Superseded by #8184

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.

Document CancellationToken propagation through nested tools

2 participants