Skip to content

fix(browser): reattach to the running browser on startup - #2196

Merged
bobleer merged 1 commit into
GCWing:mainfrom
bobleer:bob/browser-cdp-startup-reattach
Aug 10, 2026
Merged

fix(browser): reattach to the running browser on startup#2196
bobleer merged 1 commit into
GCWing:mainfrom
bobleer:bob/browser-cdp-startup-reattach

Conversation

@bobleer

@bobleer bobleer commented Aug 10, 2026

Copy link
Copy Markdown
Collaborator

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_CONNECTIONS is a process-wide static in cdp_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 Statedevtools.remote_debugging.user-enabled: truethe preference survives browser restarts
  • profile Preferencesno record of approved clients — the grant is not written to disk
  • DevToolsActivePort present and the port reachable while the browser runs

So 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 DevToolsActivePort is the only reliable source — as the existing code already assumes.

Changes

Reattach at startupbrowser_control_api::init_on_startup(), spawned from lib.rs alongside remote_connect_api::init_on_startup(). Deliberately conservative:

  • only when DevToolsActivePort exists and the port is reachable
  • never starts a browser, never opens a settings page
  • no-ops if a connection is already held
  • a denial or approval timeout is logged, not surfaced — the user did not ask for this particular connection

Honest statuscdp_available: false was rendered as "not connected" whether the browser was ready or absent. New browser_ready splits them:

state shown
attached Google Chrome · N tabs
running, enabled, reachable, not attached Ready, connects on use
nothing to attach to Not connected

default_cdp_enabled and browser_ready now 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=Truebrowser_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 for en-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 is Reattached to the running Google Chrome profile on startup.

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.
@bobleer
bobleer merged commit b30cda4 into GCWing:main Aug 10, 2026
5 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant