The examples on this page omit
auth. To authenticate with your W&B API key, install cwsandbox[wandb] and pass auth=AuthStrategy.WANDB to Sandbox.run(), Sandbox(), or Sandbox.session(). See Get started.Automatic cleanup
Automatic cleanup ties a sandbox’s lifetime to a Python construct (a context manager or the running process) so the SDK releases resources without explicitstop() calls.
Context managers (recommended)
Context managers are the recommended pattern because they guarantee cleanup even if the enclosed code raises an exception. The SDK stops sandboxes when the context exits:Global cleanup handlers
The SDK registers cleanup handlers that stop sandboxes owned by registered sessions when the process terminates. Standalone sandboxes created outside a session or context manager require explicit cleanup:Manual cleanup
Use manual cleanup when you need explicit control over when to release a sandbox or session. Manual cleanup lets you pass stop options or coordinate cleanup across long-running code.Sandbox stop()
Session close()
Batch cleanup
Orphan management
Orphans are sandboxes that outlive the process that created them. The following sections describe how to make orphans easy to find, how to query for them, and how to bring them back under managed cleanup.Tagging for discovery
The SDK’s automatic cleanup handlers prevent most orphans. Sandboxes can keep running after forced shutdowns (kill -9), network failures, or when you create sandboxes outside of sessions and context managers. Use tags to make any orphans discoverable later.
- Project or application name (
my-project) - Job or run identifier (
batch-job-123,run-2024-01-15) - Environment (
dev,staging,prod)