{"record":{"id":"8fa96dd1b85e6687","repo":"mastra-ai/mastra","slug":"command-failed-inside-sandbox-exit-result-exitc","errorCode":null,"errorMessage":"Command failed inside sandbox (exit ${result.exitCode}): ${what}\\n${truncate(result.stderr || result.stdout, 4_000)}","messagePattern":"Command failed inside sandbox \\(exit (.+?)\\): (.+?)\\\\n(.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"deployers/sandbox/src/shared.ts","lineNumber":73,"sourceCode":"  },\n): Promise<{ stdout: string; stderr: string; exitCode: number }> {\n  if (!sandbox.executeCommand) {\n    throw new Error(\n      `Sandbox provider \"${sandbox.provider}\" does not support executeCommand, which is required for sandbox deploys.`,\n    );\n  }\n  // Run via `sh -c` (argv style): providers pass `command` straight to their\n  // exec API as an executable, so a raw script string with spaces would fail.\n  const result = await sandbox.executeCommand(\n    'sh',\n    ['-c', script],\n    opts?.timeout ? { timeout: opts.timeout } : undefined,\n  );\n  if (!result.success && !opts?.allowFailure) {\n    // Never echo the full script back: it can contain secrets (env values)\n    // or entire base64 upload chunks. Use the label or a bounded excerpt.\n    const what = opts?.label ?? truncate(script, 120);\n    throw new Error(\n      `Command failed inside sandbox (exit ${result.exitCode}): ${what}\\n${truncate(result.stderr || result.stdout, 4_000)}`,\n    );\n  }\n  return { stdout: result.stdout, stderr: result.stderr, exitCode: result.exitCode };\n}\n\nfunction truncate(value: string, max: number): string {\n  return value.length > max ? `${value.slice(0, max)}… (truncated)` : value;\n}\n\n/**\n * Kill the previously launched server (if any) using its pidfile, waiting for\n * the process to exit (bounded, then SIGKILL) so the replacement never races\n * the old server for the port. Safe when nothing is running.\n */\nexport async function killPreviousServer(sandbox: WorkspaceSandbox, remoteDir: string): Promise<void> {\n  const pidfile = shellQuote(`${remoteDir}/${SERVER_PIDFILE}`);\n  const script = [","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/deployers/sandbox/src/shared.ts#L55-L91","documentation":"A command executed inside the sandbox exited non-zero and allowFailure was not set. The error includes the exit code, a short label (or 120-char script excerpt — never the full script, which may contain secrets), and up to 4KB of stderr/stdout so you can diagnose the remote failure.","triggerScenarios":"Any runInSandbox command (deployToSandbox, markerCheck, uploadFile, killPreviousServer, launchServer) whose provider result has success=false with opts.allowFailure unset — e.g. the app fails to launch, the upload decode fails, or a marker check misses.","commonSituations":"Server crashes on boot (bad env, missing deps), base64 upload chunk corruption, stale server process that killPreviousServer cannot kill, package install failures in the sandbox image.","solutions":["Read the stderr/stdout excerpt in the message to identify the failing remote command.","Reproduce the command in the sandbox (e.g. via its logs/console) and fix the underlying cause (deps, env, paths).","If non-zero exit is expected for a probe, rerun with `allowFailure: true` and a `label` so the failure is tolerated and readable.","Verify required env vars and files were uploaded before the failing step."],"exampleFix":"// before\nawait runInSandbox(sandbox, './start-server.sh', { label: 'start-server' });\n// after (exit code tolerated)\nawait runInSandbox(sandbox, './start-server.sh', { label: 'start-server', allowFailure: true });","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await deployToSandbox({ sandbox });\n} catch (e) {\n  const m = e.message.match(/Command failed inside sandbox \\(exit (\\d+)\\): (.*)/s);\n  if (m) {\n    logger.error(`sandbox step failed (exit ${m[1]}): ${m[2].slice(0, 500)}`);\n    // inspect sandbox logs, then retry or abort\n  } else throw e;\n}","preventionTips":["Validate env, deps, and uploaded files before launch steps","Use `label` and `allowFailure` for probe commands with expected non-zero exits","Check the sandbox's own logs when the excerpt is insufficient"],"tags":["sandbox","command-failure","deploy"],"backgroundTag":"sandbox-command-failed","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}