Skip to content

The kept connection

This page tells you how the daemon sends the commands of one sandbox down one open connection to the helper. It is for a contributor who must change internal/sandbox/kept.go or the serve mode of the helper. The serve mode is runServe in internal/guest/run.go. The frames are in internal/guest/proto.go.

Read commands-and-files.md first. It describes Manager.Run, the spec of a command and the result. The page helper.md gives the full frame protocol.

Without the kept connection, each command is one engine exec: the engine starts a new helper process in the container for it. ROADMAP.md records the measurement that led to this design: almost all of the time of a command was the engine exec.

With the kept connection, the engine starts the helper one time, in serve mode. The daemon keeps that stream open. Each subsequent command is one frame on the stream, and the helper starts the command itself.

These numbers come from README.md. The measurement was on the test machine: Docker with gVisor, 30 rounds each.

Case Median time of one command
Each command with its own engine exec (no kept connection) 13 ms
The first command in a sandbox with the kept connection 17 ms
Each subsequent command with the kept connection 4 ms

The design has a cost:

  • The first command is slower, because it opens the connection and waits for the hello.
  • While the connection is open, the helper holds about 15 MB of the memory of the sandbox. README.md gives this number, also from the test machine.
  • A process that the daemon depends on now stays in the sandbox between commands. The code in the sandbox can stop it or replace it. The section “Defenses” lists what the daemon does about that.

Manager.Run calls runKept first. runKept returns handled false when the command must go on its own connection.

Case Path
Manager.Run with a valid spec and input of a maximum of 1 MiB The kept connection
Manager.Run with input larger than keptStdin (1 MiB) Its own engine exec
Manager.Run with a spec that is not valid Its own path, where Exec refuses it with the reason
Manager.Run when the daemon runs with --connection-idle 0 Its own engine exec
Manager.Run in a sandbox with a helper of an earlier build (noConnIn) Its own engine exec
Manager.Run when the connection does not open Its own engine exec, for this command only
Manager.Exec, the streamed command Always its own engine exec
File operations (fs mode) Always their own engine exec
The questions of the daemon to the helper (version, procs, gpus, dns, own) Always their own engine exec

Run is the path of POST /v1/sandboxes/{id}/exec without stream, and of the tool sandbox_exec.

The limit on the input has a reason. The helper has one loop that reads all frames of the connection. It must never wait for one command to read its input. Thus the helper puts input in a queue and does not wait for room. The daemon sends only as much input as the queue can hold.

Part Where What it is
cmdConn kept.go One open connection: the engine stream, the writer, the commands in progress and the state.
keptRun kept.go One command in progress on a connection: two capped buffers, the exit, and the channels heard and done.
record.conn manager.go The connection of the sandbox, or nil.
record.connGate manager.go A channel with one place. A request holds the place while it opens the connection.
record.noConnIn manager.go The container whose helper cannot serve, or empty.
record.openFailed manager.go The time of the last failed open, or zero.
record.frozen, record.thawedAt manager.go The count of freezes in progress, and the end of the last freeze. See “Freezes”.
Manager.helloWait, Manager.stall, Manager.openRetry manager.go The three time limits. The tests make them short.
runServe internal/guest/run.go The loop of the helper in serve mode.
guest.MuxWriter, guest.ReadMux internal/guest/proto.go The writer and the reader of frames that have a command number.

None of these record fields is in the state file. A restart of the daemon starts with no connections.

Manager.keptFor returns the connection of a sandbox. It opens one when the sandbox has none that works.

  1. keptFor finds the record and puts a value in rec.connGate. One request opens the connection, and the other requests wait.
  2. If the context of the request ends during the wait, keptFor returns nil. runKept then returns the context error.
  3. Under m.mu, if rec.frozen is more than zero, keptFor returns nil. See “Freezes”.
  4. If the connection is for a different container, keptFor clears rec.conn and closes the connection.
  5. If the connection is not usable, keptFor clears rec.conn. Such a connection ended, or it has the retired mark.
  6. If rec.noConnIn is the current container, keptFor returns nil.
  7. If the last open failed less than keptRetry ago, keptFor returns nil.
  8. If there is no connection now, keptFor calls openKept.
  9. After openKept, it does a check under m.mu: same record, not dying, same container.
  10. If the check fails, it closes the new connection and returns nil.
  11. It clears rec.openFailed and stores the connection in rec.conn.
  12. It takes its value out of rec.connGate.

