Skip to main content
Async snippets with top-level await run inside an async function or a notebook that supports it. For a standalone script, use the asyncio.run(main()) structure from Your first sandbox.

Fail a sandbox workflow on a nonzero exit

A nonzero command exit returns a result. Calling run_workflow() below raises inside the context, discards unsaved changes, and closes the VM.

Advanced: a command returned a nonzero exit code

This is a process result, so run() returns it without raising FailedOperationError. The example uses disposable execution and leaves the session image unchanged.
Commands save by default. Inspect the exit code before continuing. A saved command can have changed files even when its process exits nonzero. Roll back explicitly if those changes should not remain selected.

Advanced: select a late result

A completed operation retains its original parent. If local navigation or another operation changes the selected position, committing the result saves a sibling without selecting it. To save and select that result, give it a local branch name:
Choose a fresh recovery branch name. The newer branch remains intact. The recovery branch selects the completed operation’s image; it does not merge the two filesystems.

Waiting failed, timed out, or was cancelled

Operation.wait() attempts cancellation when waiting raises and keeps the original exception. A cancelled operation raises InterruptedError; an operation with no successful result raises FailedOperationError. Client transport errors can also propagate. Cleanup is best effort: retain the operation UUID if you need to inspect its server status afterward. A subprocess wait timeout does not itself terminate the whole operation. Use its operation’s signal() or cancel(), or leave the operation context to shut it down. See Advanced: control a running sandbox and SDK exceptions.

Advanced: catch an operation failure

If the server cannot complete the operation, the SDK raises FailedOperationError and leaves history unchanged. Keep the operation UUID to investigate the failure: