Sandbox route: forEach(async) fire-and-forget can run code before multi-file writes finish
Author: chirag127Created Jul 4, 2026Updated Jul 6, 2026
Problem
Multi-file fragments can execute before all files are written to the sandbox — a race condition in app/api/sandbox/route.ts.
Location
app/api/sandbox/route.ts:52-56:
if (fragment.code && Array.isArray(fragment.code)) {
fragment.code.forEach(async (file) => {
await sbx.files.write(file.file_path, file.file_content)
console.log(`Copied file to ${file.file_path} in ${sbx.sandboxId}`)
})
}Array.prototype.forEach ignores the promises returned by the async callback. The outer POST handler continues to sbx.runCode(...) / URL return at line 62+ without awaiting the writes.
Steps
- Generate a multi-file fragment (Next.js/Vue template producing 2+ files).
- POST to
/api/sandboxwithfragment.codeas an array. - Sandbox may run/serve before later files are on disk.
Expected
All files persisted before execution/URL return.
Actual
Fire-and-forget writes; ordering depends on E2B SDK internal timing.
Fix
Replace with await Promise.all(fragment.code.map(file => sbx.files.write(...))).
Environment
@e2b/code-interpreter@^1.0.2, next@^14.2.35, Node per repo package.json.
Thanks for maintaining e2b-dev/fragments!
Source: e2b-dev/fragments