Skip to content

fix(serve): dismiss a dialog that opened while no client was attached - #91

Merged
tenequm merged 3 commits into
mainfrom
fix/orphan-dialog-attach
Sep 19, 2026
Merged

tenequm merged 3 commits into
mainfrom
fix/orphan-dialog-attach

Conversation

@tenequm

@tenequm tenequm commented Sep 19, 2026 •

Copy link
Copy Markdown
Member

Problem

A native dialog that opens while no CDP client is attached (a page timer that fires after cuttle pw detach, or after the driver dropped) is seen by no session, so the dismiss-on-disconnect from #87 never runs. Chrome binds a pending dialog to the DevTools sessions that were Page-enabled when it opened: a later client's Page.enable waits on the blocked renderer, and its Page.handleJavaScriptDialog gets "No dialog is showing". Every later cuttle pw auto-attach hung until playwright's 30s timeout, and only closing the tab recovered.

Fix

Each launched browser now gets a dialogWatch: the daemon's own browser-level CDP connection that auto-attaches to every page (existing and future, pages only), enables Page on each, and tracks which tabs have a dialog showing. When a client connects to a seed that had no other client, serveWS has the watch dismiss those dialogs before the client is proxied, including any a dismissal sets off (same rounds as #87). The watch reuses #87's openDialogs tracking and dismissal rounds, and writes on a context detached from the connecting client (coder/websocket closes the whole connection when a write's context ends).

An idle reap or shutdown also has the watch dismiss any dialog before the teardown capture: an orphaned dialog otherwise held the capture for the full 30s captureTimeout with the seed lock held (measured live in pool mode: 30s before, 0.27s after).

It does not dismiss at open time: with no client attached the dialog may be the person's in the viewer, and with a client attached it is that client's to answer (it still surfaces to playwright as a modal state). Works the same in session and pool mode, since every instance comes from spawn. Best-effort: a browser the watch cannot dial simply has none.

Verification

Live, against an image built from this branch, with a page doing setTimeout(() => alert('x'), 3000): pw goto, pw detach, wait 6s, pw snapshot.

  • Before: pw snapshot failed after 32s (playwright attach timeout).
  • After: pw snapshot returned the page in 3s; the log shows dismissing 1 native dialog(s) no client answered.
  • Same result for a dialog in a tab opened after launch (pw tab-new).
  • With the driver attached when the alert fires, pw snapshot still reports Modal state: ["alert" dialog with message "x"] and pw dialog-dismiss clears it.
  • Pool mode (--mode=pool --idle-timeout=10, raw CDP client per seed): the next attach's Page.enable answers in ~260ms after an orphaned dialog; after an idle reap the relaunched browser gets a fresh watch and the same fix holds.
  • With no client, the dialog stays up (the page title set after alert() does not change in /json/list) until the next client connects.

New unit test TestDialogWatchDismissesADialogNoClientSaw drives the watch against the fake browser harness; it fails when dismissal is disabled. just check passes.

Release notes

A native dialog (alert, confirm, "Leave site?") that opens while no driver is attached no longer hangs the next cuttle pw command for 30s: cuttle dismisses it when the next client connects. A dialog that opens while a driver is attached still reaches the driver.

@tenequm
tenequm force-pushed the fix/orphan-dialog-attach branch 2 times, most recently from ecc9f0f to cc54111 Compare September 19, 2026 00:22
…r's context

The watch now tracks dialogs with openDialogs.observe/take and shares its dismissal rounds, fires its commands without a reply map, prefilters frames before decoding, and writes on a context detached from the connecting client: coder/websocket closes the whole connection when a write's context ends, so a client leaving mid-dismissal ended the watch for the rest of that browser's life.
An orphaned dialog held an idle reap's state capture for the full 30s captureTimeout, with the seed lock held, so a reconnect to that seed waited too. The browser is going away, so its dialogs are dismissed first.
@tenequm
tenequm force-pushed the fix/orphan-dialog-attach branch from cc54111 to 8f581ec Compare September 19, 2026 00:28
@tenequm
tenequm merged commit 10b060b into main Sep 19, 2026
9 checks passed
@tenequm
tenequm deleted the fix/orphan-dialog-attach branch September 19, 2026 00:33
tenequm added a commit that referenced this pull request Sep 19, 2026
## Problem

#91 has the dialog watch dismiss an orphaned dialog before the teardown
capture, on an idle reap and on shutdown. On a signal shutdown (`cuttle
down`, `docker stop`) that dismissal never landed. The watch's read loop
ran on `pool.baseCtx`, which is the daemon's `signal.NotifyContext`
context. coder/websocket closes a connection when a `Read`'s context
ends, so SIGTERM closed every watch before `pool.shutdown()` reached
`captureAndTerminate`. The dismissal then wrote to a closed socket and
dropped the error, while still logging `dismissing 1 native dialog(s)`.
The capture sat on the dialog until the shared `shutdownCaptureBudget`
ran out, which cost every other seed its final snapshot as well.

## Fix

The watch now uses `ctx` only to bound its dial. Its read loop runs on
`context.WithoutCancel(ctx)`, so it ends when the browser's connection
does. Chrome's exit (terminate, SIGKILL after grace, or a crash) closes
the loopback socket, so the watch cannot outlive its browser.

## Verification

Live test, headed session mode, one image per side. Open
https://example.com, write a localStorage key, schedule `setTimeout(()
=> alert('x'), 3000)`, detach, wait 6s, then `docker stop -t 40`.

- main: the stop took 8.4s and logged `shutdown: 8s budget spent before
every seed was snapshotted`.
- this branch: the stop took 0.55s, and the capture completed 0.26s
after the dismissal.
- The #91 attach path still works on this branch: after an orphaned
dialog, the next client's `Page.enable` answers in 1ms.

`just check` and `go test -race -count=5 ./internal/serve/` pass.

## Release notes

`cuttle down` or a container stop no longer waits out its whole snapshot
budget when a native dialog opened while no driver was attached. The
dialog is dismissed before the final capture, so every seed's final
state is saved.
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