fix(sdk): keep stdin open until background tasks finish - #1532
Open
JamesScrase-PortSwigger wants to merge 1 commit into
Open
fix(sdk): keep stdin open until background tasks finish#1532JamesScrase-PortSwigger wants to merge 1 commit into
JamesScrase-PortSwigger wants to merge 1 commit into
Conversation
In headless runs the CLI terminates a background Bash task shortly after the final result once stdin has closed. runClaudeWithSdk passes a string / one-shot prompt, so stdin closes immediately and a background command launched during a turn is killed before it finishes — while the step still reports success and its output is never consumed. Feed the prompt as a stream and keep stdin open until a result reports no live background tasks (tracked via background_tasks_changed), so background work runs to completion and the model receives its follow-up turn(s). Because stdin stays open until then, background work started in a later turn is covered too, not just the first. The no-background path is unchanged (break on the first result with an empty live set), preserving the anti-hang guard from anthropics#1339; keeping the loop open while tasks are live also covers the background-subagent case (anthropics#1499). A run whose background work never settles is bounded by the job's timeout-minutes. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When the model launches a background Bash task (
run_in_background: true, or a timeout auto-background) and ends its turn, the background shell is terminated shortly after the finalresult— the termination fires once stdin has closed, andrunClaudeWithSdkpasses a string prompt, so stdin closes immediately. A long background command (build, test suite, …) never finishes and its output is never consumed, yet the step still reportssubtype: "success". See #1462 for the user-facing "green run, nothing happened" symptom.Change
base-action/src/run-claude-sdk.ts: feed the prompt through an async generator that yields it and then holds the CLI's stdin open, and break the loop only when aresultreports no live background tasks (tracked via thebackground_tasks_changedsystem message). Afinallyreleases the hold if the stream ends or throws.Because stdin stays open until nothing is live, a completed or killed background task keeps the process alive long enough to run to completion / be observed, and the model receives its follow-up turn(s) to consume the output. Runs with no background tasks are unaffected — they break on the first
result, preserving the anti-hang guard from #1339.Possible alternatives
This behavior originates in Claude Code / the Agents SDK, not in
claude-code-action; this PR is a consumer-side mitigation. You may prefer a holistic upstream fix — e.g. not terminating background shells while an SDK consumer is still iterating the stream. Happy to adapt or close this in favor of that, or it may be preferred to make this a config option in case other users still desire long-running tasks to be killed or have a timeout.See also
#1500 fixes this just for background subagents, but doesn't work for backgrounded Bash commands.
Fixes #1531, #1499.
Environment
@anthropic-ai/claude-agent-sdk^0.3.211-p(GitHub Actions)