Sandbox route: forEach(async) fire-and-forget can run code before multi-file writes finish
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 6.4k
- Forks
- 862
- Avg merge
- 2d 22h
- Merged PRs (30d)
- 2
Description
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!
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in app/api/sandbox/route.ts:52-56 and trace the POST handler through sbx.runCode and the URL return. Reproduce with a multi-file fragment, then verify that every file is persisted before execution or the URL is returned.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- nextjs, typescript
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 82/100