Skip to content

Share in-flight default session init#613

Merged
ghostwriternr merged 3 commits intomainfrom
fix/ensure-default-session-coalescer
Apr 23, 2026
Merged

Share in-flight default session init#613
ghostwriternr merged 3 commits intomainfrom
fix/ensure-default-session-coalescer

Conversation

@ghostwriternr
Copy link
Copy Markdown
Member

@ghostwriternr ghostwriternr commented Apr 22, 2026

Parallel operations on a fresh sandbox each fired their own createSession RPC; the first succeeded and the rest got SessionAlreadyExistsError handled as a no-op. Sequential callers already short-circuited on the second call, so the duplication was bounded to the narrow window before the first call settled, but there's no reason to let it happen. The first caller now records the in-flight initialization promise, and later callers that arrive before it settles share that promise.

Setup was also writing the session id to the in-memory field before persisting it to DO storage. That ordering means a storage write that fails after the container has accepted the create call would leave the DO thinking setup completed. Swapped to persist-first, cache-second so a failed persistence retries on the next operation.


Open in Devin Review

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 22, 2026

🦋 Changeset detected

Latest commit: bca7eda

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
Copy Markdown

pkg-pr-new Bot commented Apr 22, 2026

Open in StackBlitz

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

commit: bca7eda

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 22, 2026

📦 Preview Build

Version: 0.0.0-pr-613-bca7eda

Install the SDK preview:

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

🐳 Docker images were not rebuilt — no container changes detected. Use the latest release images from Docker Hub.

devin-ai-integration[bot]

This comment was marked as resolved.

@ghostwriternr ghostwriternr marked this pull request as draft April 22, 2026 12:51
@ghostwriternr ghostwriternr force-pushed the fix/ensure-default-session-coalescer branch from 25e6f28 to 2d9bc5c Compare April 23, 2026 11:32
@ghostwriternr ghostwriternr marked this pull request as ready for review April 23, 2026 11:35
devin-ai-integration[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

Copy link
Copy Markdown
Contributor

@aron-cf aron-cf left a comment

Choose a reason for hiding this comment

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

Nice!

@ghostwriternr ghostwriternr enabled auto-merge (squash) April 23, 2026 15:34
Parallel operations on a fresh sandbox each fired their own
createSession RPC; the first succeeded and the rest got
SessionAlreadyExistsError handled as a no-op. Callers that target
the same default session id now share one in-flight initialization
promise. The slot is keyed by session id so that if the sandbox
is named mid-flight, a caller expecting the new name starts its
own init.

Setup also writes to storage before updating the in-memory session
id, so a storage failure after createSession succeeds retries on
the next operation rather than treating setup as complete.
The in-flight session initialization promise introduced in the previous
commit could interleave with onStop. If createSession returned
successfully after onStop cleared the default session, the init's
continuation would re-persist the stale session id, and any concurrent
caller could join the stale promise and receive it. The next DO wake
would then fast-path through a session that no longer exists in the
fresh container.

onStop now advances a container generation counter and clears the
in-flight slot before any await. The init captures the generation at
start and checks it between the container RPC and the state write. A
container stop during init fails the current attempt so the next
caller starts fresh against the new container.
@ghostwriternr ghostwriternr force-pushed the fix/ensure-default-session-coalescer branch from d76e9dc to bca7eda Compare April 23, 2026 15:40
@ghostwriternr ghostwriternr disabled auto-merge April 23, 2026 15:42
@ghostwriternr ghostwriternr merged commit 174313f into main Apr 23, 2026
21 checks passed
@ghostwriternr ghostwriternr deleted the fix/ensure-default-session-coalescer branch April 23, 2026 15:52
@sandy-bonk sandy-bonk Bot mentioned this pull request Apr 23, 2026
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.

2 participants