[BUG] AbortController not respected
Status Fixed / completed
Maintainer reply ✓ Yes — ThariqS
Activity 4 comments · opened Jul 4, 2025 · closed Aug 22, 2025
💡 Likely answer: A maintainer (ThariqS, collaborator)
responded on this thread — see the highlighted reply below.
Environment
- Platform (select one):
- [x] Anthropic API
- [ ] AWS Bedrock
- [ ] Google Vertex AI
- [ ] Other: <!-- specify -->
- Claude CLI version: 1.0.43 (using SDK directly)
- Operating System: macOS 14.x
- Terminal: N/A (using Node.js SDK)
Bug Description
The SDK does not respect AbortController signals during query execution. When controller.abort() is called, the SDK continues processing and executing all queued tool calls to completion instead of stopping immediately.
Steps to Reproduce
- Save the following as
repro.mjs:
import { query } from "@anthropic-ai/claude-code/sdk.mjs";
// Minimal repro of AbortController not working
async function demonstrateBug() {
const controller = new AbortController();
let messageCount = 0;
let abortedAt = null;
console.log("Starting query that will use multiple tools...");
try {
for await (const message of query({
prompt: "Create a todo list with 3 items, then read package.json, then list files in current directory",
abortController: controller,
options: {
model: "claude-3-5-sonnet-20241022",
maxTurns: 10,
permissionMode: "bypassPermissions",
},
})) {
messageCount++;
console.log(`Message ${messageCount}: ${message.type}`);
// Abort after receiving the first assistant message
if (message.type === "assistant" && !abortedAt) {
abortedAt = messageCount;
console.log(`\n>>> ABORTING at message ${messageCount} <<<`);
console.log(`>>> controller.signal.aborted before:
${controller.signal.aborted}`);
controller.abort();
console.log(`>>> controller.signal.aborted after:
${controller.signal.aborted}`);
console.log(`>>> Expecting query to stop soon...\n`);
}
}
console.log(`\nQuery completed. Total messages: ${messageCount}`);
console.log(`Aborted at message: ${abortedAt}`);
console.log(`Messages after abort: ${messageCount - abortedAt}`);
} catch (error) {
console.log("\nQuery threw error:", error.message);
console.log("Error name:", error.name);
}
}
// Run the demo
demonstrateBug().catch(console.error);
- Run with:
ANTHROPIC_API_KEY=your_key node repro.mjs
Expected Behavior
When controller.abort() is called, the query should stop immediately and throw an AbortError:
Starting query that will use multiple tools...
Message 1: system
Message 2: assistant
>>> ABORTING at message 2 <<<
>>> controller.signal.aborted before: false
>>> controller.signal.aborted after: true
>>> Expecting query to stop soon...
Query threw error: The operation was aborted
Error name: AbortError
Actual Behavior
The SDK continues executing all tools and completes the full query despite the abort signal:
Starting query that will use multiple tools...
Message 1: system
Message 2: assistant
>>> ABORTING at message 2 <<<
>>> controller.signal.aborted before:
false
>>> controller.signal.aborted after:
true
>>> Expecting query to stop soon...
Message 3: assistant
Message 4: assistant
Message 5: assistant
Message 6: user
Message 7: user
Message 8: user
Message 9: user
Message 10: assistant
Message 11: result
Query completed. Total messages: 11
Aborted at message: 2
Messages after abort: 9
Additional Context
Tested with:
@anthropic-ai/claude-code: 1.0.43Node.js: v18.19.0 and v20.x
4 Comments
Okay, I think this is only an issue with passing abortController as a property like
query({ abortController }). Turns out you can also pass it likequery({ options: { abortController }}), which does work.Closing this issue as we've updated the docs to clarify this.
Thanks Thariq!
This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.