Skip to content

Commit 0516162

Browse files
ericcurtinTaylorMutch
authored andcommitted
fix(python): add opt-in sandbox delete timeout (#4697)
* fix(python): add opt-in sandbox delete timeout Signed-off-by: Eric Curtin <eric.curtin@docker.com> * fix(python): omit delete timeout unless set Signed-off-by: Eric Curtin <eric.curtin@docker.com> * fix(python): fix delete test typing Signed-off-by: Eric Curtin <eric.curtin@docker.com> * test(python): tighten delete timeout coverage Signed-off-by: Eric Curtin <eric.curtin@docker.com> * fix(python): rename delete timeout to timeout_seconds Signed-off-by: Eric Curtin <eric.curtin@docker.com> --------- Signed-off-by: Eric Curtin <eric.curtin@docker.com> Signed-off-by: Taylor Mutch <taylormutch@gmail.com> Co-authored-by: Taylor Mutch <taylormutch@gmail.com>
1 parent 40e0569 commit 0516162

4 files changed

Lines changed: 100 additions & 6 deletions

File tree

‎src/openshell/sandbox.py‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -680,9 +680,16 @@ def exec_python(
680680
timeout_seconds=timeout_seconds,
681681
)
682682

683-
def delete(self, *, allow_missing: bool = False) -> DeletionResult:
683+
def delete(
684+
self, *, allow_missing: bool = False, timeout_seconds: float | None = None
685+
) -> DeletionResult:
686+
# Forward `timeout_seconds` only on opt-in so older clients still work.
687+
extra = {} if timeout_seconds is None else {"timeout_seconds": timeout_seconds}
684688
return self._client.delete(
685-
self.sandbox.name, workspace=self._workspace, allow_missing=allow_missing
689+
self.sandbox.name,
690+
workspace=self._workspace,
691+
allow_missing=allow_missing,
692+
**extra,
686693
)
687694

688695
def stop(self) -> SandboxRef:
@@ -1138,15 +1145,25 @@ def list_ids_for_all_workspaces(
11381145
]
11391146

11401147
def delete(
1141-
self, name: str, *, workspace: str, allow_missing: bool = False
1148+
self,
1149+
name: str,
1150+
*,
1151+
workspace: str,
1152+
allow_missing: bool = False,
1153+
timeout_seconds: float | None = None,
11421154
) -> DeletionResult:
1155+
"""Delete a sandbox.
1156+
1157+
`timeout_seconds` is the gRPC deadline for this call (default: the
1158+
client timeout).
1159+
"""
11431160
response = self._stub.DeleteSandbox(
11441161
openshell_pb2.DeleteSandboxRequest(
11451162
workspace_scope=_workspace_scope(workspace),
11461163
name=name,
11471164
allow_missing=allow_missing,
11481165
),
1149-
timeout=self._timeout,
1166+
timeout=self._timeout if timeout_seconds is None else timeout_seconds,
11501167
)
11511168
return DeletionResult(
11521169
DeletionOutcome(response.outcome), response.sandbox_id or None
@@ -1751,13 +1768,18 @@ def __init__(
17511768
workload_template: str | None = None,
17521769
timeout: float = 30.0,
17531770
ready_timeout_seconds: float = 120.0,
1771+
delete_timeout_seconds: float | None = None,
17541772
auto_refresh: bool = True,
17551773
write_back: bool = True,
17561774
insecure: bool = False,
17571775
client_credentials: ClientCredentialsAuth | None = None,
17581776
) -> None:
17591777
"""Bind a Sandbox context to the active gateway.
17601778
1779+
`delete_timeout_seconds` bounds the delete RPC on exit and defaults to
1780+
`timeout`. Raise it for drivers that hold the delete open while the
1781+
workload terminates.
1782+
17611783
OIDC kwargs (`auto_refresh`, `write_back`, `insecure`, and
17621784
`client_credentials`) forward
17631785
directly to `SandboxClient.from_active_cluster` and have the
@@ -1777,6 +1799,7 @@ def __init__(
17771799
self._workload_template = workload_template
17781800
self._timeout = timeout
17791801
self._ready_timeout_seconds = ready_timeout_seconds
1802+
self._delete_timeout_seconds = delete_timeout_seconds
17801803
self._auto_refresh = auto_refresh
17811804
self._write_back = write_back
17821805
self._insecure = insecure
@@ -1872,7 +1895,9 @@ def __exit__(self, *args: object) -> None:
18721895
and self._session is not None
18731896
and self._client is not None
18741897
):
1875-
result = self._session.delete(allow_missing=True)
1898+
result = self._session.delete(
1899+
allow_missing=True, timeout_seconds=self._delete_timeout_seconds
1900+
)
18761901
if result.outcome == DeletionOutcome.ACCEPTED:
18771902
self._client.wait_deleted(
18781903
self._session.sandbox.name,

‎tests/e2e/conftest.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ def cluster_name() -> str:
3535

3636
@pytest.fixture(scope="session")
3737
def sandbox_client(cluster_name: str) -> Iterator[SandboxClient]:
38-
with SandboxClient.from_active_cluster(cluster=cluster_name) as client:
38+
# Direct deletes can wait out the Kubernetes pod grace period.
39+
with SandboxClient.from_active_cluster(
40+
cluster=cluster_name, timeout=120.0
41+
) as client:
3942
yield client
4043

4144

@@ -76,6 +79,9 @@ def _create(
7679
delete_on_exit=delete_on_exit,
7780
# Allow time to pull an explicitly supplied workload fixture.
7881
ready_timeout_seconds=300.0,
82+
# Kubernetes holds DeleteSandbox open until the pod terminates
83+
# (30s grace plus API headroom), which can exceed the 30s default.
84+
delete_timeout_seconds=120.0,
7985
)
8086

8187
return _create

‎tests/sandbox_cleanup_test.py‎

Lines changed: 41 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,44 @@ def test_context_cleanup_handles_absence_and_intercepted_errors(
158161
assert managed._session is None
159162

160163

164+
@pytest.mark.parametrize(
165+
("client_timeout", "delete_timeout_seconds", "expect_deadline"),
166+
[(0.2, None, True), (0.2, 5, False), (5, 0.2, True)],
167+
)
168+
def test_context_cleanup_delete_timeout_is_opt_in(
169+
cleanup_client,
170+
monkeypatch,
171+
client_timeout,
172+
delete_timeout_seconds,
173+
expect_deadline,
174+
):
175+
client, state = cleanup_client
176+
state.exists = True
177+
state.delete_delay = 0.5
178+
monkeypatch.setattr(
179+
SandboxClient,
180+
"from_active_cluster",
181+
classmethod(lambda _cls, **_kwargs: client),
182+
)
183+
184+
managed = Sandbox(
185+
workspace="default",
186+
sandbox="cleanup-test",
187+
delete_timeout_seconds=delete_timeout_seconds,
188+
)
189+
expected = pytest.raises(GatewayError) if expect_deadline else nullcontext()
190+
with expected as caught, managed:
191+
# Only the exit delete runs under the short deadline.
192+
client._timeout = client_timeout
193+
state.exists = False
194+
if caught is not None:
195+
assert caught.value.code() == grpc.StatusCode.DEADLINE_EXCEEDED
196+
assert state.calls == ["GetSandbox", "GetSandbox", "DeleteSandbox"]
197+
assert state.closed == [True]
198+
assert managed._client is None
199+
assert managed._session is None
200+
201+
161202
class _EntryFailureClient:
162203
"""Fails the named client call and records close()."""
163204

‎tests/sandbox_test.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3274,3 +3274,25 @@ def test_sandbox_session_delete_passes_workspace() -> None:
32743274

32753275
assert stub.delete_request is not None
32763276
assert _request_workspace(stub.delete_request) == "staging"
3277+
3278+
3279+
def test_sandbox_session_delete_omits_timeout_for_legacy_clients() -> None:
3280+
from openshell.sandbox import SandboxSession
3281+
3282+
calls: list[tuple[str, str, bool]] = []
3283+
3284+
class LegacyClient:
3285+
def delete(self, name, *, workspace, allow_missing=False):
3286+
calls.append((name, workspace, allow_missing))
3287+
3288+
ref = SandboxRef(
3289+
id="sandbox-1",
3290+
name="job-1",
3291+
workspace="staging",
3292+
status=SandboxStatusRef(phase=2, current_policy_version=0),
3293+
)
3294+
session = SandboxSession(cast("Any", LegacyClient()), ref)
3295+
3296+
session.delete(allow_missing=True)
3297+
3298+
assert calls == [("job-1", "staging", True)]

0 commit comments

Comments
 (0)