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/sandbox with fragment.code as 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!
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:Array.prototype.forEachignores the promises returned by the async callback. The outerPOSThandler continues tosbx.runCode(...)/ URL return at line 62+ without awaiting the writes.Steps
/api/sandboxwithfragment.codeas an array.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 repopackage.json.Thanks for maintaining e2b-dev/fragments!