Skip to content

Timeouts and cancellation

Every wait in fastsandbox has a deadline, but no single deadline bounds a whole run, and a deadline that expires ends the wait on your side, not always the work in the sandbox. This page maps each deadline, what it covers, and what a cancellation stops.

The deadlines

Deadline Default Covers Set with
Ordinary request 30 s Health at connection, listing and inspecting, credentials, policy; the start of a stream, a session or an attached command connect(timeout=...)
Long operation 15 min Creating, starting, stopping, deleting and saving a sandbox; exec without a deadline of its own; file transfers; pulling, loading and exporting images; installing kits connect(long_operation_timeout=...)
One command The long operation's One exec() exec(..., timeout=30)
One agent prompt None, then 10 s The wait for the end of the output of one prompt(), not the start of the agent (operation agent run); then the wait for its exit code (execution exit) prompt(..., timeout="15m")
Waiting for a sandbox to run 60 s wait_running() its timeout
An interactive session 60 s, then 10 s Interactive.wait(), then the exit code after the output ends in collect() their arguments
Compiling a kit with sbx 600 s One compilation KitCache(..., timeout=...)
A task None The call of its function only @app.task(timeout="10m")
The exit of the interpreter 10 s The cleanups the crates finish in tasks of their own; a second Ctrl-C gives the wait up none

There is no overall deadline for a run. A streamed agent (stream()), an agent session and ctx.wait have none of their own; the deadline of the task bounds them when it has one.

An expired deadline raises SandboxTimeoutError, a TimeoutError whose after says how long it waited and whose operation says what.

What the deadline of a task covers

grid-columns: 3
horizontal-gap: 70
vertical-gap: 60
inputs: Validate the inputs
slot: "Wait for a slot\nmax_concurrency"
connect: "Connect,\nenter the lifespan"
launch: "Forge the kit,\nlaunch the sandbox"
upload: "Upload directories,\nresolve dependencies"
call: Call the function {style.stroke-width: 4; style.bold: true}
remove: "Close dependencies,\nremove the sandbox"
result: Validate the result
inputs -> slot -> connect -> launch -> upload -> call -> remove -> result

Only the call of the function runs under the timeout of the task, the step in bold above. Validating, waiting for a slot, connecting, forging, launching, uploading, resolving and closing dependencies, removing the sandbox and validating the result come before or after it, each under the deadlines of its own requests. Only the expiry of the task's own deadline becomes the SandboxTimeoutError of the task, operation="task run"; a deadline inside the function, that of a command for instance, goes on as it is.

A deadline ends the wait, not the work

  • A command. The deadline of exec() is kept by the client and not sent to the daemon: when it expires, the command goes on in the sandbox, as the crate documents. Cancelling the call does the same.
  • An agent. The timeout of prompt() ends the wait for the agent and raises; nothing stops the agent, which goes on until it ends or its sandbox is removed, writing to a read-write workspace in the meantime. Leaving the loop of stream() early stops the reading, not the agent.
  • A run. When the function of a task times out or is cancelled, the run removes its sandbox, which ends everything still running in it.

So the removal of the sandbox is what stops remote work. A sandbox you keep, with keep=True or an awaited client.create(), keeps whatever runs in it.

Synchronous tasks and dependencies

  • A task written as a plain def runs in a worker thread. A thread cannot be interrupted: when the task times out, the run raises and removes the sandbox, but the function goes on until it returns, and its calls to the removed sandbox fail. asyncio.run, and with it run_sync, fastsandbox.run and the command line, waits for that thread before it returns.
  • A synchronous dependency runs on the thread of the event loop, not in a worker thread: while it runs, it blocks every other run of that loop, and no task deadline covers it. Keep synchronous dependencies short, or write them as coroutines.

Cancellation

Cancelling a run, an async with block or any awaitable of fastsandbox drops the request under way, then runs the cleanup:

  • the sandbox of a block or a run is removed, and the cancellation goes on;
  • a creation, a launch, a kit compilation or the removal of a lease cut short is finished by the crate in a task of its own, which removes what it made; the interpreter waits for those tasks as it exits, for 10 s at most or until a second Ctrl-C, and warns on the fastsandbox logger of those it cuts short;
  • a second cancellation during the cleanup of an async with client.create() block interrupts it: the sandbox may stay.

Under asyncio.run, the first Ctrl-C cancels the program and lets the cleanup run; a second Ctrl-C interrupts it. The command line exits with code 130 on Ctrl-C. A kill, SIGTERM, is not handled by Python by default: the process ends at once, and nothing is removed.

Next