Repository navigation
Description
waitForUnixSocket retries on a fixed 20 ms sleep, which makes every Firecracker restore
about 10x slower than the work it does. Measured in #303: of a 27,706 us restore,
24,956 us (90.1%) is this sleep and 2,748 us is real work.
The code
remote-worker/internal/vmpool/launcher_firecracker.go:
func waitForUnixSocket(ctx context.Context, path string, timeout time.Duration) error {
deadline := time.Now().Add(timeout)
for {
if conn, err := net.DialTimeout("unix", path, 100*time.Millisecond); err == nil {
_ = conn.Close()
return nil
}
if time.Now().After(deadline) {
return fmt.Errorf("timed out after %s waiting for %s", timeout, path)
}
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(20 * time.Millisecond): // <-- every miss costs a full 20ms
}
}
}The first dial fires immediately and fails, because the jailer has not yet chrooted, dropped
privileges and exec'd Firecracker, and Firecracker has not yet bound its API socket — which
takes on the order of 1-3 ms. The loop then sleeps a full 20 ms before looking again.
Evidence it is the 20 ms and not the socket
The measured distribution over 3249 restores is quantized with nothing in between and, decisively,
nothing below 20 ms — a socket that genuinely took 20 ms would produce a spread, not steps:
| sockwait | restores |
|---|---|
| ~20-22 ms | 2,535 (78%) |
| ~40-42 ms | 714 (22%) |
| < 20 ms | 0 |
At c=16 a third step appears at ~60 ms (28 restores) and the ~40 ms step grows to 26%, i.e. under
more load Firecracker occasionally needs a second or third quantum — but the step structure is
unchanged, so the sleep sets the floor in every case.
Suggested direction
Poll far more finely, or not at all:
- Fine/exponential backoff — start at ~250-500 us and grow (e.g. x1.5, capped at a few ms).
Cheap, local, no new dependency. Should land restores at ~3-5 ms. - Watch instead of poll —
inotifyon the jail'srun/directory for the socket's creation,
then dial. Removes the guess entirely, at the cost of a Linux-specific path (the launcher is
already Linux-only in practice, seefcPlatformSupported).
Either way keep the existing 5 s overall timeout and the ctx cancellation, and keep dialing
rather than stat-ing: the current comment is right that the socket file appears before
Firecracker is accept()ing, and that is load-bearing for the id-collision guard
(fcJailOccupied) too.
Expected impact — real but not dramatic, stated honestly
Restore ~28 ms -> ~3-5 ms. Because a cold acquire is only part of an Exec, the throughput effect
at the measured operating points is roughly:
- c=8 (
coldAcquireRate0.76): per-Exec 118 ms -> ~100 ms, about +18% - c>=16 (
coldAcquireRate~1.0): per-Exec 254 ms -> ~231 ms, about +10%
It also improves cold-acquire latency for every user, which matters independently of
aggregate throughput, and it lowers p95 (measured 139-155 ms at these rungs).
It is not the scale-out answer — #259 measured 1.82x from a second worker process. This is a
cheap, safe, independent win.
Doing it under TDD
A test can pin this without hardware: drive waitForUnixSocket against a socket that is created
after a known short delay (say 2 ms) on a temp path, and assert it returns in well under one
current quantum. That test fails today (it would take >=20 ms) and passes with a finer poll, which
is exactly the failing-test-first shape. Add a second case asserting the 5 s timeout and ctx
cancellation still hold, so the fix cannot regress the refusal paths.
Note SH_DIAG_PPROF-style phase timing is what made this visible; #303 has the method.
Activity
pdettori commentedon Sep 21, 2026
Still worth doing, but the "Expected impact" section above is wrong and should not be used to
justify or size it. The correction matters because it changes why you would do this.
What was wrong
The estimate ("+18% at c=8, +10% at c>=16") was derived from the driver's
coldAcquireRateof0.76 and ~1.0 — i.e. from the assumption that most Execs pay a restore. That figure is a latency
proxy and it is wrong by 13x: the true cold rate, read from
vmpool.Phases.Coldon the same runs,is 0.056 at c=8 and 0.0039 at c=16 (#306).
So at the measured operating points a restore is almost never on the Exec critical path:
Acquireis 961 us at c=8 and 158 us at c=16, against a ~62 ms Exec. Removing 25 ms from arestore that >99% of Execs never wait for buys ~0% throughput there. The claimed +18% / +10% is
not real.
Why it is still worth fixing
Three reasons, none of which depend on the bad estimate:
WORKER_MAX_CONCURRENT=16(session: DefaultConcurrency=4 is the per-process throughput cap — raising it beats adding workers (2.74x) #305, which took one worker to 172.88 Exec/s)Acquirerose from158 us to 13,338 us — because 16 concurrent Execs drain standbys faster than
D=2per poolreplenishes. At that operating point the restore is on the hot path, and 90% of it is this
sleep. The fix becomes a throughput fix precisely where throughput is going.
first-execwas a recorded cold cause on every run. Asession's very first command pays a full restore, and today that is ~28 ms of which ~25 ms is a
sleep. That is a cold-start cost a user feels directly.
ReplenishDelay + restoreper slot; cutting restorefrom ~28 ms to ~3-5 ms materially raises how fast a pool recovers from a burst, which is what
keeps
Acquirenear zero in the first place.Restated expectation
MaxConcurrent=4): negligible throughput change; a real improvementto first-Exec latency and to burst recovery.
Acquireis ~13 ms of an~89 ms Exec there and ~90% of that is the sleep.
The TDD shape suggested above is unaffected: drive
waitForUnixSocketagainst a socket createdafter a known short delay and assert it returns in well under one 20 ms quantum, plus cases
holding the 5 s timeout and
ctxcancellation.Refs #305, rossoctl/moca-experiments#8, #303
feat(vmpool): surface Exec/restore phases and add opt-in pprof
pdettori commentedon Sep 21, 2026
Measured: this is the binding constraint on throughput, not a ~0 latency nicety
This issue's own correcting comment says the throughput effect "at the current default is ~0"
because >99% of acquires are warm, and the campaign plan sequences it last for that reason. A slot
sweep on
srv-r16b14s16(full data on #305) says that holds at 4 slots and nowhere above it.Restore sub-phases, microVM arm, D=2, 256 MiB guests,
SH_DIAG_PHASES=1, one run per point withslots == c, 400 iters/slot,mainate170ae0:(ms, means.) Every other sub-phase is flat across a 16x range of concurrency. Real work in a
restore is 2.80 ms.
The 20 ms poll is a hard floor, not a tail effect
Zero of 1,625 restores finished inside one quantum, over ~2.8 ms of real work. The 78/22 split
across the first two quanta reproduces the distribution recorded on this issue earlier.
Why it is now a throughput issue and not a latency one
The sweep found throughput peaks at 16 slots / 170.92 Exec/s and then regresses — 120.49 at
32 slots, 59.69 at 64, with the host never above 18.1% CPU. What binds is the rate at which
the pool turns out a fresh warm standby. The cold-acquire cause is overwhelmingly
exhausted(replenishment behind demand): 19,577 of 25,856 Execs at 64 slots.
Acquiretherefore grows4.18 -> 940.18 ms while
ResumeandRunstay flat.Replenishment is the restore path, and 90% of a restore is this sleep. So the sleep sets the
ceiling.
It also fails outright above the knee
At 64 slots, 396 replenishments hit this function's 5 s overall timeout and failed:
Those surfaced as 23
spawn-failureExecs. So Step 4's "the 5 s overall timeout and ctxcancellation still hold" is not only a regression guard — that timeout is being hit in practice,
and a fix needs to say whether 5 s is still the right ceiling once the poll is fine-grained.
One thing the data cannot settle
At 4 slots the socket clearly appears well inside 20 ms and the caller sleeps a full quantum, so
the win there is unambiguous: ~27.7 ms -> ~5 ms per restore, ~5x on replenishment rate. At 64 slots
sockwait is not quantized (p50 1540 ms), so the socket really is slow under load. Whether that
is caused by the slow cycle — slow restores mean more restores in flight, which means more
contention, a feedback loop — or by an independent bottleneck cannot be told apart from this data.
So this issue is both the fix and the experiment. Re-running the same five-point sweep after it
distinguishes the two: if the knee moves past 16, the feedback loop was real; if each restore gets
cheap but the knee stays, something else binds and that becomes the next question.
Suggested sequencing change: promote ahead of #258. #258's
Destroyis still 52.3% of an Exec atthe knee and worth ~1.3x on per-Exec latency, but it is not what stops throughput scaling.
pdettori commentedon Sep 21, 2026
Note on my comment above: the clean-box numbers stand, the "above the knee" framing does not
My previous comment argued two things. The first survives; the second was confounded and I am
withdrawing it. Detail in #305.
Stands — the clean-box restore decomposition. That came from the first run of the sweep, on
a box with zero accumulated state:
sockwait24.92 ms = 89.9%, over 2.80 ms of real work;21.8% in [40,60)).
That is a quantization proof of the fixed sleep, on an idle box, and no confound touches it. The
expected win — a restore from ~27.7 ms to ~5 ms, so ~5x on replenishment rate — is unchanged.
Withdrawn — the escalating
sockwaitcolumn and the "binding constraint above the knee"argument. Those rows (54 ms at 16 slots, 332 at 32, 1722 at 64) are contaminated: every Exec
leaks a per-VM cgroup directory (#255), the sweep ran in ascending order without removing them,
so stale-cgroup count grew in lockstep with slot count. A same-configuration control with 41,000
stale directories ran at 25.84 Exec/s with
sockwait1008 ms; with the directories removed andnothing else changed, 136.51 Exec/s and 98 ms. So most of that column is cgroup-creation cost,
not concurrency.
The 396 restore timeouts at 64 slots are likewise attributable to that, not to load as such —
though it does make the point that this function's 5 s ceiling is reachable in practice, so a
fix should still say whether 5 s remains right once the poll is fine-grained.
Sequencing. I previously argued this should jump ahead of #258. I still think it should, but
the reason is now the clean-box decomposition (90% of a restore is a sleep over 2.8 ms of work)
rather than any claim about where throughput stops scaling. And #255 should go first — while
that leak is present, no throughput measurement on a long-lived worker means anything.
fix(vmpool): rmdir the per-VM cgroup on the normal destroy path (#255)
perf(vmpool): poll finely for the API socket instead of a fixed 20ms …
Merge pull request #315 from pdettori/fix/304-fine-grained-socket-wait
feat(vmpool): time Destroy's sub-phases behind SH_DIAG_PHASES (#258)
docs: publish the Exec/s campaign results and preserve #303's dead ends
feat(vmpool): split the pre-socket window at the jailed execve (rosso…
feat(vmpool): observe when a killed VMM stops being able to touch the…
feat(vmpool): observe when a killed VMM stops being able to touch the…
feat(vmpool): observe when a killed VMM stops being able to touch the…
feat(vmpool): observe when a killed VMM stops being able to touch the…