Skip to content

Commit 2822051

Browse files
committed
Use error code instead of string matching for session conflicts
String matching on error.message is fragile - if the message wording changes, the code silently breaks. Using error.code is type-safe and reliable.
1 parent aab345f commit 2822051

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

.changeset/improve-session-initialization.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'@cloudflare/sandbox': patch
33
---
44

5-
Improve session initialization to check storage before creating new session
5+
Fix session initialization to eliminate noisy error logs during hot reloads

CLAUDE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@ Turbo handles task orchestration (`turbo.json`) with dependency-aware builds.
291291

292292
**Subject line should stand alone** - don't require reading the body to understand the change. Body is optional and only needed for non-obvious context.
293293

294+
**Focus on the change, not how it was discovered** - never reference "review feedback", "PR comments", or "code review" in commit messages. Describe what the change does and why, not that someone asked for it.
295+
294296
Good examples:
295297

296298
```

packages/sandbox/src/sandbox.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ import {
3737
CustomDomainRequiredError,
3838
ErrorCode,
3939
ProcessExitedBeforeReadyError,
40-
ProcessReadyTimeoutError
40+
ProcessReadyTimeoutError,
41+
SandboxError
4142
} from './errors';
4243
import { CodeInterpreter } from './interpreter';
4344
import { isLocalhostPattern } from './request-handler';
@@ -1014,7 +1015,10 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
10141015
this.logger.debug('Default session initialized', { sessionId });
10151016
} catch (error: unknown) {
10161017
// Session may already exist (e.g., after hot reload or concurrent request)
1017-
if (error instanceof Error && error.message.includes('already exists')) {
1018+
if (
1019+
error instanceof SandboxError &&
1020+
error.code === ErrorCode.RESOURCE_BUSY
1021+
) {
10181022
this.logger.debug(
10191023
'Session exists in container but not in DO state, syncing',
10201024
{ sessionId }

0 commit comments

Comments
 (0)