Skip to content

Commit 2da4883

Browse files
committed
fix(python): close Sandbox client on context entry failure
Fixes NVIDIA#3922 Signed-off-by: Honey Tyagi <[email protected]>
1 parent 21fea95 commit 2da4883

1 file changed

Lines changed: 35 additions & 28 deletions

File tree

‎python/openshell/sandbox.py‎

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,36 +1614,43 @@ def __enter__(self) -> Sandbox:
16141614
)
16151615
self._client = client
16161616

1617-
if self._sandbox_input is None and self._workload_template is not None:
1618-
self._session = client.create_session_from_template(
1619-
workspace=self._workspace,
1620-
workload_template=self._workload_template,
1621-
spec=self._spec,
1622-
name=self._name,
1623-
labels=self._labels,
1624-
)
1625-
elif self._sandbox_input is None:
1626-
self._session = client.create_session(
1627-
workspace=self._workspace,
1628-
spec=self._spec,
1629-
name=self._name,
1630-
labels=self._labels,
1631-
)
1632-
elif isinstance(self._sandbox_input, SandboxRef):
1633-
self._session = SandboxSession(client, self._sandbox_input)
1634-
else:
1635-
self._session = client.get_session(
1636-
self._sandbox_input, workspace=self._workspace
1637-
)
1617+
try:
1618+
if self._sandbox_input is None and self._workload_template is not None:
1619+
self._session = client.create_session_from_template(
1620+
workspace=self._workspace,
1621+
workload_template=self._workload_template,
1622+
spec=self._spec,
1623+
name=self._name,
1624+
labels=self._labels,
1625+
)
1626+
elif self._sandbox_input is None:
1627+
self._session = client.create_session(
1628+
workspace=self._workspace,
1629+
spec=self._spec,
1630+
name=self._name,
1631+
labels=self._labels,
1632+
)
1633+
elif isinstance(self._sandbox_input, SandboxRef):
1634+
self._session = SandboxSession(client, self._sandbox_input)
1635+
else:
1636+
self._session = client.get_session(
1637+
self._sandbox_input, workspace=self._workspace
1638+
)
16381639

1639-
self._workspace = getattr(self._session, "_workspace", self._workspace)
1640+
self._workspace = getattr(self._session, "_workspace", self._workspace)
16401641

1641-
ready = client.wait_ready(
1642-
self._session.sandbox.name,
1643-
workspace=self._workspace,
1644-
timeout_seconds=self._ready_timeout_seconds,
1645-
)
1646-
self._session = SandboxSession(client, ready)
1642+
ready = client.wait_ready(
1643+
self._session.sandbox.name,
1644+
workspace=self._workspace,
1645+
timeout_seconds=self._ready_timeout_seconds,
1646+
)
1647+
self._session = SandboxSession(client, ready)
1648+
except Exception:
1649+
with contextlib.suppress(Exception):
1650+
client.close()
1651+
self._client = None
1652+
self._session = None
1653+
raise
16471654

16481655
return self
16491656

0 commit comments

Comments
 (0)