fix(browser): reject pending CDP calls on Session invalidation
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.tsagainst 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 testinpackages/bcode-browser: 50 pass, 8 skipped (live browser/network tests), 0 fail.tsgo --noEmitinpackages/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.