openKept does these steps:

  1. It tells the engine to start /.boxfork/guest serve in the container.
  2. It reads the first frame in a goroutine.
  3. It waits for the first of three events. The events are the first frame, the end of the request, and keptHello (15 seconds).
  4. It accepts only a FrameHello frame whose payload is valid JSON for guest.Hello.
  5. It makes the cmdConn and starts the reader goroutine cmdConn.read.

openKept gives the context of the request to the engine. The contract of engine.Engine.Exec says that this context controls only the start of the process. The stream stays open after the context ends. Thus a request that stops also stops a start that the engine does not complete. The connection stays open after the request that opened it.

The helper in serve mode first writes one FrameHello frame with command number 0. The payload is {"version":"2"} (guest.Version). The daemon does not compare the version. It only makes sure that the payload is JSON.

A sandbox that a client created before an upgrade of the daemon keeps the helper of the earlier build. That helper does not know serve. It writes guest: unknown command "serve" to its standard error and stops.

openKept reports old when the stream ends cleanly and the standard error contains the text unknown command. keptFor then puts the container id in rec.noConnIn and writes one line to the log. The daemon does not ask that container again. Each of its commands uses its own engine exec, and the first command costs one engine exec more.

A wake puts the sandbox in a new container with the helper of the current build. The id of the new container is not in rec.noConnIn, thus the daemon asks the new helper.

Each other failure of openKept is not permanent: no answer, a wrong first frame, a different message, or an engine error. keptFor puts the time in rec.openFailed. The command goes on its own engine exec. For keptRetry (30 seconds), each command of the sandbox uses its own engine exec without a wait. Then the daemon asks the helper again.

This rule limits the cost of a helper that never sends the hello. Only one command waits keptHello. The commands at the same time and after it do not wait.

A request that stops while openKept waits is not a failure of the helper. keptFor does not set rec.openFailed for it.

A command can have no cwd. The helper in run mode then runs it in the working directory of the image, because the engine starts the helper there. The helper in serve mode keeps the path of its own start directory. It gives that path to each command that has no cwd. Thus a command finds the directory by its path each time, as a new engine exec does. This is important when a command removes the directory and makes it again, for example with git clone.

sequenceDiagram
participant C as Client
participant M as Manager.runKept
participant K as cmdConn
participant H as Helper (serve mode)
participant P as Command
C->>M: Run(spec, stdin, limit)
M->>M: use(id): inUse + 1
M->>K: keptFor: get or open the connection
K->>K: take the next free number n, add runs[n]
K->>H: FrameSpec, number n
K->>H: FrameStdin and FrameStdinEOF, number n (if there is input)
H->>P: start in a new session
H-->>K: FrameStarted, number n
Note over K: read closes heard for runs[n]
P-->>H: output
H-->>K: FrameStdout and FrameStderr, number n
Note over K: read adds to the capped buffers
P-->>H: exit
H-->>K: FrameExit, number n
Note over K: read removes runs[n] and closes done
K-->>M: Result, started = true
M->>M: done(): inUse - 1
M-->>C: Result

Each frame on the connection has a header of 9 bytes: the type, the command number and the length of the payload. cmdConn.run gives each command a number under k.mu:

  • It adds one to k.next until it finds a number that is not in k.runs and that is not zero.
  • Zero is the number of the helper itself. Only the hello frame uses it.
  • A number becomes free again when its command ends.

The helper keeps a map running from number to command. If a spec arrives with a number that is in the map, the helper ignores the spec. TestServeDoesNotStartASecondCommandUnderANumberInUse holds this.

One goroutine for each connection, cmdConn.read, reads all frames. For each frame it looks for k.runs[number] under k.mu:

  • If there is no such command, it drops the frame.
  • The first frame about a command closes the heard channel of the command.
  • FrameStdout and FrameStderr go into the capped buffers of the command. Each buffer keeps the first limit bytes.
  • FrameExit sets the exit, removes the command from k.runs and closes done.
  • It ignores each other frame type.

