fix(browser): clear stale Chrome singleton lock before launch#1046
fix(browser): clear stale Chrome singleton lock before launch#1046RaviTharuma wants to merge 2 commits intoChromeDevTools:mainfrom
Conversation
When an MCP client (or the host process) crashes or is killed without cleanly shutting down the browser, Chrome's singleton lock files (SingletonLock, SingletonSocket, SingletonCookie) persist in the user data directory. On the next startup Chrome refuses to launch because it believes another instance is already running, producing the error: "The browser is already running for <userDataDir>." This is particularly disruptive for MCP clients that manage the browser lifecycle automatically — users have to manually find and delete the lock files or restart their machine to recover. This commit adds a `clearStaleSingletonLock()` helper that runs before `puppeteer.launch()` when a persistent user data directory is in use. It reads the PID encoded in the SingletonLock symlink (format: `hostname-PID`), checks whether that process is still alive via `process.kill(pid, 0)`, and removes the lock files only when the process no longer exists. If the process is still running the lock is left untouched and Chrome's own conflict detection handles it normally.
|
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. |
|
I have signed the CLA. |
|
I believe Chrome is managing the lock files and we should not be clearing it for Chrome. I believe Chrome should already detect if the process is running. Do you have a test case or a screencast of this happening? Perhaps the fix needs to be done on the Chrome side. |
|
@OrKoN Thanks for the review — fair question. Here's the concrete scenario: Reproduction:
You're right that Chrome itself handles lock detection when Chrome is the one managing its own lifecycle. But in the MCP context, Chrome is launched by puppeteer as a child process. When the parent (MCP client) crashes without triggering puppeteer's cleanup, Chrome exits too (orphaned child), but the lock files remain because nobody called Chrome's own "process alive" check works when Chrome crashes and restarts itself. But here, both Chrome and its parent are dead — Chrome just left its locks behind. On next startup, The fix is minimal: read the PID from I don't have a screencast readily available but the reproduction steps above are deterministic — happens every time a persistent-profile MCP session is interrupted non-gracefully. |
|
I am not able to reproduce that:
Observed: Chrome is automatically closed and can be started again. Could you please exact steps to reproduce? Could you confirm that the chrome-devtools-mcp server is actually killed/terminated? Related #1765 (comment) |
|
So I think we should not handle the process checks for Chrome, let's find an exact scenario where this fails in an issue before revisiting this PR. |
Problem
When an MCP client (or its host process) crashes or is killed without cleanly shutting down the browser, Chrome's singleton lock files (
SingletonLock,SingletonSocket,SingletonCookie) persist in the user data directory. On the next startup, Chrome refuses to launch because it believes another instance is already running:This is particularly disruptive for MCP clients that manage the browser lifecycle automatically. The only recovery options are manually deleting the lock files or using
--isolated(which loses all session state, cookies, and logins). Users who rely on a persistent Chrome profile for authenticated workflows are stuck — they either lose their login state every time, or they have to manually intervene after every crash.Reproduction
Solution
This PR adds a
clearStaleSingletonLock()helper that runs beforepuppeteer.launch()when a persistent user data directory is in use.How it works:
SingletonLocksymlink in the user data directory (Chrome writes it ashostname-PID)process.kill(pid, 0)SingletonLock,SingletonSocket, andSingletonCookieso Chrome can start cleanlyThis is a safe, minimal change:
--isolatedbehavior is unaffectedTest plan
tsc --noEmitpassesmacbook-pro-16---m1-max.home-85428) → PID correctly extracted