Skip to content

Commit e69dce5

Browse files
Fix code context state isolation (#249)
* Fix code context state isolation Each code context now gets a dedicated executor process from creation to deletion, preventing state leakage between contexts. Removed maximum pool size limits to allow organic scaling based on context count. * Improve context isolation reliability and safety Add atomic concurrent execution checks, exit handler cleanup, error handling for executor release failures, per-language process limits, and language validation. Include concurrent execution test. * Simplify error handling and improve test coverage Remove redundant try-catch blocks that just rethrow errors, use try-finally pattern for guaranteed cleanup, simplify comments about atomicity, and enhance concurrent execution test to verify actual state isolation rather than just error counts. * Use promise-based locking for concurrent execution Replace isAvailable flag with executionPromise to fix race condition. The promise acts as an atomic lock preventing concurrent execution on the same context. * Revert Dockerfile and improve concurrency test Revert Dockerfile platform change that should not be committed. Increase concurrent test requests to 20 for better stress testing. * Serialize execution per code context Use async-mutex to prevent race conditions when concurrent requests target the same context. Execute code before creating stream to hold mutex during actual execution. * Fix test-worker dockerfile * Add pool locking and executor health checks Prevents race conditions when multiple contexts reserve executors concurrently, and detects crashed executors before execution. * Fix executor leak and stateless execution race - Add cleanup on context creation failure to prevent executor leaks - Serialize stateless executions with per-language mutex * Refactor to single-layer executor locking Moved all mutex locking from InterpreterService to ProcessPoolManager, using per-executor locks instead of per-context locks. This enables parallel stateless execution via borrow/return pattern and simplifies code by centralizing all concurrency control in one layer. * Add test for concurrent context execution isolation Adds E2E test that verifies state isolation is maintained when executing code concurrently across 12 different contexts. This validates that the executor locking mechanism prevents race conditions while maintaining proper context isolation.
1 parent 088ee5f commit e69dce5

File tree

11 files changed

+909
-740
lines changed

11 files changed

+909
-740
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@cloudflare/sandbox': patch
3+
---
4+
5+
Fix code context isolation bug where contexts leaked state after 10 executions. Each code context now gets a dedicated executor process from creation to deletion, ensuring complete isolation between contexts. Removed maximum pool size limits to allow organic scaling.

package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/sandbox-container/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
},
1414
"dependencies": {
1515
"@repo/shared": "*",
16+
"async-mutex": "^0.5.0",
1617
"esbuild": "^0.27.0",
1718
"zod": "^3.22.3"
1819
},

packages/sandbox-container/src/handlers/interpreter-handler.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import type {
99
import { ErrorCode } from '@repo/shared/errors';
1010

1111
import type { RequestContext } from '../core/types';
12-
import type { CreateContextRequest } from '../interpreter-service';
13-
import type { InterpreterService } from '../services/interpreter-service';
12+
import type {
13+
CreateContextRequest,
14+
InterpreterService
15+
} from '../services/interpreter-service';
1416
import { BaseHandler } from './base-handler';
1517

1618
export class InterpreterHandler extends BaseHandler<Request, Response> {

packages/sandbox-container/src/interpreter-service.ts

Lines changed: 0 additions & 299 deletions
This file was deleted.

0 commit comments

Comments
 (0)