From 6c7b86b19a37f8fd6b06b94360b3caaca06c122c Mon Sep 17 00:00:00 2001 From: SAY-5 Date: Tue, 14 Apr 2026 22:36:33 -0700 Subject: [PATCH] feat(transport): expose subprocess PID for external cleanup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Per anthropics/anthropic-sdk-python#1370, callers that need to forcibly clean up the spawned `claude` CLI process (timeouts, hard shutdowns, parent-process cleanup) currently have to walk async generator internals to find the process, which is fragile and depends on SDK private state. Add a public `pid` property on `SubprocessCLITransport` that returns the underlying process PID, or `None` if no process is currently running. The existing `close()` already implements the graceful-shutdown → SIGTERM → SIGKILL escalation requested in the issue, so this PR only adds the PID accessor. Closes anthropics/anthropic-sdk-python#1370 --- .../_internal/transport/subprocess_cli.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/claude_agent_sdk/_internal/transport/subprocess_cli.py b/src/claude_agent_sdk/_internal/transport/subprocess_cli.py index 4b47f115..51e58bb1 100644 --- a/src/claude_agent_sdk/_internal/transport/subprocess_cli.py +++ b/src/claude_agent_sdk/_internal/transport/subprocess_cli.py @@ -448,6 +448,18 @@ async def _handle_stderr(self) -> None: except Exception: pass # Ignore other errors during stderr reading + @property + def pid(self) -> int | None: + """Return the PID of the underlying CLI subprocess, or ``None`` if no + process is currently running. + + This is exposed as a public property so callers that need to clean up + the spawned ``claude`` CLI process out-of-band (e.g. on hard timeouts + or shutdown signals) can do so without walking SDK internals. + See anthropics/anthropic-sdk-python#1370. + """ + return self._process.pid if self._process is not None else None + async def close(self) -> None: """Close the transport and clean up resources.""" if not self._process: