fix: allow CLI to flush session transcript before terminating subprocess#614
Open
garythung wants to merge 1 commit intoanthropics:mainfrom
Open
fix: allow CLI to flush session transcript before terminating subprocess#614garythung wants to merge 1 commit intoanthropics:mainfrom
garythung wants to merge 1 commit intoanthropics:mainfrom
Conversation
SubprocessCLITransport.close() was immediately sending SIGTERM after closing stdin, not giving the CLI subprocess time to flush the session transcript to disk. This caused session resume to fail with "No conversation found" because the .jsonl file was nearly empty. Now waits up to 10 seconds for the process to exit on its own after stdin EOF before falling back to SIGTERM. 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
SubprocessCLITransport.close()sendsSIGTERMimmediately after closingstdin, killing the CLI subprocess before it can flush the session transcript to disk. This breaks session resume (--resume <session_id>) because the.jsonlfile only contains a single dequeue operation instead of the full conversation history.close()callsself._process.terminate()unconditionally (when the process is still running). In--input-format stream-jsonmode, the CLI keeps running after sendingResultMessage, waiting for more input onstdin. So by the timeclose()fires, the process is always still alive, andterminate()always fires. There's no timing luck involved.Problem
When
ClaudeSDKClientexits, the transport'sclose()method:stdinself._process.terminate()(SIGTERM)The CLI subprocess detects
stdinEOF and begins its shutdown sequence — writing user messages, assistant messages, and tool use entries to~/.claude/projects/<encoded-cwd>/<session-id>.jsonl— butSIGTERMarrives before the write completes.The resulting session file contains only:
Any subsequent
--resumecall finds no conversation data and exits with code1.A secondary effect is that
stderroutput is also lost: thestderrreader task gets cancelled before it can collect output, so all CLI errors surface as"Check stderr output for details"with no actual details.Solution
Wait for the CLI to exit gracefully after
stdinEOF before falling back toSIGTERM. A 10-second timeout prevents hanging if the process doesn't exit on its own.Before
After
Test Plan
resume=<session_id>from the first query’s result — the second query should succeed and the session.jsonlfile should contain the full conversation transcript