cmdConn.run waits in one select for five events:

Event What run does
done Returns the Result.
heard Stops the timer for the first answer. From now on the command has no time limit in the daemon.
The timer k.stall (10 seconds) with no frame about the command See “The retire behavior”.
The context of the request ends Removes the command, sends FrameClose in a goroutine and returns the context error.
The connection ends Returns the Result if done is also closed. If not, returns the error from cmdConn.lost.

The helper handles FrameClose with command.lose: it sends SIGKILL to the process group and sends no FrameExit for that command. The other commands on the connection continue.

stateDiagram-v2
[*] --> None
None --> Opening: a Run calls keptFor
Opening --> Open: hello in keptHello
Opening --> None: no hello, bad hello, engine error
Opening --> Without: unknown command "serve"
Without --> Without: each Run uses its own engine exec
Open --> Open: a command starts or ends
Open --> Retired: no frame about a command in keptStall
Open --> Ended: idle for --connection-idle
Open --> Ended: bad frame, end of stream, frame not taken in keptStall
Open --> Ended: kill, timeout of the sandbox, different container
Retired --> Ended: last command ends, or end of stream
Ended --> None: the sweep or the next Run clears rec.conn
Ended --> [*]

None, Opening and Without are states of the record (rec.conn is nil). Open, Retired and Ended are states of a cmdConn:

State In the code
Open ended is not closed and retired is false. usable returns true.
Retired retired is true. No new command goes down the connection. The commands in progress continue.
Ended ended is closed. why holds the cause. The engine stream is closed.

cmdConn.end is the one place that ends a connection, with the exception of closeIfIdle, which does the same steps under its own check. Both close the ended channel under k.mu and then close the stream outside the lock.

Manager.Sweep calls closeIdleConns at its start. Manager.Watch calls Sweep each second.

closeIdleConns makes a list of the records that have a connection. For each one:

  • If the connection is usable, it calls closeIfIdle with cfg.ConnectionIdle.
  • closeIfIdle closes the connection only when k.runs is empty and idleAt is older than the idle time.
  • If the connection is closed now, or was not usable, closeIdleConns sets rec.conn to nil.

idleAt is the time when the last command left the connection (forgetLocked), or the time when the connection opened. A command in progress keeps the connection open for as long as the command runs.

closeIfIdle and cmdConn.run both make their decision under k.mu. Thus a command is in k.runs before the check, or it does not start on that connection.

The flag --connection-idle of boxfork serve sets the time. The default is DefaultConnectionIdle, one minute. A value of zero or less means “no kept connection”. cmd/boxfork/main.go changes such a value to -1, because zero in Config means “use the default”.

The sweep does not look at the state of the sandbox. It also closes the idle connection of a paused sandbox.

The helper runs in the sandbox. The code in the sandbox can stop it, kill it or put a different program in its place. The daemon thus does not trust what arrives on the connection.

Threat Defense Where
A frame about a command number that is not in progress The reader drops the frame. cmdConn.read
More output than the limit Each capped buffer keeps limit bytes. The reader drops the remainder and sets truncated. capped.add
A frame with a length larger than guest.MaxFrame (4 MiB) guest.ReadMux returns an error before it gets memory for the payload. The connection ends. ReadMux, cmdConn.read
A frame that stops in the middle, or a stream that ends ReadMux returns an error. The connection ends. cmdConn.read
A FrameExit whose payload is not JSON The connection ends with “bad frame from the sandbox helper”. cmdConn.read
A frame of a type that the daemon does not know The reader ignores it. cmdConn.read
No hello, or a first frame that is not a hello openKept closes the stream after keptHello. The command uses its own engine exec. openKept
A helper that does not take a frame cmdConn.send starts a timer of k.stall with cmdConn.watch. When the timer fires outside a freeze, it ends the connection, and the write returns. cmdConn.send, cmdConn.watch
A helper that takes a spec and sends no frame about it The retire behavior. cmdConn.run

When a connection ends, each command in progress on it gets the error from cmdConn.lost, or ErrNotFound from cutOff.

