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.
Why it exists
Section titled “Why it exists”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.
Which commands use it
Section titled “Which commands use it”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.
The parts
Section titled “The parts”| 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.
How the daemon opens a connection
Section titled “How the daemon opens a connection”Manager.keptFor returns the connection of a sandbox. It opens one when the sandbox has none that works.
keptForfinds the record and puts a value inrec.connGate. One request opens the connection, and the other requests wait.- If the context of the request ends during the wait,
keptForreturns nil.runKeptthen returns the context error. - Under
m.mu, ifrec.frozenis more than zero,keptForreturns nil. See “Freezes”. - If the connection is for a different container,
keptForclearsrec.connand closes the connection. - If the connection is not
usable,keptForclearsrec.conn. Such a connection ended, or it has theretiredmark. - If
rec.noConnInis the current container,keptForreturns nil. - If the last open failed less than
keptRetryago,keptForreturns nil. - If there is no connection now,
keptForcallsopenKept. - After
openKept, it does a check underm.mu: same record, notdying, same container. - If the check fails, it closes the new connection and returns nil.
- It clears
rec.openFailedand stores the connection inrec.conn. - It takes its value out of
rec.connGate.
openKept does these steps:
- It tells the engine to start
/.boxfork/guest servein the container. - It reads the first frame in a goroutine.
- It waits for the first of three events. The events are the first frame, the end of the request, and
keptHello(15 seconds). - It accepts only a
FrameHelloframe whose payload is valid JSON forguest.Hello. - It makes the
cmdConnand starts the reader goroutinecmdConn.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 hello
Section titled “The hello”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 helper of an earlier build
Section titled “A helper of an earlier build”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.
Other failures to open
Section titled “Other failures to open”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.
The working directory of a command
Section titled “The working directory of a command”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.
How a command runs on the connection
Section titled “How a command runs on the connection”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: ResultThe command numbers
Section titled “The command numbers”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.nextuntil it finds a number that is not ink.runsand 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.
How the daemon matches the answers
Section titled “How the daemon matches the answers”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
heardchannel of the command. FrameStdoutandFrameStderrgo into thecappedbuffers of the command. Each buffer keeps the firstlimitbytes.FrameExitsets the exit, removes the command fromk.runsand closesdone.- 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.
The states of a connection
Section titled “The states of a connection”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.
The idle close
Section titled “The idle close”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 callscloseIfIdlewithcfg.ConnectionIdle. closeIfIdlecloses the connection only whenk.runsis empty andidleAtis older than the idle time.- If the connection is closed now, or was not
usable,closeIdleConnssetsrec.connto 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.
Defenses against what runs in the sandbox
Section titled “Defenses against what runs in the 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.
The retire behavior
Section titled “The retire behavior”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:
- If the sandbox is frozen, or was frozen less than
k.stallago, it starts the timer again. - Under
k.mu, ifdoneis closed now, it returns the result. - Under
k.mu, ifheardis closed now, it continues to wait without the timer. - It removes the command from
k.runs. - It sets
retiredon the connection.usablenow returns false, and no new command goes down this connection. - If no command remains in
k.runs, it closes the connection in a goroutine. - It sends
FrameClosefor the command in a goroutine. If the helper starts the command later, the helper stops it. - 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.
The retry rule
Section titled “The retry rule”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:
- It calls
keptFor. If the answer is nil and the context of the request ended,runKeptreturns the context error. - If the answer is nil,
runKeptreturnshandledfalse, andRunuses its own engine exec. - It calls
k.run. Ifstartedis true,runKeptreturns the result or the error. It never sends the command again. - If
startedis false and the context of the request ended, it returns the context error. - If
startedis 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.
What other operations do to a connection
Section titled “What other operations do to a connection”| 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.
Freezes
Section titled “Freezes”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.watchruns the timer ofsend. When the timer fires andheldreports true, it starts the timer again.- The timer for the first answer in
cmdConn.rundoes 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.
The lock order
Section titled “The lock order”Three locks are in this code. Take them only in this order:
rec.connGatem.muk.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.
Reference: constants
Section titled “Reference: constants”| 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. |
Invariants that a change must keep
Section titled “Invariants that a change must keep”- 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
endedis closed orretiredis set. - A connection belongs to one container. The daemon never uses a connection for a different container.
rec.connis set only when the record is the current record, is notdyingand has the same container.- The result of a command is the same on both paths.
- The lock order stays
rec.connGate, thenm.mu, thenk.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
servemode never waits for one command.
Failure cases
Section titled “Failure cases”| 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. |
How to change it
Section titled “How to change it”- Read
kept.gofrom the start to the end. It is one file of about 500 lines. - Read
runServeandstartininternal/guest/run.go. - If you add a frame type, add it to
internal/guest/proto.goand to helper.md. - Use a type number less than
0x10for a frame from the daemon. Use0x10or more for a frame from the helper. - Make sure that the two sides ignore a frame type that they do not know. Do not change that rule.
- If you add a wait in the daemon, give it a time limit and an exit on
k.ended. - If you add a field to
cmdConn, write in its comment which lock guards it. - If you add a path that returns from
cmdConn.run, decide the value ofstartedfrom the retry rule. - If you add a path that ends a connection, call
cmdConn.end. Do not close the stream directly. - Add a test to
kept_test.gothat usesnewStandInto play a helper that does not obey the protocol. - Do a break-it check. Remove your defense in a copy of the code, and make sure that the new test fails.
- If you changed the helper, add a test to
internal/guest/serve_test.go. - Run
make test. - Run
scripts/proof/conn.shon 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.
Where the tests are
Section titled “Where the tests are”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.
Limits
Section titled “Limits”- 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/groupafter the open thus does not change the groups of the commands on that connection. A new engine exec sees the change. Use--connection-idle 0if 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.shdid 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.shdid the check of a stopped helper on Docker. - The connection has no limit on the number of commands at the same time.