fix(browser): wait for CDP readiness after start to avoid PortInUseError races (#21149)#29538
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9e2bfddccc
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
src/browser/server-context.ts
Outdated
| // launch again on the same port. Poll briefly so browser(action="start"/"open") is stable. | ||
| const maxAttempts = 50; | ||
| for (let attempt = 0; attempt < maxAttempts; attempt++) { | ||
| if (await isReachable(1200)) { |
There was a problem hiding this comment.
Bound CDP post-launch wait by wall-clock time
The new post-launch loop can block browser(action="start"/"open") much longer than intended when Chrome never becomes CDP-ready: each iteration calls isReachable(1200), and for loopback profiles that translates to up to a 2s WS handshake timeout before the extra 100ms sleep, repeated 50 times. In cases where /json/version responds but the WS endpoint never opens, this path can stall tool calls for roughly 100s+ instead of failing quickly; consider enforcing a real elapsed-time deadline (or reducing per-attempt timeout) to keep startup failures bounded.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Thanks — good catch. The original post-launch loop was bounded by attempt count, but each attempt could block on isReachable(1200) (loopback WS handshake), so in the worst case it could stall browser(action="start"/"open") far longer than intended.
I updated the wait to be bounded by a wall-clock deadline (~8s) and reduced per-attempt timeouts, so startup failures fail fast while still avoiding the follow-up PortInUseError race. Added/adjusted the regression test to cover the post-launch readiness wait behavior.
Greptile SummaryAdded polling for Chrome CDP readiness after launch to prevent race conditions where follow-up browser calls (snapshot/screenshot) attempt to launch Chrome again on an already-in-use port, causing
Confidence Score: 5/5
Last reviewed commit: 9e2bfdd |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3b2857199f
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| const launched = await launchOpenClawChrome(current.resolved, profile); | ||
| attachRunning(launched); | ||
| try { | ||
| await waitForCdpReadyAfterLaunch(); |
There was a problem hiding this comment.
Reconcile start/open request timeout with extra CDP wait
ensureBrowserAvailable() now always waits for waitForCdpReadyAfterLaunch() after spawning Chrome, which can add up to 8s (plus cleanup) on top of launchOpenClawChrome()'s existing 15s startup wait in src/browser/chrome.ts, but the client-side browserStart/browserOpenTab calls still abort at 15s in src/browser/client.ts. In slow-start scenarios (for example, /json/version becomes reachable near the end of the 15s launch window and WebSocket readiness trails by a few seconds), the request will time out and surface a transport error even though the browser process may become ready shortly after, which is a user-visible regression for browser(action="start"|"open").
Useful? React with 👍 / 👎.
3b28571 to
d12dfe0
Compare
|
Landed via temp rebase onto main.
Thanks @AaronWander! |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d12dfe0ddb
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| const launched = await launchOpenClawChrome(current.resolved, profile); | ||
| attachRunning(launched); | ||
| try { | ||
| await waitForCdpReadyAfterLaunch(); |
There was a problem hiding this comment.
Increase browser start/open request timeout for extra CDP wait
After launchOpenClawChrome() returns, this new waitForCdpReadyAfterLaunch() adds up to ~8s of extra blocking, but the local browser clients still time out at 15s (browserStart/browserOpenTab in src/browser/client.ts), while launchOpenClawChrome() itself already spends up to 15s waiting for /json/version (src/browser/chrome.ts). In slow-start cases where HTTP comes up near the end of that 15s window and WS readiness lags slightly, browser(action="start"|"open") now fails with a client timeout even though Chrome becomes ready moments later.
Useful? React with 👍 / 👎.
Summary
Change Type (select all)
Scope (select all touched areas)
Linked Issue/PR
User-visible / Behavior Changes
Security Impact (required)
Repro + Verification
Environment
Steps
Expected
Actual
Evidence
Human Verification (required)
Compatibility / Migration
Failure Recovery (if this breaks)
Risks and Mitigations