Repository navigation
pool: one rule tells a replaced record from a released one; a release follows the id through a resize; failover.go and claim.go split - #677
Merged
Conversation
…lease, env and tun answer 503 for a replaced record held.go holds the two answers to "is this record still the claimed one": holdsLocked for passes and bools, heldErrLocked for calls, which tells a replaced record (ErrReplaced) from a released one (ErrUnknownSandbox). Every inline compare of m.claimed[id] with a record now goes through one of them, and a test fails on any new compare outside held.go. Renew, releaseResolved, writeEnv, planTun and commitTun check the record under m.mu after a lookup that holds no lock; a resize between the two answered 404 although the claim still exists. They now answer 503 and the retry reaches the new record. commitVolumeRun answers like its sibling checkVolumeRunLocked. The other heldErrLocked sites run under lockTransition or lockServing, so their answer does not change.
… its doors alone The old record of a resized claim keeps ReplicaCk, ArchiveCk and ArchiveEpoch, so retireReplica and unrowExpired consumed the registry row the new record relies on before commitIfLive found the record replaced: no peer could adopt the claim until the orphan sweep re-armed the row. Both now check the record under m.mu right after taking its lock and return when another record or none holds the id; the swap needs that lock, so the answer holds for the rest of the pass. disarmIfReleased closed the doors at the id for any record that is not the claimed one; Disarm is keyed by id, so for a replaced record it closed the new record's doors. It now disarms only when no record holds the id.
lockServing's fast path, the passive branch of dialRunning and SetInstanceMetadata served through the record they looked up once its TryLock succeeded, so a caller that stalled across a resize reached the old record. lockServing now reads heldErrLocked in the m.mu section that already reads the move pause, before the TryLock, so the data plane keeps one m.mu acquisition; the passive dial and SetInstanceMetadata check the record under m.mu after the TryLock. Each answers ErrReplaced (503) for a replaced record and ErrUnknownSandbox for a released one. The resize test asserted that a wake through the replaced record reaches the running guest; it now asserts ErrReplaced. The egress-lane wake test passed records that were never claimed, which lockServing now refuses first, so it registers them as claims.
…vmm exited" Resize trimmed the "vmm exited; " prefix that coldBootBlocker added for the liveness pass. The prefix now belongs to the two liveness callers, which add it when the VM record exists, so every failed reason and error string stays byte-identical and "vm record gone" keeps no prefix.
…y close Section 1 names held.go and its two helpers, the 503 of the calls that check the record after a lock-free lookup, the identity check of the retire and unrow passes, and disarmIfReleased. Section 7 drops the 404 gap and the consumed-row gap, and narrows the TryLock gap to the window lockServing keeps between its m.mu check and its TryLock.
Both SDKs' Close turned a release 404 into a lookup and a resend, which released the resized record. A 503 retry is returned to the caller instead, and defer-style callers (defer Close, the LangChain close, the mcp shutdown) drop it and leave the resized claim running until its TTL. On a node with archive_failover the release already answered that 503 since #644: it waits for the whole resize in lockTransition and then finds the record replaced. Release now resolves the id again with the same cred and releases the record that holds it whenever its release answers ErrReplaced. Each extra pass needs a resize that completed in between, so the loop is bounded by the resizes that ran. releaseRowed and releaseResolved still answer ErrReplaced to their caller. Hot path: none. Release is not on the claim path, and the common release is still one resolve and one release.
…r calls that take no lock
…can reach Resize takes the old record's Transition before it swaps the record and holds it until the cold boot of the new record has finished, so a TryLock on the old record succeeds only for a caller that stalled between its lookup and its TryLock for the whole resize. The checklist keeps that window as an accepted gap; lockServing keeps its check, which reads the record in the m.mu section it already takes.
…eplaced record there The callers in archive.go, hibernate.go and liveness.go hold the record's Transition, which the swap needs, and the one in claim.go runs before the claim response is sent, so none meets a replaced record. retireReplica and unrowExpired keep their early return: those passes copy pointers and act later.
…the lock answers a replaced record The identity check in lockServing's first m.mu section added one map lookup to every data-plane call (wake, dialPort, the agent relay, the non-passive dial) against main. Resize holds the old record's Transition from before the swap until the new record's cold boot has finished, so a caller that looked the old record up before the swap fails its TryLock and blocks, and the check after the lock answers ErrReplaced; only a caller that stalled for the whole resize gets the TryLock first.
This was referenced Dec 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
One cleanup pass over
sandboxd/poolafter the resize work (#644, #653, #657, #674, #676) and the checklist (#675), plus the remaining structural steps of #419 and a modern-Go pass over engine, server, store, mcp and silkdtest.Behaviour changes
Renew,PUT/PATCHenv,PUTtun (plan and commit) and a volume commit look a record up without a lock and check it underm.mu. A resize between the two answered 404unknown sandboxalthough the claim still exists. They now answer 503sandbox changed while the call waited; retry, and the resend reaches the new record.Manager.Releaseresolves the id again with the same credential when its release finds the record replaced, and releases the record that holds the id: 204. Before, a node withoutarchive_failoveranswered 404 (both SDKs'Closethen did a lookup and a resend), and a node witharchive_failoveranswered the 503 since sandboxd: a resize verb moves a hibernated claim to another pool size and cold-boots it from its disk #644, whichdefer sb.Close(), the LangChain close and the mcp shutdown drop: the resized claim then lived to its TTL.retireReplicaandunrowExpiredleave the registry row alone when they meet a replaced record. The old record keepsReplicaCk,ArchiveCkandArchiveEpoch, so these passes consumed the row the new record relies on; no peer could adopt the claim until the orphan sweep re-armed the row.Not changed: the paths that take the transition lock with
TryLock(lockServing's fast path, the passive dial,SetInstanceMetadata) read what main reads. Resize holds the old record's lock from before the swap until the cold boot has finished, so a caller reaches the old record there only if it stalls between its lookup and the lock for the whole resize.CHECKLIST.mdkeeps that row as an accepted gap.The other sites that answer
ErrReplaced(captureAllowed,beginWake,commitWake,settlePendingSnap,Resizeand itsreshape) are reached with the record's transition lock held, as on main, so no caller sees a different answer from them.Every failed reason and error string is byte-identical; only the status of the calls in (1) and (2) changes.
docs/sandboxd-api.md,docs/sdk.mdanddocs/sdk-python.mdcarry the new answers per route.Consumers of (1) and (2), read at each call site: Go and Python SDK
Renew/SetEnv/PatchEnvget a retryable 503 instead of a 404 that read as "gone"; sandbox-operator, vk-sandbox and cocoonbox read a release 404 as released and so left a resized claim running, and with (2) their delete releases it; SDKClose, the OpenAI and LangChain adapters and mcp get 204 through (2).Structure
held.go):holdsLockedfor passes,heldErrLockedfor calls (ErrReplacedvsErrUnknownSandbox),servingErrLockedfor a record that must also not be paused for a move. They replace 44 inline compares ofm.claimed[id]with a record; a source-scan test fails on a new compare outsideheld.go.failover.go(837 lines) →archive_row.go,archive_intent.go,adopt.go;claim.go(830) →claim.go,release.go,dial.go,quota.go. Pure moves: the sorted lines of the package before and after each move differ only bypackageandimportlines.Managerfields 85 → 76: the replicate and unfence single-runner gates joinsweeps; the heal peers, semaphore and flights joincheckpointHeals; disk and memory pressure become onenodePressurevalue with its own checks (disk.go+memory.go→pressure.go).sweepBounded,storeErr(7 sites),claimStore.commitOrRecommit(7 sites),volumeCatalog.addHolders,ownedTemplatewith the digest,readJSONFile[T](3 loaders),exitBlocker.runJSON[T]andsilkdCollect, serverhandleBodyVerb[T], mcp genericput/dropand one JSON tool result, storeExportFiles, silkdtest request iterator andslices.Chunk.CHECKLIST.mdnames the helpers and files as they are now; the two gaps this change closes are gone from it (15 accepted → 13), none open.Tried and dropped
TryLockon the three paths above: one more map lookup (and for two of them one morem.musection) on every data-plane call, for a window no caller reaches. It landed early in the series and is reverted in it.templateCachetype for the template fields: +28 production lines, six one-caller methods.entryErrLockedfor Resize's entry checks: it cannot sit both before and after the checks that decide which error wins.addCounton the claim path (== 0vs<= 0), a genericappendAudit(one call and onetime.Nowper request with audit off),rewriteRowIf(flag arguments),readChunkthroughio.ReadFull(changes the error a short read gives).Size
37 commits. Production Go +1378 −1434 (net −56), tests +258 −68 (net +190), comment lines +28 −28, docs +27 −27. Pool production files 50 → 55; the largest is
pool.goat 833 lines (wasfailover.go837,pool.go831,claim.go830).Hot path
Target: nothing added against main on the claim path or the data plane. Measured against main f1e66f7 on one machine:
go tool objdumpof the pool test binary per function, and an A/B of both trees interleaved in one run (12 samples per arm, order swapped every round;Hot*are benchmarks written for this comparison, the others are in the tree).Claim path and data plane:
ClaimWarm,ClaimProvision,finalize,finalizeBatch,armIfRunning,admitOptions,quotaErr,resolve,byID,DialPort,dialPort,dialRunning,dialPassive,SetInstanceMetadata,wakeOrRead.heldErrLocked,holdsLockedandservingErrLockedinline at every site.lockServing: 248 instructions (main 271), 10 calls (11), the same three map lookups.Renew: 634 (638), 26 calls (27).capacityErr: 294 instructions (main 293). OneADDforms the address of the pressure group fordiskErr, which is out of line in both;memoryErrinlines in both.admitClaim: 223 instructions in the binary (main 212), becauseaddHolderscarries the drop-at-zero branch thatadoptVolumesdid not have. A claim without volumes runs none of them. A claim with volumes runs one more compare-and-branch pair per volume and never the delete.Allocations per op are equal on every row above. The machine also ran container tests during the samples, which is where the wide intervals come from; no row differs at p < 0.05.
Worse than main, off the claim path and the data plane, accepted:
sweepBoundedsees an empty list.archiveOnceon an empty node: 2.088µ → 2.212µ (+5.9%, p=0.006).beginWake,releaseResolved,commitTunandlockTransition(oneerrors.Iscall), andReleaseis 23 instructions longer for its loop. Claim + release shows no difference.Gates
sbx-gate.shon a3ff8ae: golangci-lint v2.15.00 issues.×10 (five modules, darwin and linux), asl and fmt clean, race tests green on darwin and in a linux/arm64 container.Hardware
Two rounds on the testbed (one NUMA node, 24G slice), binaries built from this head (
sandboxd devel-a3ff8aef98be) with main's binary (devel-f1e66f758d34) as the control. Every run checked that the listener was the arm's binary by sha.claim,hibernate,resize,vmm_restart,releaseclaim,hibernate,archive,unarchive,release; the archive's data gone from the store after the wake (its manifest stays, as on main); sandboxd logged no warning and no errorNot run: the egress, socks and credentials scripts (they need an uplink the round did not have). Not reached by any cell: change (3) above (the replica-retire and expired-row passes run only for a claim whose pool stopped replicating or whose lease lapsed), which rests on its unit tests. The two nodes shared one host, each in its own mount namespace.
Closes #419.