Skip to content

fix(py-sdk): robust target parsing + empty-response handling in Sandbox._send (closes #256)#258

Merged
WaylandYang merged 1 commit into
mainfrom
fix/sdk-send-ipv6-empty-256
Jun 23, 2026
Merged

fix(py-sdk): robust target parsing + empty-response handling in Sandbox._send (closes #256)#258
WaylandYang merged 1 commit into
mainfrom
fix/sdk-send-ipv6-empty-256

Conversation

@WaylandYang

Copy link
Copy Markdown
Contributor

Closes #256. Thanks @Bug-Keeper — accurate report with exact code citations.

Two real defects in sdk/python/forkd/sandbox.py _send

  1. IPv6 targets mangled. host, _, port_s = self.target.rpartition(":") turns [::1]:8888 into host "[::1]" (brackets retained), which socket.create_connection can't use. Latent today (default 10.42.0.2:8888) but target / FORKD_TARGET is user-configurable. Fixed via a _split_host_port helper on urllib.parse.urlsplit (strips brackets; raises a clear ValueError on a malformed target).
  2. Empty response → opaque JSONDecodeError. json.loads(buf.decode()) on an empty buf (agent closed without replying) gave a baffling parse error. Now a plain RuntimeError("empty response …"), matching the TS SDK's empty-body handling in controller.ts. A recv timeout is also surfaced as a clear TimeoutError.

The issue title ("loses data with custom timeout") slightly mischaracterizes — the actual bugs are the two above (no data loss). The report's substance + proposed fixes were correct.

Tests + CI

sdk/python had no tests (only example.py). Adds sdk/python/tests/test_send.py — 12 cases over IPv4/IPv6/hostname/malformed parsing + empty-response/roundtrip wire behavior against an in-process socket server (no firecracker/daemon needed). New sdk-python CI job installs the package + runs them so they don't rot.

Test plan

  • pytest sdk/python/tests/ → 12 passed locally
  • CI green (new sdk-python job)

🤖 Generated with Claude Code

…ox._send

Closes #256 (thanks @Bug-Keeper — accurate report, exact code citations).

Two real defects in sdk/python/forkd/sandbox.py `_send`:

1. `host, _, port_s = self.target.rpartition(":")` mangled IPv6 targets:
   `[::1]:8888` → host `"[::1]"` (brackets kept), which
   `socket.create_connection` can't resolve. Latent today (default is
   `10.42.0.2:8888`) but `target` / `FORKD_TARGET` is user-configurable.
   Fixed with a `_split_host_port` helper using `urllib.parse.urlsplit`,
   which strips the brackets and raises a clear ValueError on a
   malformed target instead of a bare `int()` failure.

2. `return json.loads(buf.decode())` on an empty `buf` raised an opaque
   JSONDecodeError when the agent closed the connection without replying
   (crash / mid-response death). Now raises a plain "empty response"
   RuntimeError — matching the TS SDK's empty-body handling in
   controller.ts. Also turns a recv timeout into a clear TimeoutError
   rather than a bare socket.timeout.

(The issue title says "loses data with custom timeout" — the actual
defects are the two above; no data loss, but the report's substance and
proposed fixes were correct.)

Adds sdk/python/tests/ (the SDK had no tests — only example.py) with
12 regression cases covering IPv4/IPv6/hostname/malformed parsing and
the empty-response + roundtrip wire behavior against an in-process
socket server (no firecracker/daemon needed). New `sdk-python` CI job
installs the package and runs them so they can't rot.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@WaylandYang WaylandYang merged commit 3e8fdc2 into main Jun 23, 2026
3 checks passed
@WaylandYang WaylandYang deleted the fix/sdk-send-ipv6-empty-256 branch June 23, 2026 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Sandbox SDK's _send loses data with custom timeout settings

1 participant