plasma-shutdown: wait for the session stop job before calling logind
Original commit message(s)
- plasma-shutdown: wait for the session stop job before calling logind
StopUnit only enqueues a job and returns as soon as it is queued, not when the units have stopped. logoutComplete() then immediately asks logind to power off or reboot. logind stops the session scope, which on X11 kills sddm-helper, so sddm tears down the X server while units in graphical-session.target are still handling their SIGTERM.
For kwin_x11 with the NVIDIA driver that is fatal: it loses the X connection while tearing down its GL context, prints "XIO: fatal IO error" and never exits. Every shutdown then stalls until plasma-kwin_x11.service hits TimeoutStopSec (90 s by default):
23:15:24 systemd[2406]: Stopping plasma-kwin_x11.service... 23:15:24 systemd[2406]: Stopped target graphical-session.target 23:15:24 systemd-logind: The system will power off now! 23:15:24 sddm: Display server stopping... 23:15:24 kwin_x11: The X11 connection broke: I/O error (code 1) 23:15:24 kwin_x11: XIO: fatal IO error 4 (Interrupted system call) 23:15:54 systemd[2406]: plasma-kwin_x11.service: State 'stop-sigterm' timed out. Killing.
Fix this the way systemctl stop does: Subscribe to the manager, connect to JobRemoved before enqueueing the StopUnit job, and only continue to logind once ListJobs reports the user manager idle. A 10 s fallback timer keeps a unit that ignores SIGTERM from blocking shutdown; after it fires we are exactly where the code was before this change, and the unit is still killed by the user@.service teardown as today.
This completes 6ce8f434 ("Use systemd to handle more of the shutdown"), which moved the session stop to systemd but never waited for the resulting job. The non-systemd fallback path is unchanged.
The race exists on every session type. On Wayland kwin owns the display, so nothing external kills it mid-stop and the change is only an ordering correction with no visible effect. With this change kwin_x11 stops in about 200 ms and logind is called about 10 ms later.
BUG: 501433 FIXED-IN: 6.7.5
Reason for the change
Shutdown::logoutComplete() calls StopUnit("graphical-session.target") and, as soon as that D-Bus call returns, asks logind to power off or reboot (shutdown.cpp lines 152-158 and 171-172 on Plasma/6.7). StopUnit returns when the job is enqueued, not when the units have stopped. logind then stops the session scope; on X11 that kills sddm-helper, and sddm tears down the X server while every unit in graphical-session.target is still handling its SIGTERM.
This MR makes plasma-shutdown do what systemctl stop does: Subscribe, connect to JobRemoved before enqueueing the stop job, and continue to logind only once ListJobs reports the user manager idle. A 10 s fallback timer keeps a unit that ignores SIGTERM from blocking shutdown; past that point the behaviour is exactly what it is today.
Symptom (X11, NVIDIA)
kwin_x11 loses the X connection while tearing down its GL context and never exits. Every shutdown from the Plasma menu then stalls until plasma-kwin_x11.service reaches TimeoutStopSec (90 s by default). Debian 13, plasma-workspace 6.3.6, kwin-x11 6.3.6, RTX 5060 Ti + RTX 3060, nvidia-driver 590.48.01, TimeoutStopSec lowered to 30 s here:
23:15:24 dbus-daemon: Activating 'org.kde.Shutdown' requested by ksmserver-logout
23:15:24 systemd[2406]: Stopped target plasma-workspace.target
23:15:24 systemd[2406]: Stopping plasma-kwin_x11.service - KDE Window Manager...
23:15:24 systemd[2406]: Stopped target graphical-session.target
23:15:24 systemd-logind: The system will power off now!
23:15:24 systemd[1]: Stopping session-4.scope...
23:15:24 sddm-helper: Signal received: SIGTERM
23:15:24 sddm: Display server stopping...
23:15:24 kwin_x11: QRhiGles2: Failed to make context current. Expect bad things to happen.
23:15:24 kwin_x11: The X11 connection broke: I/O error (code 1)
23:15:24 kwin_x11: XIO: fatal IO error 4 (Interrupted system call) on X server ":0"
23:15:54 systemd[2406]: plasma-kwin_x11.service: State 'stop-sigterm' timed out. Killing.
23:15:54 systemd[2406]: plasma-kwin_x11.service: Killing process 2712 (kwin_x11) with signal SIGKILL.
23:15:56 systemd-shutdown[1]: Syncing filesystems and block devices.This is bug 501433 (filed against kwin, but the trigger is here). Same signature in sddm issues #1783 and #2058.
With this change
X11, same machine, default TimeoutStopSec (90 s), no drop-ins:
22:23:12.645 systemd[2394]: Stopping plasma-kwin_x11.service - KDE Window Manager...
22:23:12.852 systemd[2394]: Stopped plasma-kwin_x11.service - KDE Window Manager.
22:23:12.861 systemd-logind[1278]: The system will power off now!
22:23:12.870 sddm[1752]: Display server stopping...
22:23:16.518 systemd-shutdown[1]: Syncing filesystems and block devices.kwin_x11 stops in 207 ms, logind is called 9 ms after that, 3.9 s from "Stopping" to filesystem sync. Previously the same span was 30 s (90 s with the default timeout).
Wayland, same machine:
23:29:03.228 systemd[2389]: Stopped target graphical-session.target
23:29:03.617 systemd[2389]: Stopping plasma-kwin_wayland.service - KDE Window Manager...
23:29:03.744 systemd[2389]: Stopped plasma-kwin_wayland.service - KDE Window Manager.
23:29:03 systemd-logind: The system will power off now!No "Timed out waiting" from plasma-shutdown in either run, so the JobRemoved path, not the fallback timer, drove the transition.
Why this layer
- This completes 6ce8f434 ("Use systemd to handle more of the shutdown", the 453928 fix). That commit moved the session stop to systemd and read the
StopUnitreply only to detect a non-systemd session; the job path was never waited on. 41629e86 later dropped the reply entirely. Nothing in the history deliberately avoided waiting. - Target choice is already settled: 80f3e0ad switched to
graphical-session.target, which is what makes thePartOf=units (kwin, ksmserver, ...) part of the same transaction and therefore visible toListJobs. This MR keeps that. - Not an X11 fix. The race exists on every session type: logind is asked to kill the session while its stop job is still running. On Wayland kwin owns the display, so nothing external kills it mid-stop and the change is only an ordering correction with no visible effect. It is still what the code intends: stop the session, then power off.
- Worst case unchanged. logind already stops
user@UID.service, which waits for every unit and itsTimeoutStopSec. The wait moves in front of the display teardown instead of behind it. AnyJobRemovedresult (done, failed, canceled, dependency) counts as completion, so a failing unit cannot block shutdown. - Fallback timer exercised in practice. In one run
xdg-document-portal.serviceignored SIGTERM (known upstream bug, xdg-desktop-portal#999, fixed in 1.21.1). kwin_x11 still stopped in 187 ms, plasma-shutdown hit the 10 s fallback and called logind, and the portal was SIGKILLed by theuser@1000.serviceteardown 90 s later, exactly as it would have been without this change. - Process lifetime. plasma-shutdown is D-Bus activated into
dbus.service's cgroup, not part ofgraphical-session.target, so it survives the stop (verified: it logged the fallback warning 9 s after the target was reported stopped). e1b251fa uses its presence to detect a shutdown in progress; staying alive up to 10 s longer is consistent with that. - Why not a logind delay inhibitor:
InhibitDelayMaxSecdefaults to 5 s and a Plasma session can take longer to stop. The job wait has no such cap. - kwin_x11 hanging when X dies is a separate bug (likely in the GLX teardown path under the NVIDIA driver) and out of scope. Asking logind to kill the session mid-stop is wrong regardless of how any single unit reacts; fixing kwin would hide this instance of the race, not remove it.
Test plan
- X11 and Wayland, Debian 13, plasma-workspace 6.3.6 with this patch (the
shutdown.cpponPlasma/6.7is byte-identical to master and differs from 6.3 only in the target name). - Shutdown and reboot from the Plasma menu; logout unaffected (non-halt path goes through
sessionStopped()and callsqApp->quit()as before). - Non-systemd fallback path is unchanged.