The helper also has a defense for itself. feed in runServe puts input into the queue of a command without a wait. If the queue is full, the helper sends SIGKILL to that command. The queue holds 1024 frames. The daemon sends a maximum of 1 MiB of input in frames of a maximum of 256 KiB, thus the daemon never fills the queue.

A helper answers each spec immediately: with FrameStarted, or with FrameExit when it cannot start the command. A helper that sends nothing about a command for k.stall (keptStall, 10 seconds) is stopped or replaced.

The timer starts after the daemon sent all of the input of the command. The time that the helper needs to read a large input thus does not count.

When the timer fires, cmdConn.run does these steps:

  1. If the sandbox is frozen, or was frozen less than k.stall ago, it starts the timer again.
  2. Under k.mu, if done is closed now, it returns the result.
  3. Under k.mu, if heard is closed now, it continues to wait without the timer.
  4. It removes the command from k.runs.
  5. It sets retired on the connection. usable now returns false, and no new command goes down this connection.
  6. If no command remains in k.runs, it closes the connection in a goroutine.
  7. It sends FrameClose for the command in a goroutine. If the helper starts the command later, the helper stops it.
  8. It returns the error “the sandbox’s helper did not answer for the command”.

Steps 2 and 3 are necessary because the answer and the timer can arrive at the same time. The answer always wins.

The daemon does not end a retired connection that has commands in progress. A command that the helper already answered can run for a long time, and the daemon lets it end. forgetLocked closes a retired connection when the last command leaves k.runs.

The next command finds that the connection is not usable. keptFor clears rec.conn and opens a new connection with a new helper. The sweep also clears rec.conn for a retired connection and does not close it.

The cost for the client is one command that fails after 10 seconds. TestAHelperThatLeavesACommandUnansweredCostsThatCommandAndTheConnection and TestCommandsRunningOverAConnectionThatIsGivenUpAreLeftToEnd hold this behavior.

cmdConn.run returns a value started. started is false only when the helper did not get a full spec frame for the command.

runKept tries a maximum of two times:

  1. It calls keptFor. If the answer is nil and the context of the request ended, runKept returns the context error.
  2. If the answer is nil, runKept returns handled false, and Run uses its own engine exec.
  3. It calls k.run. If started is true, runKept returns the result or the error. It never sends the command again.
  4. If started is false and the context of the request ended, it returns the context error.
  5. If started is false, it starts again from step 1 with a new connection.

After two attempts with started false, runKept returns handled false, and Run uses its own engine exec.

Case started The daemon sends the command again
The connection ended or is retired before run took a number false Yes
The write of the spec frame returned an error false Yes
The helper sent no frame about the command in k.stall true Never
The connection ended after the write of the spec true Never
The request ended after the write of the spec true Never

The reason for “never” is that the command possibly ran. A second run can do the work of the command two times. The reason for “yes” is in the code: one frame is one write, and the helper acts only on full frames.

After a started error, runKept calls cutOff. If the sandbox is gone or dying, the client gets ErrNotFound.

Operation Effect on the connection Effect on a command in progress on it
Kill, or the timeout of the sandbox forget calls dropConn, which closes the connection in a goroutine. The engine also ends the stream when it removes the container. The client gets ErrNotFound (HTTP 404).
The container stops behind the daemon The stream ends, and read ends the connection. The client gets “the command’s connection to the sandbox was lost” (HTTP 500).
Pause Nothing. The helper is frozen with the other processes. use refuses each new command with ErrPaused. No frame goes to the helper. The stall timers do not fire. See “Freezes”.
Snapshot or fork Nothing. The engine freezes the container while it saves the files. A new command uses its own engine exec, which the engine refuses during the freeze. The stall timers do not fire.
Resume Nothing. The next command uses the same connection, if the sweep did not close it. None.
Suspend No code closes the connection. The engine ends the stream when the daemon removes the container. None: the daemon suspends a sandbox only when inUse is zero.
Wake The sandbox gets a new container. keptFor sees a different container, closes the old connection and opens a new one. None.
Restart of the daemon The connections are only in memory, thus they end with the process. The first command after the restart opens a new one. The client lost its request with the daemon.
Client stops the request Nothing. The daemon sends FrameClose. The helper kills that command only.

