feat(transport): expose pid and process as public properties#819
Open
MukundaKatta wants to merge 1 commit intoanthropics:mainfrom
Open
feat(transport): expose pid and process as public properties#819MukundaKatta wants to merge 1 commit intoanthropics:mainfrom
MukundaKatta wants to merge 1 commit intoanthropics:mainfrom
Conversation
Addresses anthropics/anthropic-sdk-python#1370 (filed on the wrong repo — code lives here). SubprocessCLITransport spawns a subprocess but exposes no public way to inspect the process or its PID. Callers needing external cleanup (killpg, psutil, cgroups) have been walking async-generator frame internals to reach the underlying Process handle — a workaround that breaks on any internal refactor. Adds two read-only properties: - transport.pid -> OS process ID, or None before connect()/after close() - transport.process -> anyio.abc.Process handle, or None Both return None when no subprocess is running, so callers can safely check state without try/except. Note on other asks in #1370: - Bounded wait + SIGKILL fallback in close() is already implemented (see lines 481-497 of subprocess_cli.py). - start_new_session=True would change cleanup semantics (Windows has no POSIX sessions) and is out of scope for this minimal change. Tests (4 new): pid/process None before connect, pid/process return expected values when _process is set. Signed-off-by: Mukunda Katta <mukunda.vjcs6@gmail.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.
Why
anthropics/anthropic-sdk-python#1370 reports that callers using the SDK for cancellation cleanup have to walk async-generator frame internals to reach the underlying subprocess:
```python
def _extract_sdk_pid(gen):
frame = getattr(gen, "ag_frame", None)
for val in frame.f_locals.values():
inner_frame = getattr(val, "ag_frame", None)
transport = inner_frame.f_locals.get("chosen_transport")
proc = getattr(transport, "_process", None)
return proc.pid
```
That's fragile — any rename of private attributes breaks every user of this pattern. Note the issue was filed on `anthropic-sdk-python` but `SubprocessCLITransport` lives here.
What
Two read-only properties on `SubprocessCLITransport`:
Both return None when `self._process is None`, so callers can safely check state without try/except or inspecting private attrs.
What I did not change
The issue also asked for a bounded wait + SIGKILL fallback in `close()`. That's already implemented (see `subprocess_cli.py:481-497` — graceful wait 5s → SIGTERM → wait 5s → SIGKILL). Calling this out so the linked issue can be closed even though not all of its asks translate into code changes.
The issue's third ask (`start_new_session=True`) has Windows portability implications and is out of scope for this minimal change.
Tests
4 new unit tests in `tests/test_transport.py` covering:
All 63 existing tests continue to pass.
Related
Closes anthropics/anthropic-sdk-python#1370 (once the issue is migrated here).