adk-js icon indicating copy to clipboard operation
adk-js copied to clipboard

Abort during a tool call: the tool's completed result is discarded and the session records the call as "Invocation was aborted by client."

Open zennicalles opened this issue 2 weeks ago • 1 comments

Versions: @google/adk 2.4.0 (same on main).

When abortSignal fires while a tool is running, the tool's promise is not cancelled. It completes and returns a result, which ADK discards. Runner.synthesizeAbortEvents then answers that call with { error: 'Invocation was aborted by client.' } and appends it to the session. On the next turn the model is told the call failed, although it succeeded. For tools with side effects (payments, emails), a graceful cancellation (client disconnect, a SIGTERM handler aborting the run) then invites the model to do it again.

Repro:

const ac = new AbortController()
const refund = new FunctionTool({ name: 'refund', description: 'x', parameters: z.object({ amount: z.number() }),
  execute: async ({ amount }) => {
    ac.abort()                                  // cancel lands while the call is in flight
    await new Promise((r) => setTimeout(r, 10)) // the API call completes anyway
    return { status: 'succeeded', refundId: `re_${amount}` }
  } })
// mock model: calls refund({amount: 50}) once, then answers with text
for await (const ev of runner.runAsync({ userId: 'u', sessionId, newMessage, abortSignal: ac.signal })) {}
// read session.events' functionResponses

Observed: the tool returned {status:'succeeded', refundId:'re_50'}. The session holds [{"error":"Invocation was aborted by client."}] for c1 (event errorCode: INVOCATION_ABORTED), and the next turn's request to the model carries that error for c1.

Expected: a call whose tool has already returned is recorded with its real result. Only calls that really have no result are sealed as aborted. At minimum, the seal should not claim the call failed when ADK cannot know that.

Cause:

  • LlmAgent.postprocess returns before yielding the function-response event it already has: if (!functionResponseEvent || invocationContext.abortSignal?.aborted) return; (core/src/agents/llm_agent.ts:1862).
  • Runner.runAsync also breaks before processAndAppendEvent once the signal is aborted (core/src/runner/runner.ts:486-505).
  • buildAbortEvents (core/src/events/abort_events.ts) then treats the call as dangling.
  • In adk-python the cancellation is delivered into the awaiting coroutine. In adk-js the tool keeps running, so the framework does have the result.

Suggested fix: when the tool task has produced a functionResponseEvent, yield it even if the signal has fired, and have the Runner persist it before sealing. buildAbortEvents then only answers calls that really have no response. Separately, consider wording ABORT_MESSAGE so it doesn't claim failure, e.g. "The invocation was aborted while this call was in progress; it may have completed." That message would also be accurate for a hard cancel where the outcome really is unknown.

Found while testing ADK with Shakedown (fault-injection tests for agents).

zennicalles avatar Nov 14 '26 01:11 zennicalles

Hi @zennicalles, Thank you for raising the issue. We have reproduced the issue and raised a PR to fix the issue. The PR will be reviewed. Thank you for your patience and understanding.

Varun-S10 avatar Nov 16 '26 08:11 Varun-S10