When the helper reads the end of its input, it calls lose for each command and stops. Thus no command of a connection continues after the connection ends.

The helper in serve mode does not prevent a suspend. countProcs in internal/guest/procs.go does not count a process whose command line is /.boxfork/guest serve. See suspend.md for the idle check.

runKept holds the use of the sandbox (use and done) for the full time of the command, as the other path does.

The engine freezes all processes of a container in two cases. The helper is frozen with them and cannot send or read a frame.

Case Where The state of the sandbox during the freeze
The commit for a snapshot or a fork. Docker freezes the container while it saves the files. Manager.Snapshot in snapshot.go running
A pause, from the call to the engine until the daemon marks the sandbox paused Manager.setPaused in manager.go running, then paused

The daemon adds one to rec.frozen before each of these engine calls. After the call it subtracts one and puts the time in rec.thawedAt. markResumed also puts the time in rec.thawedAt.

record.heldLocked reports true when rec.frozen is more than zero, the state is paused, or the last thaw was less than k.stall ago. Each cmdConn gets a function held that calls heldLocked under m.mu.

The two stall timers use held:

  • cmdConn.watch runs the timer of send. When the timer fires and held reports true, it starts the timer again.
  • The timer for the first answer in cmdConn.run does the same.

The thaw margin of k.stall gives a helper that was frozen the same time to answer as a helper that was not frozen.

A new command does not go down the connection while rec.frozen is more than zero. keptFor returns nil. The command then uses its own engine exec, and the engine refuses it, as before the kept connection. TestANewCommandWhileTheSandboxIsSnapshottedIsAnsweredAsBefore makes sure that the two paths give the same answer.

Three locks are in this code. Take them only in this order:

  1. rec.connGate
  2. m.mu
  3. k.mu

rec.connGate is a channel and not a mutex, because a request must stop its wait when its context ends.

Rule Where you can see it
keptFor takes rec.connGate first and then takes m.mu for short sections. keptFor
keptFor calls k.usable, which takes k.mu, while it holds m.mu. keptFor
No code takes m.mu or rec.connGate while it holds k.mu. read, run, forgetLocked
No code takes rec.connGate while it holds m.mu. keptFor, closeIdleConns, dropConn
No code closes the engine stream while it holds k.mu or m.mu. end, closeIfIdle, dropConn
The function held takes m.mu. Only timer goroutines and run call it, and they hold no lock at that time. watch, run

dropConn runs with m.mu held. It thus closes the connection in a new goroutine. forgetLocked and retireLocked run with k.mu held. They close a connection in a new goroutine.

keptFor holds rec.connGate while openKept waits for the engine and for the hello. It does not hold m.mu in that time. The other requests of the same sandbox wait for rec.connGate, each until its own context ends. The requests of other sandboxes do not wait.

The writer guest.MuxWriter has its own lock, which makes each frame one write. No code holds k.mu while it writes a frame.

Name Value Meaning
DefaultConnectionIdle 1 minute The idle time before the sweep closes a connection.
keptStdin 1 MiB The maximum input of a command on the kept connection.
keptHello 15 seconds The time that a helper has to send the hello.
keptStall 10 seconds The time that a helper has to take a frame, and to send the first frame about a command. Time in a freeze does not count.
keptRetry 30 seconds The time after a failed open in which each command uses its own engine exec.
guest.MaxFrame 4 MiB The maximum payload of one frame.
muxHeader 9 bytes The header of a frame in serve mode.
The queue for input in the helper 1024 frames stdinQ in internal/guest/run.go.
  • The daemon never sends a command a second time after the write of its spec frame was successful.
  • The daemon never reads an unbounded quantity of data on the word of the helper.
  • A helper that is stopped costs one sandbox one command and its connection. It does not stop the daemon or a different sandbox.
  • A frame that the daemon cannot read ends the connection. The daemon does not try to find the next frame.
  • A command that the helper answered is not stopped by the retire behavior or by the idle close.
  • No command starts on a connection after ended is closed or retired is set.
  • A connection belongs to one container. The daemon never uses a connection for a different container.
  • rec.conn is set only when the record is the current record, is not dying and has the same container.
  • The result of a command is the same on both paths.
  • The lock order stays rec.connGate, then m.mu, then k.mu.
  • Time in which the engine freezes the sandbox does not count against the helper.
  • A request does not wait for the connection longer than its own context lets it.
  • The helper loop in serve mode never waits for one command.
Failure Result
The engine cannot start the serve helper keptFor returns nil. The command uses its own engine exec, and that path reports the engine error.
The request ends while openKept waits for the hello openKept closes the stream and returns the context error. runKept returns the context error.
The engine does not answer when openKept starts the helper The engine call has the context of the request. The request ends when its context ends.
The helper never sends the hello The first command waits keptHello and then uses its own engine exec. For keptRetry, the other commands use their own engine exec without a wait.
The engine freezes the sandbox for longer than keptStall The stall timers start again. The commands in progress continue after the freeze.
The sandbox goes away while openKept waits The check in step 7 of keptFor fails. keptFor closes the new connection.
The code in the sandbox kills the helper The stream ends. Each command in progress gets the “lost” error. The next command opens a new connection.
The code in the sandbox stops the helper (SIGSTOP) The next command fails after 10 seconds. The command after that one opens a new connection.
The helper cannot start a command The helper sends FrameExit with exit_code 127 and error. The connection stays open.
A spec is not valid JSON for the helper The helper sends FrameExit with exit_code 127. The connection stays open.
  1. Read kept.go from the start to the end. It is one file of about 500 lines.
  2. Read runServe and start in internal/guest/run.go.
  3. If you add a frame type, add it to internal/guest/proto.go and to helper.md.
  4. Use a type number less than 0x10 for a frame from the daemon. Use 0x10 or more for a frame from the helper.
  5. Make sure that the two sides ignore a frame type that they do not know. Do not change that rule.
  6. If you add a wait in the daemon, give it a time limit and an exit on k.ended.
  7. If you add a field to cmdConn, write in its comment which lock guards it.
  8. If you add a path that returns from cmdConn.run, decide the value of started from the retry rule.
  9. If you add a path that ends a connection, call cmdConn.end. Do not close the stream directly.
  10. Add a test to kept_test.go that uses newStandIn to play a helper that does not obey the protocol.
  11. Do a break-it check. Remove your defense in a copy of the code, and make sure that the new test fails.
  12. If you changed the helper, add a test to internal/guest/serve_test.go.
  13. Run make test.
  14. Run scripts/proof/conn.sh on a machine with a real engine, if you have one.

Caution: Do not remove the noConnIn path while sandboxes of an earlier build can exist. Each command in such a sandbox then costs one failed engine exec more.

internal/sandbox/kept_test.go has the tests of the daemon side. The type standIn plays a helper that the test controls.

Test What it holds
TestCommandsAfterTheFirstStartNoProcessInTheEngine 30 commands after the first one cause no engine exec.
TestACommandIsAnsweredTheSameEitherWay The same Result on both paths.
TestCommandsAtOnceShareOneConnectionAndStayApart 24 commands at the same time open one connection and get their own output.
TestAConnectionNobodyUsesIsClosed, TestAConnectionWithACommandRunningIsNotClosedHoweverLongItTakes The idle close.
TestACommandWhoseCallerGivesUpIsStoppedAndTheConnectionGoesOn A canceled request stops one command only.
TestASandboxOfAnEarlierBuildRunsCommandsTheOldWay The noConnIn path: one question, then the old path.
TestASandboxOfAnEarlierBuildKeepsAConnectionOnceItComesBack After a wake, the daemon asks the new helper.
TestAHelperThatCannotBeAskedNowIsAskedAgainLater A failure to open is not permanent, and the daemon asks again after openRetry.
TestAHelperThatDoesNotSayHelloIsNotWaitedForByEveryCommand Eight commands at the same time cost one wait for the hello.
TestACommandWaitingForTheConnectionGivesUpWithItsCaller, TestAnEngineThatHangsWhileTheConnectionIsOpenedDoesNotHoldTheCaller The context of the request ends the wait for the gate and the engine call. The engine starts no process for a request that stopped.
TestACommandUnderWayWhileTheSandboxIsFrozenIsNotGivenUp A snapshot and a pause longer than stall do not retire the connection, with and without input in progress.
TestAHelperThatSaysNothingAfterAFreezeIsStillGivenUp After the freeze and the thaw margin, the retire behavior applies again.
TestAnAnswerThatComesWithTheEndOfTheWaitIsKept An answer that arrives with the end of the timer wins. The test holds the daemon at that moment through held.
TestANewCommandWhileTheSandboxIsSnapshottedIsAnsweredAsBefore During a commit, the two paths give the same answer, and no command runs.
TestStdinThatTakesLongToGoIsNotTakenForAHelperThatDoesNotAnswer The timer for the first answer starts after the input is sent. The helper answers at once, after its input, or before it reads its input.
TestAConnectionGivenUpWithNothingOverItIsClosed A retired connection with no commands closes immediately.
TestAHelperThatStopsTakingInputCostsTheSandboxItsConnectionOnly The timer in send, and the retry of a command that the helper did not take.
TestWhatTheHelperSaysIsNotTakenOnTrust Bad frames, frames about no command, output larger than the limit.
TestAHelperThatLeavesACommandUnansweredCostsThatCommandAndTheConnection The retire behavior.
TestCommandsRunningOverAConnectionThatIsGivenUpAreLeftToEnd A retired connection lets its commands end.
TestAPausedSandboxKeepsItsConnection, TestASuspendedSandboxComesBackWithANewConnection, TestAConnectionToAnotherContainerIsNotUsed Pause, suspend, wake.
TestWithNoConnectionKeptEveryCommandGetsItsOwn, TestTheDefaultIdleTimeAndWhatDoesNotGoDownTheConnection The settings, the limit on input, the streamed command.

internal/guest/serve_test.go has the tests of the helper side. The most important ones are TestServeFindsItsDirectoryByPathForEachCommand, TestServeKeepsCommandsApart, TestServeStopsACommandThatIsClosedAndSaysNoMoreAboutIt, TestServeLosingTheConnectionStopsEverything, TestServeStopsACommandSentMoreStdinThanItHolds and TestMuxFrameRejectsTruncatedAndOversized.

The proof script scripts/proof/conn.sh does the checks on a real engine and measures the times. It covers these cases: a killed helper, a stopped helper, pause, suspend, a restart of the daemon, --connection-idle 3s and --connection-idle 0.

  • The numbers on this page come from one test machine: Docker with gVisor on arm64, in a nested virtual machine. Nothing ran on x86_64 or on a machine with an NVIDIA GPU.
  • The helper holds memory of the sandbox while the connection is open. The memory limit of the sandbox includes it.
  • All commands of one sandbox share one stream and one reader. A large frame of one command delays the frames of the other commands.
  • After the first frame about a command, the daemon has no timer for that command. If the code in the sandbox stops the helper then, the client waits until it stops the request or the sandbox goes away.
  • The helper applies timeout_ms. A stopped helper does not apply it.
  • A command that fails after the write of its spec is not sent again. The client must decide if it is safe to run the command again.
  • The daemon does not compare the version in the hello.
  • The user and the groups of a command come from the helper. The engine finds them when it starts the helper, at the open of the connection. A change to /etc/group after the open thus does not change the groups of the commands on that connection. A new engine exec sees the change. Use --connection-idle 0 if your commands depend on such a change.
  • A sandbox that gets no CPU time for keptStall, for example at its CPU limit, can cause one retire error. No test measured this case.
  • The idle check does not count a process whose command line is /.boxfork/guest serve. Code in the sandbox can use that name to hide a process. The result is only that its own sandbox is suspended.
  • The freeze rules come from the test engine. conn.sh did not yet do a snapshot while commands ran on a real engine.
  • The retry rule depends on the transport: a write that returns an error must mean that the helper did not get the full frame. The tests use pipes in memory for this. conn.sh did the check of a stopped helper on Docker.
  • The connection has no limit on the number of commands at the same time.