Skip to content

FastSandbox

FastSandbox is the framework of fastsandbox: an application declares tasks with decorators, and each call of a task runs in a throw-away sandbox. The signature of the function drives the rest: its parameters are the validated inputs, its annotations ask for what is injected, and its return annotation validates the result. This page builds a first application and runs it; the next pages take each part in turn.

A first application

docs_src/tasks/first_app.py
import fastsandbox
from fastsandbox import Context, FastSandbox

app = FastSandbox("hello")


@app.task
async def uname(ctx: Context) -> str:
    """Says which system the sandbox runs."""
    output = await ctx.sandbox.exec("uname -sm", check=True)
    return output.stdout.strip()


if __name__ == "__main__":
    print(fastsandbox.run(uname))
  • FastSandbox("hello") creates the application. Its name prefixes the names of its sandboxes: a run of uname creates hello-uname- followed by a random suffix.
  • @app.task declares the function as a task named after it. Without a kit, its sandbox comes from the kit of the application, shell by default.
  • The ctx: Context parameter is injected: the Context of the run gives the sandbox (ctx.sandbox), the lease that owns it (ctx.lease), the client, the task and a logger. Parameters that are not injected are the inputs of the task.
  • ctx.sandbox.exec(...) runs a command in the sandbox; check=True raises CommandFailedError on a non-zero exit code.
  • The return annotation, str, validates the result.
  • The docstring is the description of the task, shown by fastsandbox tasks.

Running it

From the command line, with the file and the name of the task:

fastsandbox run docs_src/tasks/first_app.py uname

From Python, fastsandbox.run(uname) runs the task from synchronous code, as the script does under __main__; in async code, await app.run("uname") or await uname(). Running tasks covers every way, Command line the options the inputs become.

The first run of a kit forges it without the sbx CLI, the base kit rebuilt from its published image, and keeps the package in memory for the next runs of the process (see Kits).

What a run does

Every run goes through the same steps. Its sandbox is launched from a sandbox spec its task and its inputs describe, and removed at the end, on an error, a timeout or a cancellation too, unless the task keeps it; a killed process leaves it (see Sandbox lifecycle):

direction: down
run: "app.run('uname')"
inputs: Validate the inputs
connect: "Connect and enter the lifespan,\nunless the application is started"
describe: "Describe the sandbox: kit, network rules,\nworkspace, mounts, environment, resources, agent keys"
launch: "Launch it: the kit forged or reused, the sandbox\ncreated, given its HTTP rules and keys, started"
inject: "Upload directories, inject the Context\nand the dependencies"
call: "Call the function, under the timeout"
shutdown: "Shut the sandbox down, unless keep=True"
result: Validate the result
run -> inputs -> connect -> describe -> launch -> inject -> call -> shutdown -> result
  • The spec holds the kit of the task, its network rules, its variables and resources, the host directories its inputs mount, and, for an agent task, the keys of the agent found in the environment. A kit the forge takes is forged at launch through the kit cache of the client, once per composition; a kit the cache compiles with the sbx CLI is launched from the package it compiles, and a KitPackage given as the kit is launched as it is, saved in a directory or held in memory, which has none.
  • The launch creates the sandbox with its host rules, applies its HTTP rules, pushes its keys and starts it. A failure there removes the sandbox, unless the task keeps it, and raises LaunchError. A run cancelled while its sandbox is launched leaves none: the crate removes it in a task of its own, which the interpreter waits for as it exits, for 10 s at most.
  • The directories the inputs upload are copied once the sandbox runs; a credential the daemon withholds from it, and allowances that restrict nothing under a global policy that allows every host, are logged as warnings on the logger of the task.
  • The run holds the Lease of its sandbox, ctx.lease, with what the launch did, and shuts it down at the end, which removes the sandbox, its rules and its keys; with keep=True it lets the sandbox go instead (see Tasks).

The function itself runs on the host, in the process of the caller: what runs in the sandbox is what it asks the sandbox to run, through ctx.sandbox.

Where to go next

Page Covers
Tasks The options of a task: kit, network, timeout, environment, resources; synchronous tasks
Inputs and outputs How the signature validates the inputs and the result
Agents Tasks whose sandbox runs a coding agent
Host paths Directories of the host mounted, cloned or copied into the sandbox
Network rules The network rules of the sandbox of a task
Dependencies Injection by annotation and Depends
Lifespan and state What runs around the application, and the state it shares
Running tasks The ways to run a task, progress, concurrency and errors
Command line The fastsandbox command
Testing Testing an application against the fake daemon