Sandbox lifecycle¶
A sandbox lives in the daemon, not in your program: it exists from its creation until something deletes it, and it runs only while a session holds it. This page explains who owns a sandbox, what removes it, and what each way of ending leaves behind.
Sessions keep a sandbox running¶
A sandbox runs right after its creation. A session is an open request to the daemon that holds it running; the daemon stops a sandbox about 30 seconds after its last session ends, and a sandbox that never had one stops about 30 seconds after its creation. A stopped sandbox keeps its files, its rules and its keys, but refuses commands and file transfers until it starts again.
sandbox.running()starts the sandbox if needed and holds a session for the duration of its block.- A lease holds a session from its launch until it is shut down or detached.
- A task holds the session of its lease for the whole run.
The daemon knows two states, running and stopped, which
SandboxInfo.status reports.
direction: right
created: "" {shape: circle; width: 16; height: 16; style.fill: "#7a7a7a"}
running: running
stopped: stopped
deleted: "" {shape: circle; width: 16; height: 16; style.double-border: true}
created -> running: created
running -> stopped: "about 30 s after\nthe last session"
stopped -> running: "started, or\na session held"
running -> deleted: deleted
stopped -> deleted: deleted
Who owns a sandbox¶
Each API that creates a sandbox decides who removes it:
| Created by | Owner | Removed when |
|---|---|---|
await client.create(...) |
Your program | You call delete(). Nothing else removes it. |
async with client.create(...) |
The block | The block ends. |
async with spec.launch(...) |
The Lease |
The block ends, or lease.shutdown(). lease.detach() hands the sandbox to your program instead. |
| A run of a task | The run | The function ends, unless the task has keep=True. |
client.sandbox(name) |
Whoever created it | fastsandbox does not remove a sandbox it only looked up. |
A lease and an async with client.create(...) block remove their sandbox by
its identifier, so they never remove another sandbox that took the same name.
Removing a sandbox removes the network rules scoped to it
and the keys pushed to it.
What ends a sandbox, case by case¶
shape: sequence_diagram
P: Your program
D: sandboxd
P -> D: create the sandbox
D -> P: created {style.stroke-dash: 3}
P -> D: "hold a session,\nwork in the sandbox"
"the block returns, raises or is cancelled": {
P -> D: delete the sandbox
}
"the process is killed": {
D."the session closes, the sandbox stops\nabout 30 s later and stays"
}
| How the block or run ends | The sandbox |
|---|---|
| It returns | Removed. A failed removal is raised, except by a run, which logs it as a warning on the logger of its task and returns its result. |
| It raises an exception | Removed. The exception goes on with its own type; a failed removal is noted on it and logged as a warning. |
| It is cancelled, or times out | Removed, as for an exception. A second cancellation while the removal runs interrupts it. |
Ctrl-C under asyncio.run, fastsandbox.run, run_sync or the command line |
The first Ctrl-C cancels the program, which removes its sandboxes; a second one can interrupt the removal. |
| Its creation fails before the sandbox exists | Nothing was created: a spec, kit or input error, or a name already taken. |
| An install command of the kit fails | The creation fails, and the daemon removes the sandbox. |
| Its launch fails once the sandbox exists | LaunchError; the sandbox is removed unless the spec keeps it on failure or the removal failed, which kept says. |
| It is cancelled during the creation or the launch | The crate finishes the creation in a task of its own, then deletes the sandbox; the interpreter waits for it as it exits, for 10 s at most or until a second Ctrl-C. A task still under way then is cut short, which the fastsandbox logger warns of, and the sandbox may stay. |
The task has keep=True, or the lease is detached |
Kept, with its rules, its keys and its files, until you remove it. |
| The process is killed, crashes, or the machine stops | Not removed. A plain kill (SIGTERM) counts: Python does not handle it by default. Its sessions close, so it stops about 30 seconds later, and stays with its files, rules and keys. |
| The daemon restarts | Not determined: no test or audit covers it. Requests under way fail. |
Find what a killed process left
sbx ls lists the sandboxes of your user, stopped ones included. Those of
fastsandbox are named fastsandbox- and a random suffix for the client
and specs, and <application>-<task>- and a random suffix for a run.
fastsandbox sets no label, and another program, still running, may use
the same prefix: check the status and the time of creation before you
remove one. Failures and cleanup shows how to
remove a sandbox only if it is still the one you saw.
What stays after a sandbox¶
Some things outlive every sandbox on purpose:
| What | Where | Removed by |
|---|---|---|
| The images of a kit: the base image, and the images a forged package loads | The image store of the daemon | You (client.images.remove()). The daemon also drops the tags of assembled kit images by itself, some minutes after their last sandbox, and keeps the base images |
| Kit packages kept in a directory | The directory of the kit cache, when one is given | You |
Templates saved with save() |
The image store of the daemon | You |
| The global network policy | The daemon | fastsandbox never changes it unless you call client.policy |
A creation cut short, by a process killed during it for instance, can leave
the network rules of its kit, scoped to its name, which sbx policy rm cannot
remove: the next sandbox of that name finds them, and they go when it is
removed. The client and FastSandbox give each sandbox a unique name by
default.
Next¶
- Sandboxes: creating, inspecting, starting, stopping and deleting with the client.
- Tasks: the sandbox of a run,
and
keep=True. - Failures and cleanup: errors, leftovers and how to remove them safely.