Skip to content

Conversation

@ghostwriternr
Copy link
Member

Fixes memory leaks from three sources: listener accumulation during log streaming, unbounded process storage, and stale Durable Object state after container restarts.

Changes:

  • ProcessStore persists completed processes to disk and frees memory
  • ReadableStream cancel handlers remove listeners when streaming stops
  • Sandbox onStop lifecycle hook clears container-specific state

Memory leaks occurred from three sources: event listeners accumulated
when log streaming was cancelled, completed processes remained in
memory indefinitely, and Durable Object state retained invalid
references after container restarts.

The ProcessStore now persists completed processes to disk while
keeping active processes in memory. ReadableStream cancel handlers
properly remove listeners when log streaming stops. The Sandbox
onStop lifecycle hook clears container-specific state to prevent
stale references.
@changeset-bot
Copy link

changeset-bot bot commented Nov 17, 2025

🦋 Changeset detected

Latest commit: 455ed50

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

claude[bot]

This comment was marked as outdated.

The onStop lifecycle hook now properly awaits storage deletions to
ensure cleanup completes before shutdown. Processes are deleted from
memory before persisting to disk to prevent race conditions during
concurrent access. Stream listeners are cleaned up on all close paths,
not just cancellation. The list method now scans disk to include
completed processes, maintaining backward compatibility.
claude[bot]

This comment was marked as outdated.

claude[bot]

This comment was marked as outdated.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Nov 17, 2025

Open in StackBlitz

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

commit: 55421ae

@github-actions
Copy link
Contributor

github-actions bot commented Nov 17, 2025

🐳 Docker Image Published

FROM cloudflare/sandbox:0.0.0-pr-224-094eefd

Version: 0.0.0-pr-224-094eefd

You can use this Docker image with the preview package from this PR.

Write to disk before deleting from memory to prevent data loss if write
fails. Always delete from memory regardless of write success to prevent
memory leaks.
Bun.file().exists() returns false for directories, causing list() to skip
disk scanning and miss completed processes. Remove the directory existence
check and rely on error handling instead.
Test coverage for memory/disk hybrid storage, terminal state transitions,
disk persistence, write failure handling, and list() filtering.
Verify completed processes can be retrieved via get() with exit codes and
timestamps, and that list() includes both running and completed processes.
claude[bot]

This comment was marked as outdated.

Change from /tmp/.sandbox-internal to /tmp/sandbox-internal for better
debuggability. Makes it easier to discover and inspect process files during
development and troubleshooting.
Event handler called store.update() fire-and-forget, causing API
queries to return stale data before disk writes completed.

Changed SessionManager to accept async callbacks and await them,
ensuring store updates complete before processing next event.

This provides strong consistency while maintaining the memory leak
fix that moves completed processes to disk.
claude[bot]

This comment was marked as outdated.

Applies memory leak prevention from this PR to the activeMounts feature
that was merged from main. When a container stops or restarts, mount
points become invalid but the Map still held stale references.
Clearing it maintains consistency with portTokens and defaultSession.
claude[bot]

This comment was marked as outdated.

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