Skip to content

Commit 14c6a40

Browse files
committed
fix(python): add opt-in sandbox delete timeout
Signed-off-by: Eric Curtin <[email protected]>
1 parent 55bf786 commit 14c6a40

3 files changed

Lines changed: 60 additions & 5 deletions

File tree

‎e2e/python/conftest.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ def _create(*, spec: object | None = None, delete_on_exit: bool = True) -> Sandb
9595
delete_on_exit=delete_on_exit,
9696
# Allow time to pull an explicitly supplied workload fixture.
9797
ready_timeout_seconds=300.0,
98+
# Kubernetes holds DeleteSandbox open until the pod terminates
99+
# (30s grace plus API headroom), which can exceed the 30s default.
100+
delete_timeout_seconds=120.0,
98101
)
99102

100103
return _create

‎python/openshell/sandbox.py‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -626,9 +626,14 @@ def exec_python(
626626
timeout_seconds=timeout_seconds,
627627
)
628628

629-
def delete(self, *, allow_missing: bool = False) -> DeletionResult:
629+
def delete(
630+
self, *, allow_missing: bool = False, timeout: float | None = None
631+
) -> DeletionResult:
630632
return self._client.delete(
631-
self.sandbox.name, workspace=self._workspace, allow_missing=allow_missing
633+
self.sandbox.name,
634+
workspace=self._workspace,
635+
allow_missing=allow_missing,
636+
timeout=timeout,
632637
)
633638

634639
def stop(self) -> SandboxRef:
@@ -1072,15 +1077,21 @@ def list_ids_for_all_workspaces(
10721077
]
10731078

10741079
def delete(
1075-
self, name: str, *, workspace: str, allow_missing: bool = False
1080+
self,
1081+
name: str,
1082+
*,
1083+
workspace: str,
1084+
allow_missing: bool = False,
1085+
timeout: float | None = None,
10761086
) -> DeletionResult:
1087+
"""Delete a sandbox, waiting up to `timeout` (default: client timeout)."""
10771088
response = self._stub.DeleteSandbox(
10781089
openshell_pb2.DeleteSandboxRequest(
10791090
workspace_scope=_workspace_scope(workspace),
10801091
name=name,
10811092
allow_missing=allow_missing,
10821093
),
1083-
timeout=self._timeout,
1094+
timeout=self._timeout if timeout is None else timeout,
10841095
)
10851096
return DeletionResult(
10861097
DeletionOutcome(response.outcome), response.sandbox_id or None
@@ -1586,13 +1597,18 @@ def __init__(
15861597
workload_template: str | None = None,
15871598
timeout: float = 30.0,
15881599
ready_timeout_seconds: float = 120.0,
1600+
delete_timeout_seconds: float | None = None,
15891601
auto_refresh: bool = True,
15901602
write_back: bool = True,
15911603
insecure: bool = False,
15921604
client_credentials: ClientCredentialsAuth | None = None,
15931605
) -> None:
15941606
"""Bind a Sandbox context to the active gateway.
15951607
1608+
`delete_timeout_seconds` bounds the delete RPC on exit and defaults to
1609+
`timeout`. Raise it for drivers that hold the delete open while the
1610+
workload terminates.
1611+
15961612
OIDC kwargs (`auto_refresh`, `write_back`, `insecure`, and
15971613
`client_credentials`) forward
15981614
directly to `SandboxClient.from_active_cluster` and have the
@@ -1612,6 +1628,7 @@ def __init__(
16121628
self._workload_template = workload_template
16131629
self._timeout = timeout
16141630
self._ready_timeout_seconds = ready_timeout_seconds
1631+
self._delete_timeout_seconds = delete_timeout_seconds
16151632
self._auto_refresh = auto_refresh
16161633
self._write_back = write_back
16171634
self._insecure = insecure
@@ -1707,7 +1724,9 @@ def __exit__(self, *args: object) -> None:
17071724
and self._session is not None
17081725
and self._client is not None
17091726
):
1710-
result = self._session.delete(allow_missing=True)
1727+
result = self._session.delete(
1728+
allow_missing=True, timeout=self._delete_timeout_seconds
1729+
)
17111730
if result.outcome == DeletionOutcome.ACCEPTED:
17121731
self._client.wait_deleted(
17131732
self._session.sandbox.name,

‎python/openshell/sandbox_cleanup_test.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
"""Cleanup status handling through the curated client's real gRPC interceptor."""
55

6+
import time
67
from concurrent import futures
78
from contextlib import nullcontext
89
from types import SimpleNamespace
@@ -20,6 +21,7 @@ def cleanup_client():
2021
state = SimpleNamespace(
2122
exists=False,
2223
code=grpc.StatusCode.NOT_FOUND,
24+
delete_delay=0.0,
2325
calls=[],
2426
closed=[],
2527
)
@@ -46,6 +48,7 @@ def delete(request, context):
4648
assert request.name == "cleanup-test"
4749
assert request.workspace_scope.workspace == "default"
4850
assert request.allow_missing
51+
time.sleep(state.delete_delay)
4952
if state.code == grpc.StatusCode.NOT_FOUND:
5053
return openshell_pb2.DeleteSandboxResponse(
5154
outcome=openshell_pb2.DELETION_OUTCOME_ALREADY_ABSENT
@@ -158,6 +161,36 @@ def test_context_cleanup_handles_absence_and_intercepted_errors(
158161
assert managed._session is None
159162

160163

164+
@pytest.mark.parametrize(
165+
("delete_timeout_seconds", "expect_deadline"), [(None, True), (5, False)]
166+
)
167+
def test_context_cleanup_delete_timeout_is_opt_in(
168+
cleanup_client, monkeypatch, delete_timeout_seconds, expect_deadline
169+
):
170+
client, state = cleanup_client
171+
state.exists = True
172+
state.delete_delay = 0.5
173+
client._timeout = 0.2
174+
monkeypatch.setattr(
175+
SandboxClient,
176+
"from_active_cluster",
177+
classmethod(lambda _cls, **_kwargs: client),
178+
)
179+
180+
managed = Sandbox(
181+
workspace="default",
182+
sandbox="cleanup-test",
183+
delete_timeout_seconds=delete_timeout_seconds,
184+
)
185+
expected = pytest.raises(GatewayError) if expect_deadline else nullcontext()
186+
with expected as caught, managed:
187+
state.exists = False
188+
if caught is not None:
189+
assert caught.value.code() == grpc.StatusCode.DEADLINE_EXCEEDED
190+
assert state.closed == [True]
191+
assert managed._client is None
192+
193+
161194
class _EntryFailureClient:
162195
"""Fails the named client call and records close()."""
163196

0 commit comments

Comments
 (0)