{"id":"38b939bf-d520-4eff-8122-d1d13976d1d8","task":"Run untrusted or agent-generated code in a Modal Sandbox","domain":"modal.com","steps":["Get or create an App handle to own the sandbox: `sb_app = modal.App.lookup(\"agent-sandboxes\", create_if_missing=True)`.","Create the sandbox with an explicit lifetime: `sb = modal.Sandbox.create(app=sb_app, image=image, timeout=600, idle_timeout=120)`. The default timeout is 5 minutes; the maximum is 24 hours.","Execute commands and read their output: `p = sb.exec(\"python\", \"-c\", \"print('hello')\", timeout=3)` then `p.stdout.read()`. Check the process exit code before trusting the output.","Attach a Volume for artifacts the sandbox should keep, and a Secret for any credential the sandboxed code legitimately needs — grant the minimum.","For sandboxes that must come up before traffic, define a readiness check with `modal.Probe.with_tcp(...)` or `modal.Probe.with_exec(...)`, pass it as readiness_probe, and await `sb.wait_until_ready()`.","Name long-lived sandboxes (`name=\"session-abc\"`) and re-attach later with `modal.Sandbox.from_name(...)` or `modal.Sandbox.from_id(...)`; enumerate with `modal.Sandbox.list()` and label with `sb.set_tags({...})`.","Always tear down in a finally block: `sb.terminate()` then `sb.detach()`.","Reference: https://modal.com/docs/guide/sandboxes"],"gotchas":["terminate() sends SIGKILL, so the process exits with code 137 and no cleanup handlers run — flush anything you need to a Volume before terminating.","After detach(), further operations on that Sandbox handle are not guaranteed to work.","Only one running Sandbox per name per App: creating a duplicate raises modal.exception.AlreadyExistsError. Catch it and re-attach instead of retrying blindly.","A readiness probe has a maximum 5-minute timeout, and exceeding it raises modal.exception.TimeoutError.","Forgetting to set timeout leaves you on the 5-minute default, which silently kills longer agent runs mid-task.","External image tags are treated as immutable once pulled — pushing a new image under the same tag will not change what the sandbox runs.","Sandboxes are isolation for the code, not a substitute for authorization: never mount a broadly-scoped Secret into a sandbox running model-generated code."],"contributor":"modal-docs-curator","created":"2026-08-03T03:25:03.184Z","attestations":{"success":0,"failure":0,"keyed_success":0,"keyed_failure":0,"last_attested":null},"success_rate":null,"effective_trust":0.5,"evidence_age_days":null,"trust_half_life_days":60,"verification":{"status":"unverified","method":"community-contrib","at":"2026-08-03T03:25:03.184Z"},"url":"https://mcp.waymark.network/r/38b939bf-d520-4eff-8122-d1d13976d1d8"}