langchain-ai/deepagents · error

Failed to cancel run: {e}

Error message

Failed to cancel run: {e}

What it means

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.

Source

Thrown at libs/deepagents/deepagents/middleware/async_subagents.py:596


def _build_cancel_tool(
    clients: _ClientCache,
) -> StructuredTool:
    """Build the `cancel_async_task` tool."""

    def cancel_async_task(
        task_id: str,
        runtime: ToolRuntime,
    ) -> str | Command:
        tracked = _resolve_tracked_task(task_id, runtime)
        if isinstance(tracked, str):
            return tracked

        try:
            client = clients.get_sync(tracked["agent_name"])
            client.runs.cancel(thread_id=tracked["thread_id"], run_id=tracked["run_id"])
        except Exception as e:  # noqa: BLE001  # get_sync() may raise ValueError; SDK raises untyped errors
            return f"Failed to cancel run: {e}"
        now = datetime.now(UTC).strftime("%Y-%m-%dT%H:%M:%SZ")
        updated = AsyncTask(
            task_id=tracked["task_id"],
            agent_name=tracked["agent_name"],
            thread_id=tracked["thread_id"],
            run_id=tracked["run_id"],
            status="cancelled",
            created_at=tracked["created_at"],
            last_checked_at=now,
            last_updated_at=now,
        )
        msg = f"Cancelled async subagent task: {tracked['task_id']}"
        return Command(
            update={
                "messages": [ToolMessage(msg, tool_call_id=runtime.tool_call_id)],
                "async_tasks": {tracked["task_id"]: updated},
            }

View on GitHub (pinned to a1af029e6e)

Solutions

  1. Ensure the subagent has a reachable 'url' before cancelling via sync tools
  2. Inspect the embedded exception message for the actual cause (auth, connectivity, already-finished run)
  3. Use the async cancel path for url-less/ASGI-transport subagents
Defensive patterns

Strategy: try-catch

Validate before calling

if subagent_spec.get("url") is None:
    # ASGI subagents must be cancelled via the async path
    use_async_cancel = True

Try / catch

try:
    client = clients.get_sync(tracked["agent_name"])
    client.runs.cancel(thread_id=tracked["thread_id"], run_id=tracked["run_id"])
except Exception as e:
    logger.warning("cancel failed: %s", e)
    # mark task for retry or fall back to async cancel

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of langchain-ai/deepagents@a1af029e6e (2026-08-29). Data as JSON: /api/errors/8e35dc58847125c2. Report an issue: GitHub.