fix: wait for graceful CLI exit on close to persist session transcript#561
Open
n0isy wants to merge 1 commit intoanthropics:mainfrom
Open
fix: wait for graceful CLI exit on close to persist session transcript#561n0isy wants to merge 1 commit intoanthropics:mainfrom
n0isy wants to merge 1 commit intoanthropics:mainfrom
Conversation
The `close()` method previously sent SIGTERM to the CLI process immediately after closing stdin. The CLI uses an async write queue (`enqueueWrite` + `setTimeout`) to batch-write session transcript entries to disk. Terminating the process before this queue drains causes the session JSONL to be incomplete (only a `queue-operation` entry), which makes `--resume` silently create a new session instead of resuming the existing one. After closing stdin, the CLI detects EOF and shuts down on its own, flushing the write queue before exiting (typically under 1 second). This change waits up to 5 seconds for the process to exit gracefully after stdin EOF, falling back to SIGTERM only on timeout. Fixes anthropics#555 Co-Authored-By: Claude Opus 4.6 <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.
Summary
close()now waits up to 5 seconds for the CLI process to exit gracefully after closing stdin, instead of immediately sending SIGTERMRoot Cause
The CLI uses
enqueueWrite()+setTimeout()to batch-write session transcript entries to the JSONL file. Whenclose()sends SIGTERM immediately after closing stdin, the write queue hasn't drained yet. The session file ends up containing only aqueue-operationentry — no user/assistant messages. When--resumeis later used with that session ID, the CLI can't find the conversation history and silently creates a new session instead.How the Fix Works
The CLI already detects stdin EOF and initiates a clean shutdown, flushing its write queue before exiting (typically under 1 second). This change simply gives it time to do so:
Verification
Tested with a script that:
ClaudeSDKClient, sends a query, disconnectsresume=session_id, sends a follow-up queryBefore fix: Resume fails silently — new session ID returned,
error_during_execution, 0 turns.After fix: Resume works — same session ID, conversation context preserved.
Also verified via direct subprocess test that the CLI exits naturally in ~0.5s after stdin EOF with the transcript fully flushed to disk.
Fixes #555
🤖 Generated with Claude Code