fix(sandbox): recreate orphaned microVMs with --replace

A session VM create that fails partway leaves an orphaned msb sandbox that msb ls omits but every later create rejects as already existing, so the session could never get a VM again. Retry the create once with --replace when msb reports the name already exists.
This commit is contained in:
Chris Scott committed 2026-10-15 16:53:49 -04:00
1 parent 052218d5d6
commit 27c673d830
4 files changed
+123 -6

No files matched your search

+2 -1
View File
@@ -214,7 +214,7 @@ function parseSandboxNamedMountSpec(spec: string): { name: string; guest: string
return { name, guest }
}
export function buildSandboxCreateArgs(spec: SandboxVmSpec): string[] {
export function buildSandboxCreateArgs(spec: SandboxVmSpec, options: { replace?: boolean } = {}): string[] {
const tmpfsSizeMib = resolveSandboxRuntimeTmpfsSizeMib(spec.memoryMib)
if (tmpfsSizeMib === null) throw new Error('sandbox memory must be positive to size the runtime tmpfs')
const workdir = spec.projectMounts[0]
@@ -222,6 +222,7 @@ export function buildSandboxCreateArgs(spec: SandboxVmSpec): string[] {
return [
'run',
'-d',
...(options.replace === true ? ['--replace'] : []),
'--name',
spec.name,
'--label',
+25 -5
View File
@@ -56,6 +56,7 @@ const SANDBOX_IDLE_SWEEP_INTERVAL_MS = 60000
const SANDBOX_RUNTIME_TMPFS_GUEST = path.resolve('/tmp')
const SANDBOX_REMOVAL_MISSING_PATTERN = /not found|no such sandbox|unknown sandbox|does not exist/i
const SANDBOX_VOLUME_REMOVAL_MISSING_PATTERN = /not found|no such volume|unknown volume|does not exist/i
const SANDBOX_CREATE_EXISTS_PATTERN = /already exists/i
export type SandboxShellTarget = {
rootSessionId: string
@@ -798,11 +799,7 @@ function scheduleBackgroundProvisionRetry(name: string): void {
}
async function createSessionSandbox(spec: SandboxVmSpec, maxRunningVms: number): Promise<void> {
await withStartAdmission(spec.name, maxRunningVms, async () => {
await executeCommand([sandboxExecutablePath(), ...buildSandboxCreateArgs(spec)], {
timeout: ENV.SANDBOX.START_TIMEOUT_MS,
})
})
await withStartAdmission(spec.name, maxRunningVms, () => runSandboxCreate(spec))
await provisionSandboxExecUser(spec.name)
const attestation = await attestSessionSandbox(spec, true)
if (!attestation.trusted) {
@@ -810,6 +807,29 @@ async function createSessionSandbox(spec: SandboxVmSpec, maxRunningVms: number):
}
}
async function runSandboxCreate(spec: SandboxVmSpec): Promise<void> {
const first = await executeSandboxCreate(buildSandboxCreateArgs(spec))
if (first.exitCode === 0) return
if (!SANDBOX_CREATE_EXISTS_PATTERN.test(`${first.stdout}\n${first.stderr}`)) {
throw new Error(`Command failed with code ${first.exitCode}: ${first.stderr || first.stdout}`)
}
logger.warn(
`Sandbox ${spec.name} already exists as an orphaned msb sandbox; recreating it with --replace: ${first.stderr || first.stdout}`,
)
const replaced = await executeSandboxCreate(buildSandboxCreateArgs(spec, { replace: true }))
if (replaced.exitCode !== 0) {
throw new Error(`Command failed with code ${replaced.exitCode}: ${replaced.stderr || replaced.stdout}`)
}
}
async function executeSandboxCreate(args: string[]): Promise<{ exitCode: number; stdout: string; stderr: string }> {
const result = (await executeCommand([sandboxExecutablePath(), ...args], {
ignoreExitCode: true,
timeout: ENV.SANDBOX.START_TIMEOUT_MS,
})) as string | { exitCode: number; stdout: string; stderr: string }
return typeof result === 'string' ? { exitCode: 0, stdout: result, stderr: '' } : result
}
async function removeSessionSandboxVm(name: string): Promise<void> {
await runSandboxRemoval(buildSandboxRemoveArgs(name), SANDBOX_REMOVAL_MISSING_PATTERN, 'msb rm')
}