Skip to content

Commit fd6814f

Browse files
committed
fix(python): preserve wrapped RPC cleanup handling
Inspect the original gRPC call when handling missing sandboxes during deletion waits and managed cleanup. Add intercepted cleanup regressions and clarify the error-wrapper migration contract. Addresses GATOR-966d7aba-01 on #3313; part of #3051. Signed-off-by: Mrunal Patel <[email protected]>
1 parent 966d7ab commit fd6814f

4 files changed

Lines changed: 165 additions & 6 deletions

File tree

‎architecture/gateway.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ write conflicts attach `google.rpc.ErrorInfo` with a stable reason and current
5959
version when available. `google.rpc.RetryInfo` expresses a minimum retry delay;
6060
it does not establish that a mutation is safe to repeat. SDKs retain the original
6161
transport status, metadata, and unknown details alongside decoded fields.
62+
Python cleanup inspects the original gRPC call beneath a typed error wrapper,
63+
preserving missing-resource handling without suppressing other failures.
6264

6365
The gateway listens on one service port and multiplexes gRPC and HTTP traffic.
6466
The default local single-user deployment mode is mTLS user authentication:

‎docs/reference/api-errors.mdx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ seconds. Absent or invalid delay details do not produce a suggested delay.
7474

7575
Existing error codes and human-readable messages remain available. Python curated
7676
clients now raise `GatewayError`, which remains a `grpc.RpcError`; existing
77-
`except grpc.RpcError` handlers continue to work. Rust error variants now retain
77+
`except grpc.RpcError` handlers continue to work. `GatewayError` is not a
78+
`grpc.Call`. If your handler checks that interface, inspect `raw_error` before
79+
checking the status. The Python SDK's deletion wait and managed-sandbox cleanup
80+
use the original call to recognize `NOT_FOUND`; other failures still propagate.
81+
Rust error variants now retain
7882
status fields; use `..` when destructuring variants that do not need those fields.
7983
Go and TypeScript add typed detail fields to their existing error types.
8084

‎python/openshell/sandbox.py‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
openshell_pb2,
2929
openshell_pb2_grpc,
3030
)
31-
from .errors import _error_mapping_channel
31+
from .errors import GatewayError, _error_mapping_channel
3232

