Skip to content

Sandbox route: forEach(async) fire-and-forget can run code before multi-file writes finish #229

Description

@chirag127

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

  1. Generate a multi-file fragment (Next.js/Vue template producing 2+ files).
  2. POST to /api/sandbox with fragment.code as an array.
  3. 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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions