Skip to content

Bug: Sandbox code contexts leak state after 10 executions. #235

@seanrcollings

Description

@seanrcollings

Description

When executing JavaScript code using a context created with sandbox.createCodeContext(), state begins to be shared between contexts after 10 have been created.

Reproduction

See minimal reproduction repo: https://github.com/seanrcollings/cloudflare-worker-sandbox-context-usage

for (let i = 0; i < 20; i++) {
  const context = await sandbox.createCodeContext({
    language: "javascript",
  });

  const result = await sandbox.runCode("const value = 2;", { context });
  results.push(result);
  sandbox.deleteCodeContext(context.id);
}

Issue: The first 10 executions behave as expected and return no errors. After the tenth context, all subsequent contexts seem to be retaining the state of the previous one.

[
  {
    "code": "const value = 2;",
    "logs": {
      "stdout": [],
      "stderr": []
    },
    "results": []
  },
  {
    "code": "const value = 2;",
    "logs": {
      "stdout": [],
      "stderr": []
    },
    "results": []
  },
  {
    "code": "const value = 2;",
    "logs": {
      "stdout": [],
      "stderr": []
    },
    "results": []
  },
  {
    "code": "const value = 2;",
    "logs": {
      "stdout": [],
      "stderr": []
    },
    "results": []
  },
  {
    "code": "const value = 2;",
    "logs": {
      "stdout": [],
      "stderr": []
    },
    "results": []
  },
  {
    "code": "const value = 2;",
    "logs": {
      "stdout": [],
      "stderr": []
    },
    "results": []
  },
  {
    "code": "const value = 2;",
    "logs": {
      "stdout": [],
      "stderr": []
    },
    "results": []
  },
  {
    "code": "const value = 2;",
    "logs": {
      "stdout": [],
      "stderr": []
    },
    "results": []
  },
  {
    "code": "const value = 2;",
    "logs": {
      "stdout": [],
      "stderr": []
    },
    "results": []
  },
  {
    "code": "const value = 2;",
    "logs": {
      "stdout": [],
      "stderr": []
    },
    "results": []
  },
  {
    "code": "const value = 2;",
    "logs": {
      "stdout": [],
      "stderr": [
        "<execution-c4cc7a91-655c-4da6-9c7e-efaa20f20823>:1\nconst value = 2;\n^\n\nSyntaxError: Identifier 'value' has already been declared\n    at Script.runInContext (node:vm:149:12)\n    at Module.runInContext (node:vm:301:6)\n    at Interface.<anonymous> (file:///container-server/dist/runtime/executors/javascript/node_executor.js:64:25)\n    at Interface.emit (node:events:524:28)\n    at [_onLine] [as _onLine] (node:internal/readline/interface:419:12)\n    at [_normalWrite] [as _normalWrite] (node:internal/readline/interface:613:22)\n    at Socket.ondata (node:internal/readline/interface:246:23)\n    at Socket.emit (node:events:524:28)\n    at addChunk (node:internal/streams/readable:561:12)\n    at readableAddChunkPushByteMode (node:internal/streams/readable:512:3)"
      ]
    },
    "error": {
      "name": "ExecutionError",
      "message": "<execution-c4cc7a91-655c-4da6-9c7e-efaa20f20823>:1\nconst value = 2;\n^\n\nSyntaxError: Identifier 'value' has already been declared\n    at Script.runInContext (node:vm:149:12)\n    at Module.runInContext (node:vm:301:6)\n    at Interface.<anonymous> (file:///container-server/dist/runtime/executors/javascript/node_executor.js:64:25)\n    at Interface.emit (node:events:524:28)\n    at [_onLine] [as _onLine] (node:internal/readline/interface:419:12)\n    at [_normalWrite] [as _normalWrite] (node:internal/readline/interface:613:22)\n    at Socket.ondata (node:internal/readline/interface:246:23)\n    at Socket.emit (node:events:524:28)\n    at addChunk (node:internal/streams/readable:561:12)\n    at readableAddChunkPushByteMode (node:internal/streams/readable:512:3)",
      "traceback": []
    },
    "results": []
  },
]

Impact

High - Cannot rely on the sandbox's code interpreter API for execution of code in a consistent fashion.

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions