Reset of a sandbox to a snapshot
This page is for a contributor who must change internal/sandbox/reset.go or a part that the reset touches.
These parts are commands, file operations, pools, suspension, snapshots and GPUs.
It describes how the daemon puts a sandbox back to the files of a snapshot, under the same ID.
After you read it, you know what a reset keeps, the steps of Manager.Reset, and what a client sees when a reset fails.
The reset uses boot in internal/sandbox/manager.go, the leases in internal/sandbox/suspend.go and the pools in internal/sandbox/pool.go.
Read manager.md, snapshots-and-forks.md and suspend.md first.
Decision 21 in decisions.md gives the reasons.
In short
Section titled “In short”- A reset gives a sandbox the files of a snapshot in a new container. The ID, the timeout, the volumes and the GPUs stay the same.
- The sandbox moves into the new container only when that container runs, so a reset that fails leaves the sandbox as it was.
- A reset can take a container from a pool. If no pool has one,
bootmakes a new container. - A reset stops all processes and closes the kept connection. A command in progress on the old container fails with
ErrReset.
The idea
Section titled “The idea”An episode of reinforcement learning starts from the same state each time. Before the reset, a client had to kill the sandbox and create a new one from the snapshot, so the ID changed for each episode.
A reset makes a new container from the snapshot, as a create from the snapshot makes one. The sandbox moves into the new container only when the new container runs, and then the daemon removes the old container. Thus a reset that fails leaves the sandbox as it was.
The wake of a suspended sandbox (see suspend.md) also gives a sandbox a new container under the same ID, with the same function boot.
The reset does not use wake, because wake starts from the files of the sandbox, not from a snapshot.
What a reset keeps and what it drops
Section titled “What a reset keeps and what it drops”| Part of the sandbox | After the reset |
|---|---|
| The ID | The same. |
The timeout and expires_at |
The same. The reset does not start the timeout again. |
cpus, memory_mb, network, metadata, suspend_after_seconds |
The same. |
| The volumes | The same mounts. The files in a volume stay: a volume is not in a snapshot. |
| The GPUs | The same devices. The GPU check runs again in the new container. |
| The environment variables | Those that the snapshot holds, with the env of the request on top. A snapshot holds the env of the create of its sandbox. |
image and snapshot |
image is the image of the snapshot. snapshot is the ID of the snapshot. |
| The files outside the volumes | Those of the snapshot. All changes after the snapshot are lost. |
| The processes | All stop with the old container. |
| The kept connection | The daemon closes it. The next command opens a new one in the new container. |
state |
running. A suspended sandbox is running after the reset. |
The snapshot can be a snapshot of a different sandbox. The reset uses the files of the snapshot and the settings of the sandbox that it resets.
The steps
Section titled “The steps”Manager.Reset does these steps:
- It refuses a request without
snapshot, and anenvwith a name that is not correct. It refusesNVIDIA_VISIBLE_DEVICES, as a create does. - If the engine has the method
NoReset, it refuses withUnsupportedError. The Kubernetes engine has this method. See “On Kubernetes” below. - It locks
opMuof the sandbox. A pause, a resume, a snapshot, a suspension, a wake or a different reset of the sandbox ends first. - From here, a client that closes the connection does not stop the reset. The reset has its own time limit of 5 minutes.
- Under
m.mu, it refuses a paused sandbox withErrPaused, and it adds a reference to the snapshot (refSnapshot). It setsresetDone. A new command or file operation now waits inuseuntil the reset ends. - It tries to take a container from a pool. See “A reset from a pool” below.
- If no pool has a container,
bootmakes a new container from the snapshot. The name isboxfork-<id>-<6 random characters>, because the old container still has the nameboxfork-<id>. A sandbox that runs gives its GPUs toboot(bootSpec.held).bootdoes not allocate them again, and runs the GPU check in the new container. A suspended sandbox has no GPU.bootallocates GPUs for its request (Want), aswakedoes. moveIntodoes the move underm.mu:- It puts the new container, the GPUs, the layers, the image and the snapshot into the record. The state becomes
running. - It adds the old container to
orphans. - It writes the state file. This is the point of commit.
- It cuts each lease on the old container, and closes the kept connection.
- It puts the new container, the GPUs, the layers, the image and the snapshot into the record. The state becomes
- It removes the reference to the earlier snapshot of the sandbox, and to the saved files of a suspension (
Disk). - It removes the old container before it answers the client. If the engine does not remove it,
stopReplacedstops its processes:- It sends
Killto the engine, if the engine has that method (engine.Killer). Docker getsPOST /containers/{id}/killwithSIGKILL. - It asks the engine (
Inspect) until the engine says that the container does not run. It waits for a maximum of 10 seconds. - The old container stays in
orphans, andretryOrphansremoves it later. - If the engine still says that the container runs, the reset gives an error. The sandbox is in the new container, but the message says that processes of the old container can still run.
- It sends
- It clears
resetDone. The commands that waited now run in the new container.
What fails and what the client sees
Section titled “What fails and what the client sees”| Case | Result |
|---|---|
| A command that runs in the old container, on the kept connection or with its own process | The call fails with ErrReset. The API gives 409 with the code sandbox_reset. |
| A command with a streamed answer | The stream ends with an error event with "code": "sandbox_reset". The status 200 was sent before. |
| A file operation in progress | It fails with ErrReset, as a command does. |
| A command or file operation that arrives during the reset | It waits, and then runs in the new container. |
| The snapshot does not exist, or its files are gone | 404 with snapshot_not_found. The sandbox stays as it was. |
| The new container does not start, or the GPU check fails | The reset fails. The daemon removes the new container. The sandbox keeps its old container and its GPUs. |
| The state file cannot be written | The reset fails. The record goes back to the old container, and the old container is not in orphans. The daemon removes the new container. |
| The engine does not remove the old container | The reset stops the processes of the old container before it answers 200. The old container stays until retryOrphans removes it. |
| The engine does not remove the old container and does not stop it | 500 with internal. The sandbox is in the new container. The message says that processes of the old container can still run until the daemon removes it. |
| A download whose data started to arrive | The status 200 came before the reset. The daemon closes the connection before Content-Length bytes arrive. The SDKs raise BoxforkError with “cut short”. |
| The sandbox is paused | 409 with sandbox_paused. Resume it first. |
| The engine has no reset (Kubernetes) | 501 with unsupported. |
A cut lease never gives success. lease.attach wraps the output and the input of the stream. After the cut, each read and each write gives ErrReset.
leaseErr gives ErrReset when the engine cannot start a process because the reset removed the container.
runKept gives ErrReset when the kept connection closes because of the cut.
A command of the old container does not touch the new container:
runKeptexamines the cut before it askskeptForfor the kept connection, and again whenkeptForgives no connection.keptForgives no connection to a lease on a container that the sandbox no longer has. It does not close the connection of the new container.- Thus a command of the old container never runs in the new container. A command on the connection of the new container continues.
Operations at the same time
Section titled “Operations at the same time”| With | Result |
|---|---|
| A kill | The kill does not wait for the reset. forget sees resetDone, and it does not release the GPUs. When the reset ends, it finds no record. It removes the new container and releases the GPUs, if no container of the sandbox is in orphans. |
| A kill that removes the old container when the new container runs | The kill sets dying first. moveInto sees dying: it does not move the sandbox, and it removes the new container. |
An old container in orphans that goes during the reset |
orphanGone does not release the GPUs while resetting has the sandbox. This is also true after a kill, when the record is gone. |
| A suspension | The suspension and the reset both hold opMu. One waits for the other. Reclaim does not suspend a sandbox during a reset, because the reset holds opMu. |
| A fork or a snapshot | Both lock opMu, so they come before or after the reset. A fork after the reset copies the files of the new container. A snapshot reads image and the paths to exclude under opMu, so they are those of the new container. |
| The delete of the snapshot | The reset holds a reference from step 5. DeleteSnapshot refuses with in_use. After the reset, the sandbox holds the reference, as a sandbox from that snapshot does. |
| A different reset | opMu puts the two resets one after the other. |
A reset from a pool
Section titled “A reset from a pool”A pool of the snapshot can have a container ready. resetFromPool takes it when all of these are true:
- The request has no
env. A container of a pool has no environment variables of its own. - The sandbox has no GPU and asks for none. A container of a pool has no GPU.
- A pool for the snapshot has the same
cpus,memory_mbandnetworkas the sandbox. - A waiting sandbox of that pool has the same volumes as the sandbox.
The reset takes only the container. The sandbox keeps its own settings, and the waiting sandbox goes away. The pool then starts a new one.
resetFromPool asks the engine if the container runs, as fromPool does. A container that stopped is drained, and the next waiting sandbox is tried.
The container keeps the name boxfork-<id of the waiting sandbox>. The state file has its ID in the record of the sandbox, so the start of the daemon finds it.
Thus do not use the name of a container to find the container of a sandbox after a reset. Use the state file.
Restart of the daemon in the middle of a reset
Section titled “Restart of the daemon in the middle of a reset”The point of commit is the write of the state file in step 8.
| The daemon stops | At the next start |
|---|---|
| Before the write | The state file has the old container. The new container has the label boxfork.managed, but no record has it. The start removes it. |
| After the write, before the old container is gone | The state file has the new container. The state file does not keep orphans. The old container has the label boxfork.managed, but no record has it, so the start removes it. |
Thus the sandbox has one container after each restart. The test TestADaemonThatStopsInTheMiddleOfAResetComesBackWithOneContainer stops the daemon at both points.
The start can fail to remove the old container. Then the start puts it in orphans. If the old container has the GPUs of the sandbox, the start holds them under the ID of the sandbox, not under leftover-<container>.
Thus a kill of the sandbox does not release the GPUs while the old container is there. retryOrphans releases them when the old container is gone.
On Kubernetes
Section titled “On Kubernetes”A reset on Kubernetes is not built. On Kubernetes, the API service runs the same manager on the Kubernetes engine.
The engine has the method NoReset in operator/internal/kubeengine/reset.go. Thus the API service refuses each reset with 501 and unsupported.
To start an episode again there, kill the sandbox and create a sandbox from the snapshot.
A control service sends a reset to the cluster of the sandbox, as it sends a pause.
The tests are in internal/sandbox/reset_test.go. Each one uses the test engine.
| Test | What it proves |
|---|---|
TestAResetGivesTheSandboxTheFilesOfTheSnapshotUnderItsID |
The files are those of the snapshot, the ID is the same, and the old container is gone. |
TestAResetKeepsWhatTheSandboxIs |
The timeout, the settings, the metadata and the volumes stay. |
TestAResetGivesTheNewContainerTheEnvironmentOfTheRequest |
The env of the request goes to the new container, and the GPU variable is refused. |
TestAResetNeedsASnapshotThatExists |
A missing snapshot, or one whose files are gone, leaves the sandbox as it was. |
TestAResetToASnapshotOfAnotherSandbox |
A snapshot of a different sandbox is accepted. |
TestACommandUnderWayAtAResetIsToldSo |
A command on the kept connection and a command with its own process fail with ErrReset, also when the engine does not remove the old container. |
TestAStreamedCommandUnderWayAtAResetIsToldSo |
A streamed command fails with ErrReset. |
TestAFileWrittenAtAResetIsToldSo |
A file operation in progress fails with ErrReset. |
TestACommandThatArrivesDuringAResetRunsAfterIt |
A command that arrives during the reset runs in the new container. |
TestAResetThatFailsLeavesTheSandboxAsItWas |
A container that does not start leaves the old one. |
TestASnapshotThatAResetUsesCannotBeDeleted |
The delete of the snapshot during the reset is refused. |
TestAResetLetsGoOfTheSnapshotTheSandboxCameFrom |
The earlier snapshot of the sandbox can be deleted after the reset. |
TestAResetBringsASuspendedSandboxBackOnTheSnapshot |
A suspended sandbox runs on the snapshot, and its saved files are released. |
TestAPausedSandboxIsNotReset |
A paused sandbox gives ErrPaused. |
TestAResetIsRefusedOnAnEngineThatCannotDoIt |
An engine with NoReset gives UnsupportedError. |
TestAGPUSandboxKeepsItsGPUsAcrossAReset |
The same GPUs, the GPU check in the new container, and the old GPUs after a failed check. |
TestAKillDuringAResetLeavesNothing |
A kill during the reset leaves no container and no held GPU. |
TestAResetWaitsForASnapshotAndAForkWaitsForTheReset |
opMu orders the reset with a snapshot and a fork. A suspension does not occur during a reset. |
TestAResetTakesAContainerFromAPoolOfTheSnapshot |
A pool of the snapshot gives its container, and fills again. |
TestAResetTakesNoContainerOfAnotherShapeFromAPool |
A different memory, network, env or volume makes a new container. |
TestAResetOfAGPUSandboxTakesNothingFromAPool |
A sandbox with a GPU does not use a pool. |
TestADaemonThatStopsInTheMiddleOfAResetComesBackWithOneContainer |
A restart before and after the point of commit leaves one container. |
TestAResetThatCannotBeRecordedLeavesTheSandboxAsItWas |
A state file that cannot be written leaves the old container, and Reconcile does not remove it after that. |
TestAnOldContainerThatStaysAfterAResetIsRemovedLater |
An old container that the engine did not remove stays in orphans, and Reconcile removes it. |
TestACommandThatStartsTooLateInTheOldContainerIsToldOfTheReset |
A command whose process starts after the old container is gone gets ErrReset, not ErrNotFound. |
TestALateCommandOfTheOldContainerLeavesTheNewConnectionAlone |
A lease on the old container gets no kept connection, and a command on the connection of the new container continues. |
TestACommandThatWaitedForTheConnectionDuringAResetDoesNotRunAfterIt |
A command that waits for the kept connection during the reset gets ErrReset, and it does not run in the new container. |
TestASnapshotAskedForDuringAResetSaysWhatTheNewContainerIs |
A snapshot during a reset has the image and the excluded paths of the new container. |
TestAnOldContainerThatIsNotRemovedIsStoppedBeforeTheResetAnswers |
When the engine does not remove the old container, the old container does not run when the reset answers. |
TestAResetSaysSoWhenTheOldContainerMayStillRun |
When the engine also does not stop it, the reset gives an error. A process that starts in the old container after the cut gets ErrReset. |
TestAGPUStaysHeldWhileAContainerAResetReplacedIsLeftAtAStart |
An old container that the start cannot remove keeps the GPU held, also after a kill of the sandbox. |
TestAGPUStaysHeldWhileAnyContainerOfAKilledSandboxIsThere |
A kill during a reset does not release the GPU while an older container or the container of the reset is there. |
TestAKillUnderWayWhenTheNewContainerRunsWins |
A kill that removes the old container when the new one runs leaves no container and no held GPU. |
TestARunningGPUSandboxThatCannotBeResetStaysAsItWas |
The GPU error of a sandbox that runs says that it stays as it was, not that it is suspended. |
TestAResetOfASuspendedGPUSandboxGivesItAGPU |
A suspended GPU sandbox gets a GPU, and the GPU check runs. |
TestASuspendedSandboxWhoseOldContainerStaysIsNotResetYet |
A suspended sandbox whose old container is in orphans is not reset until that container is gone. |
TestAResetTakesNoStoppedContainerFromAPool |
A container of a pool that stopped is not given to the sandbox. |
TestInputToACommandAResetCutIsToldSo |
Input and a signal to a command after the cut give ErrReset. |
TestAKeptCommandInAnOldContainerThatRunsOnIsToldOfTheReset |
When the old container runs on, a command on its kept connection gets ErrReset, because the reset closes the connection. |
The tests of the layers above are in internal/api/reset_test.go, internal/client/reset_test.go, cmd/boxfork/reset_test.go, internal/tools/reset_test.go, operator/internal/kubeengine/reset_test.go and control/internal/router/reset_test.go.
The proof on a real engine with gVisor is scripts/proof/reset.sh.
On 6 December 2026 it passed 69 of 69 checks with the build 0.1.0-reset2, on the test machine of performance.md, with Docker and gVisor.
It kills a reset at 5 points, and it stops the daemon with SIGKILL at 8 points of a reset. Each time, the sandbox had one container after the restart.
It also examines a file that a process of the old container writes in a volume: the file stops growing when the reset answers.
The script also sends commands to the old and the new container at each reset. With the build 0.1.0-reset, this check did not find the fault of keptFor. Thus it does not prove the correction. The test engine proves it.
scripts/proof/reset-faults.sh runs the daemon through scripts/proof/faultproxy. The proxy refuses the removal, and then the kill, of an old container.
With the build 0.1.0-reset2 it passed 15 of 15 checks. With the build 0.1.0-reset, 4 checks failed: the old container continued to run and to write to the volume after the answer 200.
Break-it checks
Section titled “Break-it checks”Three sets of changes to the code were tried, each on a copy of the code of this page. A change is killed when at least one test fails:
- The 20 changes of the first author: 20 killed. One of them was adapted to the new removal of the old container.
- The 22 changes of the review: 21 killed. One of them was adapted to the new
resettingmap. The change that lets a reset take a sandbox that waits in a pool is not killed. A second check underopMurefuses that reset also. - 11 changes to the corrections of the review: 10 killed. The change that removes the first check of the cut in
runKeptis not killed. A cut always comes with a new container, sokeptForreturns nil and the second check givesErrReset. The first check only prevents a wait for the connection.
These are examples of killed changes:
- no cut of the leases, or no wait in
use - no reference to the snapshot during the reset
- a new allocation of the GPUs
- the removal of the old container after the answer
- no record of the old container in
orphans - no close of the kept connection
- no stop of an old container that the engine does not remove
- a kept connection that a command of the old container closes
- GPUs freed while a container of the sandbox is still there
Not proven
Section titled “Not proven”- Nothing ran on a machine with a GPU. The tests of the GPUs use the test engine and a GPU allocator with stand-in devices.
- A reset of a sandbox with a large snapshot, and many resets at the same time, were not measured.
- A file operation in progress at a reset is proven only with the test engine.