Skip to content

Commit 0c752d9

Browse files
Document code context isolation guarantees
Add documentation explaining that each code context is completely isolated with a dedicated executor process. Includes example showing concurrent execution across multiple contexts. Related to cloudflare/sandbox-sdk#249 which fixed state isolation bug.
1 parent ad91c85 commit 0c752d9

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/content/docs/sandbox/api/interpreter.mdx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ const ctx = await sandbox.createCodeContext({
3737
```
3838
</TypeScriptExample>
3939

40+
:::note[Context isolation]
41+
Each code context is completely isolated from other contexts. Variables, imports, functions, and state defined in one context are never accessible to another context. Each context has a dedicated executor process from creation to deletion, ensuring complete isolation even under heavy concurrent usage.
42+
:::
43+
4044
### `runCode()`
4145

4246
Execute code in a context and return the complete result.
@@ -70,6 +74,13 @@ await sandbox.runCode('import math; radius = 5', { context: ctx });
7074
const result = await sandbox.runCode('math.pi * radius ** 2', { context: ctx });
7175
7276
console.log(result.results[0].text); // "78.53981633974483"
77+
78+
// Create isolated contexts for concurrent execution
79+
const ctx2 = await sandbox.createCodeContext({ language: 'python' });
80+
await Promise.all([
81+
sandbox.runCode('x = 1', { context: ctx }),
82+
sandbox.runCode('x = 2', { context: ctx2 })
83+
]);
7384
```
7485
</TypeScriptExample>
7586

0 commit comments

Comments
 (0)