[Linux] Quit cleanly when the desktop session ends, using the session monitor of the Inhibit portal
Categories
(Core :: Widget: Gtk, defect)
Tracking
()
| Tracking | Status | |
|---|---|---|
| firefox163 | --- | fixed |
People
(Reporter: mmitar, Assigned: mmitar)
References
Details
Attachments
(1 external file)
When a desktop session ends, at logout, shutdown or reboot, Firefox does not learn about it. It keeps running until the session manager stops it: on GNOME, gnome-session ends and systemd stops the application's scope with SIGTERM, at the same time as GNOME Shell. So even with the SIGTERM handler working (bug 2086801), Firefox quits while the compositor goes away, and GTK ends a process whose Wayland connection breaks. Without the handler, Firefox is killed at once, and every logout and shutdown counts as a crash for session restore (bug 1017362).
xdg-desktop-portal tells applications about the end of the session beforehand, through the session monitor of org.freedesktop.portal.Inhibit (version 3): CreateMonitor() creates a session whose StateChanged signal reports session-state 2 (query end), which the application acknowledges with QueryEndResponse(), and then 3 (ending), while the session still runs. GTK 4's GtkApplication quits on "ending", Chromium does since 159 (https://issues.chromium.org/issues/580443832), and GNOME points applications to the portal for this (https://gitlab.gnome.org/GNOME/gnome-session/-/work_items/195). Firefox already uses the same portal interface for its wake locks (bug 1860269).
The patch for this bug, on top of the one in bug 2086801, creates a session monitor in the parent process at startup, in XREMain::XRE_mainRun() next to installing the SIGTERM handler. It answers "query end" at once, as nothing in Firefox keeps the session from ending, and on "ending" it calls nsAppShell::ScheduleQuitEvent(), which the SIGTERM handler calls too. The session is identified by the handle which the portal derives from the caller's unique name and the session_handle_token, so StateChanged signals are recognized also when they arrive before CreateMonitor() returns.
Tested with 159.0.1 and 160.0 in a GNOME 50 session on Ubuntu 26.04, with xdg-desktop-portal-gtk serving the Inhibit portal. At logout, Firefox receives "ending" and starts quitting, which takes it about half a second. xdg-desktop-portal-gtk answers gnome-session's EndSession at once, though, so systemd stops Firefox's scope and GNOME Shell about 60 ms after "ending", while Firefox is still quitting: its child processes are killed, and once it left a minidump. With a local change to xdg-desktop-portal-gtk which answers EndSession only once the applications which monitor the session have closed their monitor sessions, Firefox finished quitting, with sessionstore.jsonlz4 written, before GNOME Shell shut down in 20 of 20 logouts, and the next start restored the session as configured instead of recovering from a crash. That change is for the portal backend, not Firefox.
Open questions:
- The shutdown reason is AppClose, as for any quit. Bug 1827643 asks whether Linux can tell the reason; this is the place which knows it is OSSessionEnd.
- Users who do not restore the previous session at startup so far had their windows restored after a logout, through the recovery after a crash. With a clean quit they see their start page, as with Chromium after a logout. Session end could instead be treated like "os-restart" on Windows, which sets browser.sessionstore.resume_session_once.
- Thunderbird shares the code and would quit at the end of the session too (bug 2067505).
Firefox learned about the end of the desktop session only from the SIGTERM
which the session manager sends when it stops the application, together
with the compositor. xdg-desktop-portal reports the end of the session
beforehand, to applications which create a session monitor with
org.freedesktop.portal.Inhibit.CreateMonitor().
The parent process now creates such a monitor at startup, next to
installing the SIGTERM handler. It answers "query end" at once, as nothing
in Firefox keeps the session from ending, and on "ending" it calls
ScheduleQuitEvent(), which the SIGTERM handler calls too, so Firefox saves
its session and quits while the session still runs.
The portal derives the handle of the monitor's session from the
connection's unique name and the session_handle_token, so StateChanged
signals are recognized also when they arrive before CreateMonitor()
returns.
Updated•19 days ago
|
Updated•17 days ago
|
Updated•16 days ago
|
Comment 2•9 days ago
|
||
We can land it when Bug 2086801 is fixed, just ping me to land it.
Thanks.
Comment 5•4 days ago
|
||
Backed out for causing build bustages @ nsNativeAppSupportUnix.cpp
Backout link: https://hg.mozilla.org/integration/autoland/rev/3a2ac168ccba9a7e5d2e2bcc8ef83fd270c9506f
I fixed that patch. Please land it again.
Comment 8•3 days ago
|
||
| bugherder | ||
Description
•