Sandboxes can run for hours or days. Two independent settings govern how long one lasts: lifetime bounds its total run time, and restart_policy decides whether Chalk recreates it when its backing instance goes away.

​
Setting a lifetime

Omit lifetime to run until you terminate the sandbox yourself. Set it to bound the run:

ValueEffect
omittedThe sandbox runs until it is terminated explicitly
"86400s"The sandbox is terminated 24 hours after it starts
"604800s"The sandbox is terminated 7 days after it starts

There is no maximum value. lifetime="0s" raises ValueError rather than meaning “no limit”; omit the argument for that.

When a lifetime expires, Chalk stops the sandbox immediately without draining in-flight work, and records the result as succeeded. Write anything you need to keep to a mounted volume before that point.

​
Surviving node drains and preemption

A sandbox running for days outlives the node it started on. By default, the backing instance terminating ends the sandbox. Set restart_policy to have Chalk recreate it instead:

from chalkcompute import Image, RestartPolicy, Sandbox, Volume

Volume("training-checkpoints")  # created if it does not already exist

sandbox = Sandbox(
    image=Image.debian_slim("3.12").pip_install(["torch"]),
    name="multi-day-training",
    cpu="8",
    memory="32Gi",
    volumes=[("training-checkpoints", "/checkpoints")],
    restart_policy=RestartPolicy.ALWAYS,
).run()

Leaving restart_policy unset behaves the same as RestartPolicy.NEVER. A recreated sandbox starts from its image again with an empty ephemeral filesystem, so write progress to a mounted Volume and have the workload resume from it on startup.

To start work that runs for hours and check on it later, see Running work across sessions.