fix: restart browser automatically on disconnect in server mode#1610
Open
kitsuyui wants to merge 1 commit into
Open
fix: restart browser automatically on disconnect in server mode#1610kitsuyui wants to merge 1 commit into
kitsuyui wants to merge 1 commit into
Conversation
Code Metrics Report
Details | | main (b5aa171) | #1610 (9ebffc2) | +/- |
|---------------------|----------------|-----------------|-------|
- | Coverage | 87.5% | 83.7% | -3.8% |
| Files | 11 | 11 | 0 |
| Lines | 177 | 185 | +8 |
| Covered | 155 | 155 | 0 |
- | Code to Test Ratio | 1:0.5 | 1:0.5 | -0.1 |
| Code | 1428 | 1442 | +14 |
| Test | 819 | 819 | 0 |
| Test Execution Time | 6s | 6s | 0s |Code coverage of files in pull request scope (75.0% → 62.5%)
Reported by octocov |
f9dd3b0 to
2d8ac86
Compare
When the Playwright browser crashes or disconnects (OOM, runtime error), listen for the 'disconnected' event and restart it automatically. The server handler reads from a mutable browser reference via a getter closure, so in-flight requests pick up the new browser after restart. Also rename createServer/createHandler to accept a browser getter (() => Browser) instead of a direct Browser instance, enabling the mutable reference to be shared across reconnects.
2d8ac86 to
18ffffe
Compare
gh-counterPR gate
Repo dashboard
Reported by gh-counter |
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.
Summary
main()shared one PlaywrightBrowserinstance across all requests with no recovery path when it disconnected (OOM, runtime crash, Playwright error). Once dead, every request returned 502 until a manual server restart.'disconnected'event listener on the browser that automatically restarts it and re-registers the listener. The server handler reads the current browser through a() => Browsergetter closure, so the new instance takes effect without restarting the HTTP server.createHandlerandcreateServernow accept agetBrowserFn: () => Browserparameter instead of a directBrowserinstance, enabling the mutable reference to propagate on reconnect.Changed files
src/server/index.ts— browser auto-restart logic inmain(), updatedcreateHandler/createServersignaturessrc/server/index.spec.ts— updated test callsites to passgetBrowserFn: () => browserTest plan
npx tsc --noEmit— type-checks passnpx biome check .— no lint errorsnpx vitest run src/lib/headers.spec.ts src/index.spec.ts— unit tests passsrc/server/index.spec.ts— server still responds correctly via the newgetBrowserFnAPITrade-offs
getBrowser()throws during a restart attempt, the error is swallowed and subsequent requests will fail until the next disconnect fires. This keeps the server alive but may silently delay recovery. A future improvement could add a retry loop or health-check endpoint that reflects browser state.createServer({ browser })→createServer({ getBrowserFn })is a breaking change for direct callers ofcreateServer; all in-repo usages have been updated.