Skip to content

Commit 2c2c01a

Browse files
committed
Add per-session mutex locking to prevent race conditions
Commands executing concurrently within the same session could cause race conditions and interleaved output. This adds per-session mutexes using async-mutex to serialize execution within each session while preserving parallelism across different sessions. The withSession API enables atomic multi-command operations where the lock is held for the entire callback. Background streaming mode allows long-running processes like servers to release the lock after startup so other commands can proceed.
1 parent 472d5ae commit 2c2c01a

File tree

10 files changed

+1095
-541
lines changed

10 files changed

+1095
-541
lines changed

.changeset/session-locking.md

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+
Add per-session mutex locking to prevent concurrent command execution race conditions

packages/sandbox-container/src/core/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ export interface ServiceError {
5656
details?: Record<string, unknown>;
5757
}
5858

59+
/**
60+
* Helper functions to construct ServiceResult with proper typing.
61+
* Use these instead of manual object construction to avoid type casts.
62+
*/
63+
export function serviceSuccess<T>(data: T): ServiceResult<T> {
64+
return { success: true, data } as ServiceResult<T>;
65+
}
66+
67+
export function serviceError<T>(error: ServiceError): ServiceResult<T> {
68+
return { success: false, error } as ServiceResult<T>;
69+
}
70+
5971
// Handler error response structure - matches BaseHandler.createErrorResponse()
6072
export interface HandlerErrorResponse {
6173
success: false;

0 commit comments

Comments
 (0)