Skip to content

Per-call CancelToken is registered but never cancelled by inbound notifications/cancelled #58

Not planned

Description

@tercel

Summary

Python registers a per-call CancelToken, attaches it to the execution Context, and exposes ExecutionRouter.cancel(call_id). But nothing in the transport calls cancel() when an inbound MCP notifications/cancelled arrives, and extra carries no abort signal. A running module therefore cannot observe context.cancel_token.is_cancelled.

This corrects the original report: the premise that "only Rust implements it" is wrong. The verified cross-language state is:

SDK Per-call token attached Notification flips it Evidence
Python Yes (router.py:287-343, 506) No router.py:111-112, 618-654 describe forwarding; no transport caller; extra has no signal
TypeScript Yes (router.ts:412-437, 514-526) Yes SDK extra.signal → cancelToken.cancel() (router.ts:425-427, factory.ts:658-674)
Rust Yes (router.rs:926-950, 1417-1477) No cancel_call only in tests; notifications wired to the async bridge only

The shared feature doc also contradicts itself: apcore-mcp/docs/features/execution-router.md:158 says Rust handles this, while :153 says synchronous calls are NOT cancelled. The code matches :153.

Evidence

  • src/apcore_mcp/server/router.py:287-343 — token registered before dispatch, removed in finally
  • src/apcore_mcp/server/router.py:506 — token passed into Context.create
  • src/apcore_mcp/server/router.py:618-654 — cancel(call_id) exists and cancels/tombstones
  • No caller of router.cancel in server/transport.py or server/factory.py; extra keys are call_id, progress_token, send_notification, session (no signal)
  • src/apcore_mcp/server/transport.py:40 — transport_session_var is for async-task session keys, not per-call cancellation

Impact

  • A cancelled Python tool call keeps running (subprocess, render, file lock) after the client disconnects.
  • Behavior differs from TypeScript, breaking the parity promise.
  • The router docstring claims transport wiring that does not exist, so integrators over-trust it.

Proposed fix

  • Forward the inbound cancellation to the per-call token: hook notifications/cancelled (or the SDK's abort signal) to router.cancel(call_id), populating call_id from the JSON-RPC request id; or pass an abort signal through extra and cancel the token on it.
  • Correct the router docstring and the shared feature doc (execution-router.md:153,158) to one statement.
  • Add a cross-language conformance fixture for cancellation.

Acceptance criteria

  • A Python sync tool observes context.cancel_token.is_cancelled after notifications/cancelled.
  • The tool can terminate its subprocess and return/raise cleanly.
  • stdio and streamable-http are both covered.
  • A shared fixture pins the behavior for Python, TypeScript, and Rust.

Related

  • Central cross-language issue: aiperceivable/apcore-mcp#21
  • TypeScript reference implementation: router.ts:412-437, factory.ts:658-674

Activity

  1. added
    bug
    on Nov 23, 2026
  2. changed the title [-]Cooperative cancel token is not wired for synchronous tool calls[/-] [+]Per-call CancelToken is registered but never cancelled by inbound notifications/cancelled[/+] on Nov 23, 2026
  3. tercel commented on Nov 23, 2026

    @tercel
    ContributorAuthor

    Cross-language follow-up (corrects the original premise): TypeScript is already wired via the SDK abort signal; Rust is also missing the notification->token wiring. Central issue: #122.

  4. tercel commented on Nov 29, 2026

    @tercel
    ContributorAuthor

    Duplicate of #122. The direct-call cancellation behavior landed in #124. Use #122 to track the remaining shared acceptance and documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions