Skip to content

Commit 3b5c262

Browse files
Document code context isolation improvements
Adds documentation for improved code context isolation: - Each context now gets a dedicated executor process (1:1 binding) - Contexts prevent concurrent execution on the same context - Complete state isolation between contexts guaranteed Related to cloudflare/sandbox-sdk#249 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 0c752d9 commit 3b5c262

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const context = await sandbox.createCodeContext(options?: CreateContextOptions):
2828

2929
**Returns**: `Promise<CodeContext>` with `id`, `language`, `cwd`, `createdAt`, `lastUsed`
3030

31+
**Isolation guarantees**: Each code context gets a dedicated executor process from creation to deletion, ensuring complete state isolation between contexts. Variables, imports, and functions defined in one context never leak to another context.
32+
3133
<TypeScriptExample>
3234
```
3335
const ctx = await sandbox.createCodeContext({
@@ -146,6 +148,24 @@ await sandbox.deleteCodeContext(ctx.id);
146148
```
147149
</TypeScriptExample>
148150

151+
:::note[Concurrent execution on same context]
152+
A code context can only execute one piece of code at a time. If you attempt to run code on a context that is already executing, the request will fail with an error. To run code concurrently, create multiple contexts:
153+
154+
<TypeScriptExample>
155+
```
156+
// Create separate contexts for concurrent execution
157+
const ctx1 = await sandbox.createCodeContext({ language: 'python' });
158+
const ctx2 = await sandbox.createCodeContext({ language: 'python' });
159+
160+
// Run code concurrently in different contexts
161+
const results = await Promise.all([
162+
sandbox.runCode('import time; time.sleep(2); 42', { context: ctx1 }),
163+
sandbox.runCode('import time; time.sleep(2); 100', { context: ctx2 })
164+
]);
165+
```
166+
</TypeScriptExample>
167+
:::
168+
149169
## Rich Output Formats
150170

151171
Results include: `text`, `html`, `png`, `jpeg`, `svg`, `latex`, `markdown`, `json`, `chart`, `data`

0 commit comments

Comments
 (0)