Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A journey to resolve the xdg-autostart-generator issue

October 20, 2023

A journey to resolve the xdg-autostart-generator issue

After updating KDE Plasma 5.25, several applications do not start automatically when a user login a desktop session on both openSUSE Tumbleweed and Leap 15.5. This issue affects input methods such as IBus and Fcitx, which are essential for us.

The cause of this problem is the systemd-xdg-autostart-generator. systemd-xdg-autostart-generator is a new mechanism to launch applications listed in autostart directories (e.g., /etc/xdg/autostart) at session startup. Instead of a desktop environment, systemd launches those applications as user services. Plasma adopted and started to use this mechanism by default since version 5.25.

However, due to the following problems, systemd-xdg-autostart-generator cannot start desktop files derived from a former version of openSUSE.

1. systemd-xdg-autostart-generator inappropriately ignores desktop files with X-GNOME-Autostart-Phase key
2. systemd-xdg-autostart-generator kills daemonized applications

In this talk, I will explain the following topics:

* A basic concept of Systemd user service and Systemd generator
* Technical details of those bugs of systemd-xdg-autostart-generator
* A root cause of Bug 2 and how state transition of a systemd service happens inside Systemd.
* A story until this problem is fixed in Leap 15.5


October 20, 2023


  1. 2023-11-21 2/17 The xdg-autostart-generator issue? • In Jul., 2022, I

    received a report that IBus does not work under Tumbleweed – ibus-daemon did not start after login at all – No changes in IBus side – Only under KDE environment • KDE Plasma just started to use xdg-autostart-generator for XDG Autostart • Also affects other applications – fcitx, pkcs11-register, spice-vdagent, dropbox, ... https://bugzilla.opensuse.org/show_bug.cgi?id=1201421
  2. 2023-11-21 3/17 XDG Autostart • A mechanism to start applications

    automatically after desktop login • Applications specified in *.desktop files in – /etc/xdg/autostart – ~/.config/autostart • Each desktop environment launches those application
  3. 2023-11-21 4/17 systemd-xdg-autostart-generator • A new mechanism/implementation for XDG autostart

    • Start applications by using Systemd as user services • How? Convert *.desktop files dynamically to *.service files – /var/run/${uid}/systemd/generator.late/app-${name}@autostart.service
  4. 2023-11-21 5/17 An example of an auto-generated service file #

    Automatically generated by systemd-xdg-autostart-generator [Unit] Documentation=man:systemd-xdg-autostart-generator(8) SourcePath=/etc/xdg/autostart/ibus-autostart.desktop PartOf=graphical-session.target Description=IBus After=graphical-session.target [Service] Type=exec ExecStart=:/usr/bin/ibus-autostart Restart=no TimeoutSec=5s Slice=app.slice [Desktop Entry] Name=IBus GenericName=IBus Daemon Comment=Start IBus daemon Exec=ibus-autostart Icon=ibus-setup Terminal=false Type=Application StartupNotify=false NoDisplay=true X-KDE-autostart-after=panel X-KDE-StartupNotify=false ibus-autostart.desktop app-ibus\\[email protected] Convert dynamically
  5. 2023-11-21 6/17 Let’s back to the problem • The generated

    service file looks fine but it does not work • Two sub-problems 1. systemd-xdg-autostart-generator ignores/skips desktop files containing X-GNOME-Autostart-Phase key • A service file for IBus was not generated at all 2. Daemonized applications are soon killed by Systemd • Encountered after fixing the 1st problem
  6. 2023-11-21 7/17 (1) X-GNOME-Autostart-Phase • Systemd ignored desktop files with

    X-G-A-P because such desktop files should be handled by GNOME – And expected to be unnecessary under the other DEs – Even though they are necessary for DEs other than GNOME • Solutions – openSUSE side: • removed unnecessary X-GNOME-Autostart-Phase from packages • migrated from a XDG autostart desktop to a native Systemd service – Upstream side: • check the current DE before starting a service by ExecCondition=/usr/lib/systemd/systemd-xdg-autostart-condition • Do not skip desktop files anymore
  7. 2023-11-21 8/17 (2) Daemonized applications are soon killed by Systemd

    • Daemonized (or forking) – Applications running background – A main process exits soon but their sub processes keep running $ ibus-daemon --daemonize # the main process exits soon after forking $ # but its children keep running in background
  8. 2023-11-21 9/17 Type=exec to cgroup • When the main process

    of a service exits, Systemd stop the service if Type=exec – Systemd kills all the remaining processes of the stopped service • So Type=exec cannot be used for daemonized applications because the main process exits soon • Solution: ExitType=cgroup – The upstream introduced a new ExitType – A service exit when all the process (in the cgroup) of the service exits [Service] Type=exec ExecStart=:/usr/bin/ibus-autostart [Service] Type=exec ExitType=cgroup ExecStart=:/usr/bin/ibus-autostart Note: Type=forking cannot be applied for every applications in XDG autostart Type=simple might be enough, but I’m not sure why not
  9. 2023-11-21 10/17 State machine (ExitType=main) RUNNING SIGCHLD The main process

    exited. START DEAD FDs for output of the main process is opened. systemctl start Start the main process of service
  10. 2023-11-21 11/17 State machine (ExitType=cgroup) cgroup empty (all the process

    of a service exited) RUNNING SIGCHLD The main process exited. START DEAD FDs for output of the main process is opened. systemctl start Start the main process of service
  11. 2023-11-21 12/17 It still does not work • IBus is

    soon killed due to timeout – It’s state does not change from START to RUNNING – It seems that Systemd cannot detect IBus has started successfully • Race condition – systemd-xdg-autostart-generator terminating process a bit too eagerly? #27919 https://github.com/systemd/systemd/issues/27919
  12. 2023-11-21 13/17 What was happening? cgroup empty (all the process

    of a service exited) RUNNING SIGCHLD The main process exited. START DEAD FDs for output of the main process is opened. systemctl start undefined SIGCHLD The main process exited.
  13. 2023-11-21 14/17 Writing a patch • Why SIGCHLD happens during

    the RUNNING state? – It’s signal – While an event that the main process has started in a queue, Systemd might catch the signal – If the application exits very quickly • I wrote a patch and submitted to the upstream
  14. 2023-11-21 15/17 The last version (ExitType=cgroup) cgroup empty (all the

    process of a service exited) RUNNING SIGCHLD The main process exited. START DEAD FDs for output of the main process is opened. systemctl start OR SIGCHLD The main process exited.
  15. 2023-11-21 16/17 The current status in openSUSE • Does this

    problem still reproduce even on Leap 15.5? – Yes – A workaround to disable systemd-xdg-autostart-generator $ kwriteconfig5 --file startkderc --group General --key systemdBoot false – Uses old Systemd and cannot import a new feature to resolve this issue – KDE maintainer would not roll back to the former mechanism • Already fixed in Tumbleweed (with Systemd v254) – Still need test systemd-xdg-autostart-generator carefully https://bugzilla.opensuse.org/show_bug.cgi?id=1213156
  16. 2023-11-21 17/17 Conclusion • KDE Plasma now uses systemd-xdg-autostart-generator for

    XDG autostart • It did not work if a service has – X-GNOME-Autostart-Phase – A main process that forks child processes and exit soon • This problem have been resolved in Tumbleweed but not in Leap 15.5 – The patch has been already merged by the upstream