fix(python-sdk): stop get_sandbox from closing or leaking shared handles

Merged
LU
1mo ago
+141-49

What this PR does / why we need it:

get_sandbox() returns the handle that create_sandbox() and earlier lookups share, but closes it on any failed lookup, such as a throttled watch or WarmPoolNotFound. Every other holder then gets AttributeError while the claim and Sandbox are alive. Two concurrent async calls for an untracked claim also each build a handle, and the one that loses stays open and untracked. A missing claim waits out resolve_timeout.

This matches the TypeScript SDK: read the claim first, and after a failure close the handle only when its claim or Sandbox is gone. The async client returns a handle another call already tracked for the same Sandbox and closes the redundant one. Tracking and SandboxNotFoundError are unchanged. The regression tests fail without the fix.

E2E validation:

Before/after on kind: after a claim's warm pool is deleted so get_sandbox() fails with WarmPoolNotFound, the held handle's commands.run("echo ok") raised AttributeError before and returns ok now, sync and async.

Which issue(s) this PR is related to:

N/A

Release Note

Python SDK: Fixed `get_sandbox()` closing shared handles when a lookup fails while the SandboxClaim still exists, and leaking a handle when async calls race. A missing SandboxClaim now fails immediately.

Summary by CodeRabbit

  • Bug Fixes
    • Sandbox lookups now check the latest claim information before waiting for updates.
    • Temporary lookup or API errors no longer close an active sandbox connection.
    • Connections are refreshed when a claim points to a different sandbox; connections are closed when the claim or sandbox is confirmed missing.
    • Concurrent lookups for the same sandbox reuse one active connection, avoiding redundant connections.
    • Looking up a nonexistent claim now fails without waiting for a watch event.