Skip to content

fix: websocket session and callback promise retention leak#154

Open
VastBlast wants to merge 3 commits intocloudflare:mainfrom
VastBlast:fix-153
Open

fix: websocket session and callback promise retention leak#154
VastBlast wants to merge 3 commits intocloudflare:mainfrom
VastBlast:fix-153

Conversation

@VastBlast
Copy link
Contributor

@VastBlast VastBlast commented Mar 19, 2026

This PR fixes #153, a client-side memory leak in long-lived RPC sessions.

The session read loop was racing each transport.receive() call against a single session-long abort promise, which caused Promise.race() to accumulate until the entire session was disposed. This changes RpcSession to use a fresh cancellation promise for each pending read.

Edit: I also noticed another issue with callbacks causing a memory leak which isn't specifically reproduced in #153 which has been patched in this PR too.

Essentially if you had something like this where the callback is called regularly:

subscribeToTicks(cb: RpcStub<(ts: number) => void>) {
    const callback = cb.dup();

    let running = true;
    (async () => {
        while (running) {
            callback(Date.now());
            await new Promise(resolve => setTimeout(resolve, 100));
        }
    })()

    function cleanup() {
        running = false;
        callback[Symbol.dispose]?.();
    }

    callback.onRpcBroken(() => { cleanup(); });
}

The client would subscribe and capnweb would keep the callback promises in memory even though the result cannot be used.

@changeset-bot
Copy link

changeset-bot bot commented Mar 19, 2026

⚠️ No Changeset found

Latest commit: 4fc13b0

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@VastBlast VastBlast changed the title fix: websocket session promise retention leak fix: websocket session and callback promise retention leak Mar 19, 2026
@VastBlast VastBlast marked this pull request as draft March 21, 2026 07:18
@VastBlast VastBlast marked this pull request as ready for review March 22, 2026 22:32
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.

Memory leak on client side with websocket sessions

1 participant