Skip to content

Commit ba44407

Browse files
authored
Add sandbox.detach to sandbox examples (#1501)
* Add sandbox.detach to sandbox examples * Address comments
1 parent e3e8584 commit ba44407

File tree

7 files changed

+26
-0
lines changed

7 files changed

+26
-0
lines changed

13_sandboxes/anthropic_computer_use.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ def is_server_up(url):
9999
# Note: The sandbox logs may mention `localhost:8080`.
100100
# Ignore this and use the printed tunnel URLs instead.
101101

102+
# After you are done calling methods on the `sandbox` object, we recommend calling `detach` to disconnect
103+
# your local `sandbox` object and clean up any client-side resources.
104+
sandbox.detach()
105+
102106
# When finished, you can terminate the sandbox from your [Modal dashboard](https://modal.com/containers)
103107
# or by running `Sandbox.from_id(sandbox.object_id).terminate()`.
104108
# The Sandbox will also spin down after one hour.

13_sandboxes/jupyter_sandbox.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,9 @@ def is_jupyter_up():
111111

112112
# You can now open this URL in your browser to access the Jupyter notebook!
113113

114+
# After you are done calling methods on the `sandbox` object, we recommend calling `detach` to disconnect
115+
# your local `sandbox` object and clean up any client-side resources.
116+
sandbox.detach()
117+
114118
# When you're done, terminate the sandbox using your [Modal dashboard](https://modal.com/sandboxes)
115119
# or by running `Sandbox.from_id(sandbox.object_id).terminate()`.

13_sandboxes/opencode_server.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ def main(
220220
sandbox = create_sandbox(image, timeout, app, sandbox_secrets, "/root/code")
221221
print_access_info(sandbox, password_secret_name)
222222

223+
# After you are done calling methods on the `sandbox` object, we recommend calling `detach` to disconnect
224+
# your local `sandbox` object and clean up any client-side resources.
225+
sandbox.detach()
226+
223227

224228
# ## Command-line options
225229

13_sandboxes/safe_code_execution.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,7 @@
112112
# Let's terminate the Sandbox to clean up after ourselves.
113113

114114
sandbox.terminate()
115+
116+
# After you are done calling methods on the `sandbox` object, we recommend calling `detach` to disconnect
117+
# your local `sandbox` object and clean up any client-side resources.
118+
sandbox.detach()

13_sandboxes/sandbox_agent.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,7 @@
7575
stderr = claude_ps.stderr.read()
7676
if stderr != "":
7777
print("Agent stderr:", stderr)
78+
79+
# After you are done calling methods on the `sandbox` object, we recommend calling `detach` to disconnect
80+
# your local `sandbox` object and clean up any client-side resources.
81+
sandbox.detach()

13_sandboxes/sandbox_pool.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ def add_sandbox_to_queue() -> None:
156156
raise Exception("Health check failed")
157157

158158
pool_queue.put(SandboxReference(id=sb.object_id, url=url, expires_at=expires_at))
159+
sb.detach()
159160

160161

161162
# We also have a utility function that can be `.spawn()`ed to terminate Sandboxes.
@@ -165,6 +166,7 @@ def terminate_sandboxes(sandbox_ids: list[str]) -> int:
165166
for id in sandbox_ids:
166167
sb = modal.Sandbox.from_id(id)
167168
sb.terminate()
169+
sb.detach()
168170
num_terminated += 1
169171

170172
print(f"Terminated {num_terminated} Sandboxes")

13_sandboxes/simple_code_interpreter.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,7 @@ def run_code(writer: modal.io_streams.StreamWriter, reader: Iterator[str], code:
117117
# Finally, let's clean up after ourselves and terminate the Sandbox.
118118

119119
sb.terminate()
120+
121+
# After you are done calling methods on the `sb` object, we recommend calling `detach` to disconnect
122+
# your local `sandbox` object and clean up any client-side resources.
123+
sb.detach()

0 commit comments

Comments
 (0)