fix(browser): reattach to the running browser on startup - #2196
Merged
Conversation
The browser keeps its remote debugging preference across its own restarts, and keeps an approved connection grant for as long as it stays running. BitFun's connection registry, however, is process-wide state, so restarting BitFun dropped the connection even though there was nothing on the browser side left to redo — Settings reported "not connected" until something asked for the browser again. Reattach at startup when, and only when, there is a live endpoint to attach to. This never starts a browser and never opens a settings page, and a denial or approval timeout is logged rather than surfaced, since the user did not ask for this particular connection. Settings also could not tell "ready, nothing attached yet" apart from "nothing to attach to" — both read as "not connected", which looks broken when the browser is in fact set up correctly. Report the ready state separately, and resolve it from a single endpoint probe that also answers whether the persistent setting is on. That probe is a file read plus a short local TCP connect, so unlike attaching it never prompts the browser.
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.
Follow-up to #2195.
The problem
Restart BitFun with default CDP already enabled and Settings shows 未连接 / Not connected, with a Connect button you have to click again — even though nothing on the browser side needs redoing.
BROWSER_CONNECTIONSis a process-widestaticincdp_client.rs, so a BitFun restart empties it. Meanwhile the browser still has the preference on, the port still listening, and the approval grant still in place.What the browser actually persists
Checked against a real Chrome 151 profile:
Local State→devtools.remote_debugging.user-enabled: true— the preference survives browser restartsPreferences→ no record of approved clients — the grant is not written to diskDevToolsActivePortpresent and the port reachable while the browser runsSo the grant lives in the browser process. While the browser stays up, reattaching costs nothing; a prompt only appears on the first connection after a browser restart, which any connect path has to pay anyway.
Worth noting the port is dynamic (a throwaway profile came up on 49160, not 9222), which is why
DevToolsActivePortis the only reliable source — as the existing code already assumes.Changes
Reattach at startup —
browser_control_api::init_on_startup(), spawned fromlib.rsalongsideremote_connect_api::init_on_startup(). Deliberately conservative:DevToolsActivePortexists and the port is reachableHonest status —
cdp_available: falsewas rendered as "not connected" whether the browser was ready or absent. Newbrowser_readysplits them:Google Chrome · N tabsdefault_cdp_enabledandbrowser_readynow come from one endpoint probe instead of two per status refresh. The probe is a file read plus a 150ms local TCP connect — no WebSocket handshake, so unlike attaching it never prompts the browser (verified against a live profile: no dialog).Verified
Against the reporter's live Chrome:
port=9222 path_shape_ok=True tcp_reachable=True→browser_ready= true, i.e. the panel that said "not connected" now reads "ready, connects on use", and startup will attach to it.Also confirmed by direct experiment that a WebSocket handshake from a never-approved client blocks until someone clicks Allow (10s timeout with nobody clicking) — which is what makes the grant's process lifetime the deciding factor above.
cargo check -p bitfun-desktop,tsc --noEmit(0 errors), locale JSON validated foren-US/zh-CN/zh-TW. End-to-end startup reattach has not been run in a packaged build yet; the log line to look for isReattached to the running Google Chrome profile on startup.