Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/claude_agent_sdk/_internal/transport/subprocess_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,14 +450,20 @@ async def close(self) -> None:
await self._stderr_stream.aclose()
self._stderr_stream = None

# Terminate and wait for process
# Wait for the CLI process to exit gracefully after stdin EOF.
# The CLI detects stdin closure and shuts down on its own, flushing
# its async write queue (session transcript) to disk before exiting.
# Without this wait, the transcript may not be persisted, causing
# subsequent --resume to silently create a new session instead.
if self._process.returncode is None:
with suppress(ProcessLookupError):
self._process.terminate()
# Wait for process to finish with timeout
with suppress(Exception):
# Just try to wait, but don't block if it fails
try:
with anyio.fail_after(5):
await self._process.wait()
except TimeoutError:
with suppress(ProcessLookupError):
self._process.terminate()
with suppress(Exception):
await self._process.wait()

self._process = None
self._stdout_stream = None
Expand Down