fix: fail fast on tool calls stuck after a dead browser connection - #2699
Open
bitbay wants to merge 1 commit into
Open
fix: fail fast on tool calls stuck after a dead browser connection#2699bitbay wants to merge 1 commit into
bitbay wants to merge 1 commit into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Root cause: when the debugged browser's CDP transport dies mid-call — silently, without ever firing a `close`/`error`/`disconnected` event (as opposed to a clean disconnect) — the in-flight tool call just hangs forever waiting on a response that will never come. Because `ToolHandler` serializes every tool call behind a single shared `Mutex`, that one stuck call blocks all subsequent tool calls too, effectively taking the whole MCP server down until someone manually reconnects via `/mcp`. Observed when an Android app being debugged over `chrome-devtools-mcp` was reinstalled/relaunched mid-session, the MCP server's tools went completely unavailable, even though the underlying CDP endpoint (http://127.0.0.1:9222/json) stayed reachable the whole time. This wasn't a startup/connection-time failure, which the existing "next-call self-heal" logic already handles fine — it was a hang mid-call, so there was never a clean "next call" for that self-heal to run on. - `browser.ts`: export `forgetBrowser()` and attach a `disconnected` listener after every successful connect/launch, so a browser Puppeteer does detect as closed is dropped from the module cache immediately rather than relying solely on the next call's `browser?.connected` check. - `ToolHandler.ts`: bound each tool handler invocation with a 60s timeout. On timeout, forget the cached browser handle and fail with a clear error instead of hanging until the client's own timeout gives up on the whole server. The next tool call then reconnects via the existing `ensureBrowserConnected`/`#getContext` self-heal path instead of reusing a handle that still looks connected. The forget call is now injectable via an optional constructor parameter (defaulting to the real `forgetBrowser`), so the timeout path can be unit tested without stubbing an ES module. - `tests/ToolHandler.test.ts`: cover the timeout path directly — a tool handler that never resolves triggers the 60s timeout, `forgetBrowser` is called with the current browser, and a normal (non-timeout) handler rejection does not trigger it. - `tests/browser.test.ts`: cover the disconnected-listener path against a real launched browser — a clean `disconnect()` is forgotten automatically so the next `ensureBrowserConnected()` reconnects instead of reusing the dead handle, and `forgetBrowser()` only clears the cache on an exact reference match. Deliberately scoped to fail-fast + rely on the existing next-call self-heal, not automatic retry-and-replay of the timed-out call (a larger change to discuss with whoever verifies this against the real device). A full Android reproduction is out of scope for this change; verified here via the targeted unit tests above.
bitbay
force-pushed
the
fix/connection-recovery-timeout
branch
from
September 8, 2026 15:22
270c579 to
6d2d4e9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Root cause: when the debugged browser's CDP transport dies mid-call — silently, without ever firing a
close/error/disconnectedevent (as opposed to a clean disconnect) — the in-flight tool call just hangs forever waiting on a response that will never come. BecauseToolHandlerserializes every tool call behind a single sharedMutex, that one stuck call blocks all subsequent tool calls too, effectively taking the whole MCP server down until someone manually reconnects via/mcp.Observed when an Android app being debugged over
chrome-devtools-mcpwas reinstalled/relaunched mid-session, the MCP server's tools went completely unavailable, even though the underlying CDP endpoint (http://127.0.0.1:9222/json) stayed reachable the whole time. This wasn't a startup/connection-time failure, which the existing "next-call self-heal" logic already handles fine — it was a hang mid-call, so there was never a clean "next call" for that self-heal to run on.browser.ts: exportforgetBrowser()and attach adisconnectedlistener after every successful connect/launch, so a browser Puppeteer does detect as closed is dropped from the module cache immediately rather than relying solely on the next call'sbrowser?.connectedcheck.ToolHandler.ts: bound each tool handler invocation with a 60s timeout. On timeout, forget the cached browser handle and fail with a clear error instead of hanging until the client's own timeout gives up on the whole server. The next tool call then reconnects via the existingensureBrowserConnected/#getContextself-heal path instead of reusing a handle that still looks connected. The forget call is now injectable via an optional constructor parameter (defaulting to the realforgetBrowser), so the timeout path can be unit tested without stubbing an ES module.tests/ToolHandler.test.ts: cover the timeout path directly — a tool handler that never resolves triggers the 60s timeout,forgetBrowseris called with the current browser, and a normal (non-timeout) handler rejection does not trigger it.tests/browser.test.ts: cover the disconnected-listener path against a real launched browser — a cleandisconnect()is forgotten automatically so the nextensureBrowserConnected()reconnects instead of reusing the dead handle, andforgetBrowser()only clears the cache on an exact reference match.Deliberately scoped to fail-fast + rely on the existing next-call self-heal, not automatic retry-and-replay of the timed-out call (a larger change to discuss with whoever verifies this against the real device). A full Android reproduction is out of scope for this change; verified here via the targeted unit tests above.