Repository navigation
fix shiny app window becoming unclosable and orphaned - #17920
Conversation
When a page hosted in a desktop window registers a beforeunload handler
that prevents unload (e.g. a Shiny app warning that state will be lost),
Chromium cancelled the window close with no UI at all, leaving the
window seemingly impossible to close. Worse, the satellite had already
unregistered itself from GwtCallback during the close attempt, so the
still-open window could no longer make desktop callbacks ("Received
callback desktop_allow_navigation from unknown window") and stayed
orphaned in a grayed-out state across subsequent app runs.
Now handle 'will-prevent-unload' by showing a "Leave page?"
confirmation (mirroring browser behavior) and forcing the close when
the user confirms, and only unregister satellite windows once they are
actually destroyed. RSTUDIO_DESKTOP_IGNORE_BEFOREUNLOAD=1 skips the
dialog for automation; the e2e harness sets it.
Addresses #17439, #17502
🍎 macOS Desktop E2E · ARM64✅ All tests passed!
📥 Download Playwright report⬇️ playwright-report.zip — extract and open
2 shards · macos-26 ARM64 · commit |
Review summaryFour specialized agents reviewed the diff (code quality, tests, error handling, comments), and I independently adjudicated the one finding where they disagreed. The two source-code fixes are correct and well-reasoned — the Electron Critical issuesNone. No correctness, security, or silent-failure defect in the fix. The Leave/Stay/env-bypass branches map correctly to Important issues1. The third e2e test's "fails without this fix" claim — most likely TRUE (one agent thought otherwise; worth confirming).
2. The orphaning symptom (fix #2) is not asserted end-to-end. 3. The source-window cancel path — which the PR description explicitly claims to fix — has no test coverage. Suggestions
Recommended action
Verdict: the fix is merge-quality; the remaining work is hardening the tests around it (gaps #2 and #3) and the quick empirical check on #1. |
gtritchie
left a comment
There was a problem hiding this comment.
LGTM, usual fit & finish suggestions on comments, tests, etc.
- add unit test for the source-window cancel path (window stays registered when the user cancels the close) - destroy test windows before clearing the app singleton so the satellite 'closed' handler doesn't crash mocha teardown - add debug logging to the will-prevent-unload handler - clarify e2e comments (CDP dialog is version-dependent; cite the onbeforeunload example from #17502)
|
Thanks for the thorough review! Evaluated each finding; resolution in 9944a7f: Finding #1 (does the third e2e test really fail pre-fix?) -- confirmed empirically. Reverted both source files to their pre-fix state and ran the third test against the dev build: it fails with Finding #2 (orphaning symptom not asserted e2e) -- accurate as a gap description, but the proposed relaunch assertion can't actually catch a fix-#2-only regression: with Finding #3 (source-window cancel path untested) -- agreed; added the suggested unit test: source-prefixed window name, Suggestions -- all applied: comment softening, aligned the Bonus goblin the review's "run npm test before merge" advice flushed out:
|
The GWT SatelliteManager in the main window learns that a satellite has closed from the satellite's own JS unload handler, but that notification can lose the race with window destruction (and window proxies can report as open even after close; see case 4436). A stale entry makes the next open of a same-named satellite "reactivate" the dead window instead of creating a new one -- e.g. re-running a Shiny app shortly after stopping it could leave R busy serving an app with no window at all, as seen in the shiny-app-window-close e2e failure on CI. Now the Electron main process authoritatively calls unregisterDesktopChildWindow in the main window once the satellite window is actually destroyed; the 'closed' event never fires for a cancelled close, so this cannot reintroduce the eager-unregister bug fixed previously on this branch.
On CI, the interruptR in the first test could land while shiny was servicing a request; shiny's error handling swallows the interrupt, bouncing the websocket (so the window still closes and the test passes) without stopping runApp(). R then stays busy forever, the next test's runApp sits queued behind it, no satellite ever opens, and the client escalates to a blocking "Terminate R" dialog. Ensure the console actually returns to idle after the window closes, re-requesting the interrupt if needed. Also absorb two unrelated CI flake sources observed in the same runs: wait out a main-window reload left in flight by a preceding spec before driving prefs (and make setPref/clearPref wait for the bridge like executeCommand already does), and disable startup update checks in the e2e sandbox since runners have no route to the update server.
Problem
With Run App > Run in Window, the Shiny app window could become impossible to close: clicking X grayed the window out (the app was stopped) but the window stayed open, never refreshed on subsequent runs, and could only be removed by quitting RStudio.
Root cause: when a page registers a
beforeunloadhandler that prevents unload (e.g.window.onbeforeunload = function() { return 'Your changes will be lost!'; }, as the app in #17502 does), Chromium cancels the window close. A regular browser shows a "Leave site?" confirmation in this situation, but RStudio Desktop had nowill-prevent-unloadhandler, so the close was cancelled silently -- every time, with no way to override it.A second bug then orphaned the window permanently:
SatelliteWindow.closeEvent()unregistered the window fromGwtCallbackduring the close attempt, so once a close was cancelled, the still-live window could no longer make desktop callbacks. This is theReceived callback desktop_allow_navigation from unknown windowerror in the reporter's logs, and why the window stayed gray and could not display a re-run app.Fix
will-prevent-unloadinDesktopBrowserWindow: show a native "Leave page?" confirmation mirroring browser behavior, and force the close if the user chooses Leave. (Chromium does not expose the page's custombeforeunloadmessage, so a generic message is the best we can do.) Strings are localized (en/fr).GwtCallbackonly on theclosedevent (window actually destroyed), so a cancelled close no longer orphans the window. This also fixes premature unregistration on the source-window close path when the user cancels an unsaved-changes prompt.RSTUDIO_DESKTOP_IGNORE_BEFOREUNLOAD=1skips the dialog and forces the close; the e2e harness sets it since automation cannot dismiss native modals.Tests
will-prevent-unloadchoices (leave/stay/env bypass) and for unregister-only-on-destroy.e2e/rstudio/tests/shiny/shiny-app-window-close.test.ts(@desktop_only) covering: window closes when the app is stopped, when closed while running, and -- the regression -- when the app's iframe registers a close-preventingbeforeunloadhandler. The third test fails without this fix.Note: if the user chooses "Stay", the app will typically already have been stopped, since the satellite close notification (which stops the app) is sent before Chromium runs
beforeunload. The window remains open and closable; reordering that is a deeper change to the satellite close protocol left out of scope here.Fixes #17439.
Fixes #17502.