fix(browser): clear stale Chrome singleton lock before launch#1046
Open
RaviTharuma wants to merge 2 commits intoChromeDevTools:mainfrom
Open
fix(browser): clear stale Chrome singleton lock before launch#1046RaviTharuma wants to merge 2 commits intoChromeDevTools:mainfrom
RaviTharuma 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. |
Author
|
I have signed the CLA. |
Collaborator
|
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. |
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.
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