3333
_ClientCallDetailsBase = namedtuple(
3434
"_ClientCallDetailsBase",
@@ -991,9 +991,10 @@ def wait_deleted(
991991
try:
992992
self.get(sandbox_name, workspace=workspace)
993993
except grpc.RpcError as exc:
994+
call = exc.raw_error if isinstance(exc, GatewayError) else exc
994995
if (
995-
isinstance(exc, grpc.Call)
996-
and exc.code() == grpc.StatusCode.NOT_FOUND
996+
isinstance(call, grpc.Call)
997+
and call.code() == grpc.StatusCode.NOT_FOUND
997998
):
998999
return
9991000
raise
@@ -1562,9 +1563,10 @@ def __exit__(self, *args: object) -> None:
15621563
workspace=self._workspace,
15631564
)
15641565
except grpc.RpcError as exc:
1566+
call = exc.raw_error if isinstance(exc, GatewayError) else exc
15651567
if (
1566-
not isinstance(exc, grpc.Call)
1567-
or exc.code() != grpc.StatusCode.NOT_FOUND
1568+
not isinstance(call, grpc.Call)
1569+
or call.code() != grpc.StatusCode.NOT_FOUND
15681570
):
15691571
raise
15701572
finally:
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
"""Cleanup status handling through the curated client's real gRPC interceptor."""
5+
6+
from concurrent import futures
7+
from contextlib import nullcontext
8+
from types import SimpleNamespace
9+
10+
import grpc
11+
import pytest
12+
13+
from openshell._proto import openshell_pb2
14+
from openshell.errors import GatewayError
15+
from openshell.sandbox import Sandbox, SandboxClient
16+
17+
18+
@pytest.fixture
19+
def cleanup_client():
20+
state = SimpleNamespace(
21+
exists=False,
22+
code=grpc.StatusCode.NOT_FOUND,
23+
calls=[],
24+
closed=[],
25+
)
26+
27+
def fail(context):
28+
context.set_trailing_metadata((("request-id", "cleanup-regression"),))
29+
context.abort(state.code, "cleanup status")
30+
31+
def get(request, context):
32+
state.calls.append("GetSandbox")
33+
assert request.name == "cleanup-test"
34+
assert request.workspace_scope.workspace == "default"
35+
if not state.exists:
36+
fail(context)
37+
response = openshell_pb2.SandboxResponse()
38+
response.sandbox.metadata.id = "sandbox-1"
39+
response.sandbox.metadata.name = request.name
40+
response.sandbox.metadata.workspace = "default"
41+
response.sandbox.status.phase = openshell_pb2.SANDBOX_PHASE_READY
42+
return response
43+
44+
def delete(request, context):
45+
state.calls.append("DeleteSandbox")
46+
assert request.name == "cleanup-test"
47+
assert request.workspace_scope.workspace == "default"
48+
fail(context)
49+
50+
server = grpc.server(futures.ThreadPoolExecutor(max_workers=1))
51+
server.add_generic_rpc_handlers(
52+
(
53+
grpc.method_handlers_generic_handler(
54+
"openshell.v1.OpenShell",
55+
{
56+
"GetSandbox": grpc.unary_unary_rpc_method_handler(
57+
get,
58+
request_deserializer=openshell_pb2.GetSandboxRequest.FromString,
59+
response_serializer=openshell_pb2.SandboxResponse.SerializeToString,
60+
),
61+
"DeleteSandbox": grpc.unary_unary_rpc_method_handler(
62+
delete,
63+
request_deserializer=openshell_pb2.DeleteSandboxRequest.FromString,
64+
response_serializer=openshell_pb2.DeleteSandboxResponse.SerializeToString,
65+
),
66+
},
67+
),
68+
)
69+
)
70+
port = server.add_insecure_port("127.0.0.1:0")
71+
server.start()
72+
client = SandboxClient(
73+
f"127.0.0.1:{port}",
74+
timeout=5,
75+
_bearer_close=lambda: state.closed.append(True),
76+
)
77+
try:
78+
yield client, state
79+
finally:
80+
client.close()
81+
server.stop(0).wait()
82+
83+
84+
def assert_original_call(error, code):
85+
assert isinstance(error, GatewayError)
86+
assert isinstance(error.raw_error, grpc.Call)
87+
assert error.code() == error.raw_error.code() == code
88+
assert error.details() == "cleanup status"
89+
assert ("request-id", "cleanup-regression") in error.trailing_metadata()
90+
91+
92+
@pytest.mark.parametrize(
93+
"code",
94+
[
95+
grpc.StatusCode.NOT_FOUND,
96+
grpc.StatusCode.PERMISSION_DENIED,
97+
grpc.StatusCode.UNAVAILABLE,
98+
],
99+
)
100+
def test_wait_deleted_handles_intercepted_status(cleanup_client, code):
101+
client, state = cleanup_client
102+
state.code = code
103+
# Prove this is the curated intercepted stub, not a fake raw exception.
104+
with pytest.raises(GatewayError) as observed:
105+
client.get("cleanup-test", workspace="default")
106+
assert_original_call(observed.value, code)
107+
108+
expected = (
109+
nullcontext()
110+
if code == grpc.StatusCode.NOT_FOUND
111+
else pytest.raises(GatewayError)
112+
)
113+
with expected as caught:
114+
client.wait_deleted("cleanup-test", workspace="default", timeout_seconds=5)
115+
if caught is not None:
116+
assert_original_call(caught.value, code)
117+
assert state.calls == ["GetSandbox", "GetSandbox"]
118+
119+
120+
@pytest.mark.parametrize(
121+
"code",
122+
[
123+
grpc.StatusCode.NOT_FOUND,
124+
grpc.StatusCode.PERMISSION_DENIED,
125+
grpc.StatusCode.UNAVAILABLE,
126+
],
127+
)
128+
def test_context_cleanup_handles_intercepted_status(cleanup_client, monkeypatch, code):
129+
client, state = cleanup_client
130+
state.code = code
131+
state.exists = True
132+
monkeypatch.setattr(
133+
SandboxClient,
134+
"from_active_cluster",
135+
classmethod(lambda _cls, **_kwargs: client),
136+
)
137+
138+
managed = Sandbox(workspace="default", sandbox="cleanup-test")
139+
expected = (
140+
nullcontext()
141+
if code == grpc.StatusCode.NOT_FOUND
142+
else pytest.raises(GatewayError)
143+
)
144+
with expected as caught, managed:
145+
state.exists = False
146+
if caught is not None:
147+
assert_original_call(caught.value, code)
148+
assert state.calls == ["GetSandbox", "GetSandbox", "DeleteSandbox"]
149+
assert state.closed == [True]
150+
assert managed._client is None
151+
assert managed._session is None

0 commit comments

Comments
 (0)