Skip to content

fix(browser): reject pending CDP calls on Session invalidation - #184

Open
xh123637 wants to merge 1 commit into
browser-use:mainfrom
xh123637:reject-pending-on-invalidate
Open

xh123637 wants to merge 1 commit into
browser-use:mainfrom
xh123637:reject-pending-on-invalidate

Conversation

@xh123637

@xh123637 xh123637 commented Oct 29, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #154

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Session.invalidate() clears this.ws before closing the socket. When the close event lands, the socket's close listener sees this.ws !== ws, treats the socket as superseded, and returns without draining pending — so a CDP call still waiting for its response never settles.

invalidate() now rejects and clears the pending calls itself, with the same invalidation error, before closing the socket. That mirrors how the close listener and the socket-replacement path in openWs already drain pending. Nothing else changes: later calls still fail with the invalidation error, and a socket that closes on its own still rejects pending calls through the close listener.

How did you verify your code works?

  • Reproduced with a standalone script against a WebSocket server that never answers: before the fix, the in-flight call was still pending 500 ms after invalidate(); after the fix it rejects immediately with the invalidation error.
  • Added two regression tests in packages/bcode-browser/test/cdp-session.test.ts against a never-answering server — one in-flight call, and two in-flight calls — both reject with the invalidation error. On the unpatched code both tests hang and hit the test timeout.
  • bun test in packages/bcode-browser: 50 pass, 8 skipped (live browser/network tests), 0 fail.
  • tsgo --noEmit in packages/bcode-browser: clean.

Screenshots / recordings

Not applicable — no UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Summary by cubic

Fixes Session.invalidate() so in-flight CDP calls reject instead of hanging forever. Previously, invalidate() cleared the socket reference before closing it, which made the socket's close listener treat the socket as superseded and skip draining pending calls, leaving waiting calls unsettled. Now invalidate() rejects and clears pending calls with the invalidation error itself before closing the socket; a socket that closes on its own still settles pending calls via the close listener.

Two regression tests in packages/bcode-browser/test/cdp-session.test.ts cover one and two in-flight calls against a server that never answers — both reject with the invalidation error.

Written for commit 628be43. Summary will update on new commits.

View guided diff

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No issues found across 2 files

Heads up: you’re close to your flex budget. Increase your flex budget so reviews don’t pause.

View guided diff | Re-trigger cubic

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.

Session.invalidate() leaves in-flight CDP calls pending

1 participant