Skip to content

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

Merged
kitsuyui merged 1 commit into
mainfrom
fix/audit-browser-dead-state-no-restart-001
Jul 9, 2026
Merged

fix: restart browser automatically on disconnect in server mode#1610
kitsuyui merged 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.

@kitsuyui
kitsuyui force-pushed the fix/audit-browser-dead-state-no-restart-001 branch 2 times, most recently 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 (88661b5) #1610 (e5896b4) +/-
TODO/expect-error 4 4 0

Reported by gh-counter

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 18ffffe to 3c28eb2 Compare July 9, 2026 01:42
@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown

Code Metrics Report

main (88661b5) #1610 (e5896b4) +/-
Coverage 87.1% 84.1% -3.0%
Code to Test Ratio 1:0.5 1:0.5 -0.1
Test Execution Time 12s 10s -2s
Details
  |                     | main (88661b5) | #1610 (e5896b4) |  +/-  |
  |---------------------|----------------|-----------------|-------|
- | Coverage            |          87.1% |           84.1% | -3.0% |
  |   Files             |             11 |              11 |     0 |
  |   Lines             |            226 |             234 |    +8 |
  |   Covered           |            197 |             197 |     0 |
- | Code to Test Ratio  |          1:0.5 |           1:0.5 |  -0.1 |
  |   Code              |           1760 |            1775 |   +15 |
  |   Test              |           1003 |            1003 |     0 |
+ | Test Execution Time |            12s |             10s |   -2s |

Code coverage of files in pull request scope (76.9% → 66.6%)

Files Coverage +/- Status
src/server/index.ts 66.6% -10.3% modified

Reported by octocov

@kitsuyui
kitsuyui merged commit de60997 into main Jul 9, 2026
10 checks passed
@kitsuyui
kitsuyui deleted the fix/audit-browser-dead-state-no-restart-001 branch July 9, 2026 10:24
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