Skip to content

fix: restart browser automatically on disconnect in server mode#1610

Open
kitsuyui wants to merge 1 commit into
mainfrom
fix/audit-browser-dead-state-no-restart-001
Open

fix: restart browser automatically on disconnect in server mode#1610
kitsuyui wants to merge 1 commit into
mainfrom
fix/audit-browser-dead-state-no-restart-001

Conversation

@kitsuyui

@kitsuyui kitsuyui commented Jun 3, 2026

Copy link
Copy Markdown
Owner

Summary

  • The server's main() shared one Playwright Browser instance 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.
  • Added a 'disconnected' event listener on the browser that automatically restarts it and re-registers the listener. The server handler reads the current browser through a () => Browser getter closure, so the new instance takes effect without restarting the HTTP server.
  • createHandler and createServer now accept a getBrowserFn: () => Browser parameter instead of a direct Browser instance, enabling the mutable reference to propagate on reconnect.

Changed files

  • src/server/index.ts — browser auto-restart logic in main(), updated createHandler/createServer signatures
  • src/server/index.spec.ts — updated test callsites to pass getBrowserFn: () => browser

Test plan

  • npx tsc --noEmit — type-checks pass
  • npx biome check . — no lint errors
  • npx vitest run src/lib/headers.spec.ts src/index.spec.ts — unit tests pass
  • Integration tests src/server/index.spec.ts — server still responds correctly via the new getBrowserFn API

Trade-offs

  • Restart race: if 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.
  • API change: createServer({ browser })createServer({ getBrowserFn }) is a breaking change for direct callers of createServer; all in-repo usages have been updated.

@github-actions

github-actions Bot commented Jun 3, 2026

Copy link
Copy Markdown

Code Metrics Report

main (b5aa171) #1610 (9ebffc2) +/-
Coverage 87.5% 83.7% -3.8%
Code to Test Ratio 1:0.5 1:0.5 -0.1
Test Execution Time 6s 6s 0s
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%)

Files Coverage +/- Status
src/server/index.ts 62.5% -12.5% modified

Reported by octocov

@kitsuyui kitsuyui force-pushed the fix/audit-browser-dead-state-no-restart-001 branch from f9dd3b0 to 2d8ac86 Compare June 4, 2026 15:55
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.
@kitsuyui kitsuyui force-pushed the fix/audit-browser-dead-state-no-restart-001 branch from 2d8ac86 to 18ffffe Compare June 5, 2026 17:26
@github-actions

github-actions Bot commented Jun 5, 2026

Copy link
Copy Markdown

gh-counter

PR gate

Removed Added +/-
TODO/expect-error 0 0 0

Repo dashboard

main (b5aa171) #1610 (9ebffc2) +/-
TODO/expect-error 4 4 0

Reported by gh-counter

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