sandkiln-protocol defines what goes on the wire, sandkiln-guest-agent answers inside the VM, and vsock_client.rs in sandkiln-vmm is the host side. This post covers all three, and the rules that keep them from hanging. For the prose introduction to vsock itself, see the docs page vsock and the host-guest wire protocol; this post assumes you have read the system overview.
Crate responsibilities
The protocol crate is the only thing the host and guest binaries share. It depends on
serde and serde_json and nothing else, and it has no idea what a vsock is. That is intentional: both binaries link the same message definitions, so the two sides cannot drift, and the crate stays small enough to read as a contract.
Framing
Every message is a 4-byte little-endian length followed by that many bytes of JSON:core/crates/protocol/src/framing.rs:6-25.) read_message does read_exact for the four length bytes, rejects anything over 64 MiB as InvalidData before allocating, then read_exacts the body.
Two alternatives were rejected. Newline-delimited JSON would let command output containing newlines or arbitrary bytes be misread as a frame boundary, and process output is not guaranteed to be valid UTF-8 at all. A binary schema format would need a schema compiler and could not be read in a packet capture; JSON is boring, self-describing, and sufficient for a protocol with a handful of message shapes. The length cap exists because a corrupt or hostile prefix would otherwise cause a multi-gigabyte allocation on a 700 KB static binary. There is a test that sends MAX_MESSAGE_LEN + 1 and expects the error, and another that round-trips a payload containing \n, a NUL and high bytes.
Bytes inside a JSON string need encoding, so file contents and process output cross the wire as base64 (content_base64, data_base64). That costs roughly a third more bytes.
Messages
The request and response enums are internally tagged, with different tag names so a captured frame reveals its direction at a glance:core/crates/protocol/src/messages.rs:4-6, abridged.) Response uses tag = "status" (messages.rs:160-162) with variants Exec, File, Dir, Link, Ok and Error. The wire shape is pinned by tests that compare exact JSON, for example {"cmd":"exec","command":"echo","args":["hi"],"env":{}} (messages.rs:199). A round-trip test alone would not catch a renamed tag, since serde would happily round-trip the new shape through itself.
The environment in an Exec is already fully resolved by the daemon, create-time defaults merged with any per-call override. The agent adds it to the process’s inherited environment and has no concept of “sandbox-level” versus “call-level”. That keeps policy on the host and the agent a dumb executor.
The PtyHandshake (cols, rows), ExecStreamHandshake and TunnelOpen types are deliberately not Request variants. They open different connection shapes, described next. ExecStreamEvent is tagged stream and has Stdout, Stderr and Exit { exit_code } (messages.rs:138-143).
There is no version negotiation. The guest agent and the host crates are rebuilt and redeployed together, and the compiler catches a missing match arm on either side if both are rebuilt.
Four ports, four connection shapes
The agent listens on three ports and dials out on a fourth.
(
core/crates/protocol/src/lib.rs:14,22,30,55.) Separate ports instead of new Request variants are the central design choice. A PTY is a long-lived bidirectional byte stream and a streamed exec is a long-lived stream of structured events; neither fits request/response. Giving each shape its own port means the listener for each can use the simplest possible loop, and no connection ever has to switch protocols halfway through based on a message it must first parse.
The agent’s own main shows the consequence. The AGENT_PORT loop handles each connection inline on one thread; the PTY and exec-stream listeners spawn a thread per accepted connection, because a session blocks for as long as its shell or command lives (core/crates/guest-agent/src/main.rs:19-30,40-42,56-58). The inline loop is why a slow Exec on port 5000 delays every other port-5000 request to that guest, including the daemon’s own control-plane calls (health checks, sync, tunnel setup), which are bounded at 3 seconds and would time out behind it. A command that must run for a long time, or that must not block anything else, belongs on the streaming port.
Concurrent-session caps live on the host, in the daemon, not in the agent. The daemon already manages each session’s WebSocket lifecycle and can count sessions with no cross-connection state needed in the guest.
Request/response on port 5000
The host opens a fresh Unix socket connection per call, performs Firecracker’sCONNECT <port>\n handshake, writes one frame and reads one frame.
The host side is short enough to quote:
core/crates/vmm/src/vsock_client.rs:51-65, abridged.) connect_handshake writes CONNECT {port} and requires the reply to start with OK (vsock_client.rs:115); anything else becomes ConnectionRefused, which is an unsent failure and therefore safe to retry, as described below.
On the guest, handle_connection loops reading frames until the peer disconnects. A frame that fails to decode produces Response::Error; a failure to serialise a response, or a failed write, ends the connection (guest-agent/src/main.rs:64-82). There is no error recovery inside a connection by design: after a bad frame the stream position is unknowable, so resynchronising would be guessing.
Each handler in handler.rs is a thin wrapper over std::process::Command or std::fs, with one raw libc::chown because std has no equivalent. Exec uses Command::output(), converting stdout and stderr with from_utf8_lossy and mapping a signal-killed process to exit code -1 (handler.rs:31-40). The handlers do no path validation. The guest agent is the wrong layer for it: a path only ever names something inside that one VM’s filesystem.
Two bounds, and knowing whether the request was sent
core/crates/vmm/src/vsock_client.rs:22, core/crates/vmm/src/vm/mod.rs:105.) Without a bound, a blocking read on a vsock stream has no upper limit. The failure that motivated one was concrete: when a VM’s vCPUs are paused for a snapshot, Firecracker’s host Unix socket still accepts a connection, but the guest cannot reply, so a read waits forever. The doc comment on IO_TIMEOUT records this (“a paused VM, vCPUs halted, is the case that surfaced this”).
The first version of the fix was one constant applied to everything, and that was wrong in a way that took a live audit to find. A request/response call has two very different kinds of waiting. Connecting, handshaking and writing the request should always be quick. Waiting for the reply lasts as long as the work does: a control-plane request (a sync, a health check, tunnel setup) is answered in milliseconds or never, but a user command is legitimately silent for as long as it runs. Applying the 3 second bound to both meant any command that stayed quiet for more than 3 seconds failed after roughly 6.5 seconds and, worse, ran twice, because the retry loop could not tell that the first attempt had already been accepted. The design that replaced it separates the two concerns.
CallError says whether the guest could have seen the request
vsock_client::call takes a response_timeout: Option<Duration> and returns Result<Response, CallError>:
vsock_client.rs:30-34.) Every failure up to and including the request write is tagged request_sent: false; everything after the frame is written (the response read, the decode) is request_sent: true. A write that fails part-way still counts as unsent, because the agent discards an incomplete frame without executing it. Connect, handshake and write always use IO_TIMEOUT; only the reply wait uses response_timeout, where None waits indefinitely.
retry_while never re-sends an accepted request
The retry exists to absorb the agent’s startup race right afterInstanceStart: the first connect can be refused for a few milliseconds. retry_while (vm/mod.rs:321) is the loop for it, with a 5 second deadline and 1 ms to 20 ms exponential backoff:
Exec is an arbitrary command, so the retry decision has to come from the one layer that knows how far the request got. The PTY and exec-stream openers differ: they carry only a handshake before anything runs, and still use the plain retry_with_backoff.
Vm::call, Vm::call_with_timeout and AgentEndpoint
Three entry points sit abovevsock_client::call, all in core/crates/vmm/src/vm/mod.rs:
AgentEndpoint is an owned, cloneable handle (vm_id and the vsock socket path) obtained from Vm::agent_endpoint(). It exists for a lock reason, not a vsock one. The daemon keeps its live sandboxes in one Mutex<HashMap>, and the old call_agent held that lock across the whole vsock call, so a single slow command froze every other sandbox operation, even GET /sandboxes. Now call_agent (core/crates/daemon/src/http/routes/exec.rs:122) copies the endpoint out, touches the activity timestamp, drops the lock, and only then calls. If the VM is stopped in the meantime the call simply fails, since the endpoint holds no reference to the Vm.
The two bounds at the HTTP boundary
The daemon passesSome(exec_timeout) for exec, file and filesystem requests, where exec_timeout_secs is a config setting with a default of 600 (core/crates/daemon/src/config/settings.rs:43, read at exec.rs:132). The 3 second control-plane bound is a different, deliberately unconfigurable number for the vmm’s own housekeeping. When the longer bound expires, agent_call_error (exec.rs:146) maps a WouldBlock or TimedOut error to HTTP 504 rather than 500: the sandbox is fine, the command has just not finished. The message says the command may still be running in the guest, because nothing kills it. Commands that need to outlive the bound, or stream partial output, use the streamed exec port.
The 3 second values remain constants because no caller has a different correct answer. Too short and a healthy but momentarily busy guest (one just resumed from a snapshot, say) fails spuriously; too long and every probe of a dead guest is slow. The repo records that a failed post-resume health check used to pay two 5 s retry timeouts back to back (about 10.3 s) before Vm::force_stop skipped the sync (about 5.4 s). The same insight fixed snapshots: snapshot_and_stop used Vm::stop, which first tries a guest sync that a paused VM can never answer, so every snapshot paid about 6 seconds waiting for it; it now calls force_stop (core/crates/daemon/src/http/routes/snapshot.rs:188), and an end-to-end snapshot dropped from 6.8 s to 0.56 s. The regression test for all three behaviours (a 4 second silent command succeeds and runs once, another request is not queued behind it, snapshot latency stays low) is scripts/integration-tests/25-long-running-commands.sh.
Long-lived sessions: PTY and streamed exec
open_pty and open_exec_stream perform the same connect-and-handshake sequence as call, with the timeouts applied only to the handshake, and then explicitly cleared:
vsock_client.rs:80-86.) A shell that sits idle between keystrokes is not a hung connection, so a session cannot reuse the request/response bound. The bound still protects the part that can hang before the session exists.
PTY
After the framedPtyHandshake, the agent calls forkpty with the requested window size, execs /bin/bash (or /bin/sh if bash is absent, since a registered custom image may not have it), and shovels bytes between the vsock stream and the pty master on two threads (pty.rs:38-60,76-81). From the handshake onward there is no framing: the daemon’s WebSocket handler proxies raw bytes. See the PTY internals page.
Streamed exec
After the framedExecStreamHandshake, the agent spawns the command with stdout and stderr piped and stdin null, and stays framed. Its structure is the interesting part:
Both pipes are drained on their own threads, because either can fill and block the child if read sequentially. Both feed one channel, and only the connection thread ever writes to the stream, so two messages’ framing can never interleave and no writer lock is needed. The child is waited on only after both pipes reach EOF:
exec_stream.rs:99-106.) Waiting first risks a deadlock on a full pipe buffer. Exactly one Exit is sent, and a failed spawn is reported in-band as a Stderr event followed by Exit { exit_code: -1 } (exec_stream.rs:49-54) so the caller sees one uniform shape for every outcome. This module never signals the child; if the host side disconnects, the pump loop stops sending and the threads end when the child’s pipes close. See exec-stream internals for how the daemon buffers these events.
The shared-socket hangup rule
The PTY code carries a design rule that applies to every bidirectional relay in the agent. To pump two directions concurrently, each needs its own handle, and the code gets them withtry_clone(). But a clone is a duplicated file descriptor on the same socket, not a second connection. Dropping one handle does not close the socket while another descriptor references it. So when one direction finishes, the thread blocked in read() on the other is not woken.
The rule: when either direction ends, the finishing side must actively unblock the other; never rely on drop. shovel_bytes applies it in both directions:
pty.rs:126,142, two separate sites shown together.) If the shell exits first, the output thread sees EOF and calls shutdown(Both), which makes the blocked input read return immediately. If the host disconnects first, the input loop sees EOF and sends SIGHUP to the shell, as a real terminal would, so it does not run orphaned. Before this rule was applied the effect was a roughly ten-second hang on exit, and it was only visible in a live test: types and compilation do not show it.
The tunnel relay applies the same rule from the start, with no process to signal, just two sockets: each copy loop ends with shutdown(Shutdown::Both) on its own write side (guest-agent/src/tunnel.rs:105,118). stop() uses a related trick for the accept loop, since std has no way to cancel a blocking accept: it sets a flag, then connects to its own listener so the loop wakes, sees the flag, and discards that connection (tunnel.rs:53-61).
The direction rule and its one exception
Across ports 5000 to 5002 the rule is absolute: the host always connects, and the guest only listens. The host owns the VM’s lifetime, so it is the one that knows when a connection is meaningful, and a guest that dials out on its own would be a guest process choosing when to talk to the host. It also keeps the guest agent’s attack surface to “answer what is asked”. The tunnel feature is the one deliberate exception, and the reason is structural. The event that needs to cross is “something inside the guest just connected to a local port”, which the host cannot have initiated. So the guest dials out: Firecracker supports guest-initiated vsock connections, but with a different mechanism from the host-initiated one. There is noCONNECT <port> line. Instead Firecracker forwards the guest’s connect() to a Unix socket at the VM’s vsock path with _<port> appended, which the host must already have bound and be listening on:
core/crates/vmm/src/tunnel.rs:57-61.) listen removes any stale file, binds a UnixListener there, and accepts on a background thread for the VM’s whole life: one listener per VM, shared by every tunnel that VM ever opens (tunnel.rs:70-73). Each accepted connection reads one TunnelOpen, and is dispatched by tunnel_id to whoever called register() for it, over a std::sync::mpsc channel. The channel is std, not Tokio, because the crate has no async runtime and the daemon already bridges every vmm call through spawn_blocking; bridging this channel the same way keeps the boundary consistent. register must be called before telling the guest to start listening, since a connection with no registered tunnel is dropped with a warning. Vm::boot and Vm::resume call listen right after the VM exists, and a failure is logged as a warning rather than failing the boot, because a missing tunnel feature on one sandbox is better than destroying a boot that otherwise succeeded. stop calls cleanup, which removes the socket file.
The handshake on the guest-initiated connection is application-level, layered over Firecracker’s raw bridge. TunnelOpen carries a tunnel_id (which tunnel) and a conn_id (which of that tunnel’s concurrent connections, since the daemon multiplexes them over one WebSocket to the caller). The local tunnel page covers that multiplexing.
What this design costs
- Every
callopens a new connection and performs a handshake. This is simple and stateless, at the price of per-call overhead. The criterion benchmark for an exec round trip recorded 242 to 248 microseconds on the dev box (kernel 6.8, 2026-11-23; an earlier run recorded 220 to 250 microseconds; both directional, one shared box), which says the steady-state cost of the protocol itself is small; the first exec on a cold sandbox is far slower (the vmm notes record roughly 420 to 460 ms, against 4 to 18 ms on a resumed one) because it includes waiting for the guest to come up, not because of framing. - The single-threaded port-5000 loop serialises request/response calls into one guest. With long commands now allowed on that port, one slow
execalso delays the daemon’s own 3 second control-plane calls to that guest. Concurrency needs go to the other ports.