{"record":{"id":"8e35dc58847125c2","repo":"langchain-ai/deepagents","slug":"failed-to-cancel-run-e","errorCode":null,"errorMessage":"Failed to cancel run: {e}","messagePattern":"Failed to cancel run: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"libs/deepagents/deepagents/middleware/async_subagents.py","lineNumber":596,"sourceCode":"\n\ndef _build_cancel_tool(\n    clients: _ClientCache,\n) -> StructuredTool:\n    \"\"\"Build the `cancel_async_task` tool.\"\"\"\n\n    def cancel_async_task(\n        task_id: str,\n        runtime: ToolRuntime,\n    ) -> str | Command:\n        tracked = _resolve_tracked_task(task_id, runtime)\n        if isinstance(tracked, str):\n            return tracked\n\n        try:\n            client = clients.get_sync(tracked[\"agent_name\"])\n            client.runs.cancel(thread_id=tracked[\"thread_id\"], run_id=tracked[\"run_id\"])\n        except Exception as e:  # noqa: BLE001  # get_sync() may raise ValueError; SDK raises untyped errors\n            return f\"Failed to cancel run: {e}\"\n        now = datetime.now(UTC).strftime(\"%Y-%m-%dT%H:%M:%SZ\")\n        updated = AsyncTask(\n            task_id=tracked[\"task_id\"],\n            agent_name=tracked[\"agent_name\"],\n            thread_id=tracked[\"thread_id\"],\n            run_id=tracked[\"run_id\"],\n            status=\"cancelled\",\n            created_at=tracked[\"created_at\"],\n            last_checked_at=now,\n            last_updated_at=now,\n        )\n        msg = f\"Cancelled async subagent task: {tracked['task_id']}\"\n        return Command(\n            update={\n                \"messages\": [ToolMessage(msg, tool_call_id=runtime.tool_call_id)],\n                \"async_tasks\": {tracked[\"task_id\"]: updated},\n            }","sourceCodeStart":578,"sourceCodeEnd":614,"githubUrl":"https://github.com/langchain-ai/deepagents/blob/a1af029e6e73cb17c36bff823d227747b28e91e1/libs/deepagents/deepagents/middleware/async_subagents.py#L578-L614","documentation":"cancel_async_task wraps client creation and runs.cancel in a broad except and returns this message instead of raising. Failures from get_sync (no url configured) or the SDK cancel call are reported to the agent as this string.","triggerScenarios":"Cancelling an async task whose subagent spec lacks a url (get_sync ValueError); cancel RPC fails due to network/auth or the run already finished and the server rejects the cancel.","commonSituations":"User interrupts a long-running subagent while the server is down; url-less ASGI subagent cancelled via the sync path; stale task records pointing at completed/deleted runs.","solutions":["Ensure the subagent has a reachable 'url' before cancelling via sync tools","Inspect the embedded exception message for the actual cause (auth, connectivity, already-finished run)","Use the async cancel path for url-less/ASGI-transport subagents"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if subagent_spec.get(\"url\") is None:\n    # ASGI subagents must be cancelled via the async path\n    use_async_cancel = True","typeGuard":null,"tryCatchPattern":"try:\n    client = clients.get_sync(tracked[\"agent_name\"])\n    client.runs.cancel(thread_id=tracked[\"thread_id\"], run_id=tracked[\"run_id\"])\nexcept Exception as e:\n    logger.warning(\"cancel failed: %s\", e)\n    # mark task for retry or fall back to async cancel","preventionTips":["Check task status before cancelling (run may already be finished)","Prefer async cancel for url-less subagents","Add retry/backoff around cancel during server outages"],"tags":["python","langgraph","network","error-handling"],"backgroundTag":"run-cancel-failed","analyzedSha":"a1af029e6e73cb17c36bff823d227747b28e91e1","analyzedAt":"2026-08-29T11:43:24.718Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}