Skip to content

Conversation

@ghostwriternr
Copy link
Member

@ghostwriternr ghostwriternr commented Dec 8, 2025

Summary

Eliminates noisy ERROR logs for "session already exists" during wrangler dev hot reloads.

Changes

  • Use RESOURCE_BUSY (409) instead of INTERNAL_ERROR (500) for session conflicts
  • Refactor ensureDefaultSession() with explicit fast path
  • Improve debug logging for state divergence

Reduces unnecessary "session already exists" errors by checking
DO storage before attempting to create a new session.
@changeset-bot
Copy link

changeset-bot bot commented Dec 8, 2025

🦋 Changeset detected

Latest commit: 2cbbf09

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@cloudflare/sandbox Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 8, 2025

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/sandbox-sdk/@cloudflare/sandbox@281

commit: 2cbbf09

@github-actions
Copy link
Contributor

github-actions bot commented Dec 8, 2025

🐳 Docker Images Published

Default (no Python):

FROM cloudflare/sandbox:0.0.0-pr-281-956a215

With Python:

FROM cloudflare/sandbox:0.0.0-pr-281-956a215-python

Version: 0.0.0-pr-281-956a215

Use the -python variant if you need Python code execution.

claude[bot]

This comment was marked as outdated.

The storage check was defensive code that added latency but rarely
helped since the constructor already loads defaultSession from storage
via blockConcurrencyWhile.
@ghostwriternr ghostwriternr force-pushed the fix/session-initialization branch from 414b353 to 933cf84 Compare December 8, 2025 11:32
claude[bot]

This comment was marked as outdated.

Changes INTERNAL_ERROR (500) to RESOURCE_BUSY (409) for "session
already exists" errors. This prevents noisy ERROR-level logs during
wrangler dev hot reloads since the SDK only logs 5xx errors.

Also improves the debug message to clarify state divergence.
@ghostwriternr
Copy link
Member Author

Re: the guard change from if (!this.defaultSession) to if (this.defaultSession === sessionId):

The scenario where they differ (sandboxName changing mid-session) is contrived - sandboxName is set once via init() and stays constant. The new behavior is also more correct: if they somehow diverge, we want the session matching the current sandboxName, not a stale one.

More importantly, the root cause of the noisy logs is now fixed - container returns 409 instead of 500, so hitting the error path is clean.

claude[bot]

This comment was marked as outdated.

String matching on error.message is fragile - if the message wording
changes, the code silently breaks. Using error.code is type-safe and
reliable.
Replaces RESOURCE_BUSY with a semantically correct error code that
properly indicates a uniqueness constraint violation rather than a
temporary lock.
Copy link
Contributor

@claude claude bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

Previous threads resolved - the force push addressed all concerns:

  • String matching replaced with instanceof SessionAlreadyExistsError (type-safe)
  • SESSION_ALREADY_EXISTS error type properly implemented following the PORT_ALREADY_EXPOSED pattern
  • Diagnostic logging added for state divergence
  • Changeset description is accurate

Implementation quality is high. The new error type follows the established pattern perfectly across all layers:

  • Error code, context, status map, class, adapter, suggestions all correct
  • Container returns 409 instead of 500
  • SDK catches specific error type instead of string matching

One gap: test coverage. The new error type has no tests. Consider adding:

  1. Container test verifying SESSION_ALREADY_EXISTS when creating duplicate session
  2. SDK test verifying graceful handling of the error (hot reload scenario)

Not blocking - implementation is sound and follows well-tested patterns.

Minor note: CLAUDE.md changes (commit message guidelines) are good but unrelated to session initialization. Consider separate PR for doc improvements.

Looks good to merge.

@ghostwriternr ghostwriternr merged commit 472d5ae into main Dec 8, 2025
12 checks passed
@ghostwriternr ghostwriternr deleted the fix/session-initialization branch December 8, 2025 14:39
@github-actions github-actions bot mentioned this pull request Dec 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant