User sessions never terminate after xsession logout?

When I logout of my X11 session and return to the login screen, a bunch of things get left behind running as my user, including an instance of systemd, pulseaudio, dbus, and assorted gvfs-related things. My display-manager is lightdm, windowmanager is exwm. I’m not running the gnome session manager, but a few gnome components are enabled. Is there something I should be doing in order to get these things cleaned up when I log out? It causes problems to have a previous instance of pulseaudio running in my next login session, for instance.

Here’s what I see after logout if I open a root shell on tty1:

root@mintaka:~# loginctl
SESSION  UID USER    SEAT  TTY 
     56 1000 benley  seat0     
     63    0 root    seat0 tty1
    c16   78 lightdm seat0     

3 sessions listed.

root@mintaka:~# ps aux | grep benley
benley    8569  0.0  0.0  63744  4216 ?        Ss   Oct18   0:01 /nix/store/3p7l67jgj2fqxh2gzybawcyh3j305cf9-systemd-239/lib/systemd/systemd --user
benley    8570  0.0  0.0 242060    80 ?        S    Oct18   0:00 (sd-pam)
benley    8584  0.0  0.1 2005808 22756 ?       Ssl  Oct18   3:46 /nix/store/xipivjz28zwbd8nnwd6wkib717779wc2-keybase-2.5.0-bin/bin/keybase service --auto-forked
benley    8593  0.0  0.0 1849772 14240 ?       Ssl  Oct18   4:30 /nix/store/r9xcbsxibnn3j8sp6j6g6lglzg5748r4-kbfs-2.5.0-bin/bin/kbfsfuse /home/benley/keybase
benley    8619  0.0  0.0  36020  1816 ?        Ss   Oct18   0:01 /nix/store/fsmvv8ah6mm5igxv77wgkywr5mnf61x6-dbus-1.12.10/bin/dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
benley    8630  0.0  0.0 500872  1536 ?        SLs  Oct18   0:05 /nix/store/061s33b4xai1j4yays0adfiawzgbblwz-gnupg-2.2.10/bin/gpg-agent --supervised
benley    8732  0.0  0.0 268856  2292 ?        Ssl  Oct18   0:00 /nix/store/k21sln7yz1f8zqlblf4b94jvxvgrjkwg-at-spi2-core-2.28.0/libexec/at-spi-bus-launcher
benley    8756  0.0  0.2 759876 35708 ?        Sl   Oct18   8:46 /nix/store/d7m8d9n0n53r62xkwrzlrn9j9mp5xda0-insync-1.4.5.37069/client/insync-portable start
benley    8922  0.0  0.0  35436  1492 ?        S    Oct18   0:05 /nix/store/fsmvv8ah6mm5igxv77wgkywr5mnf61x6-dbus-1.12.10/bin/dbus-daemon --config-file=/nix/store/k21sln7yz1f8zqlblf4b94jvxvgrjkwg-at-spi2-core-2.28.0/share/defaults/at-spi2/accessibility.conf --nofork --print-address 3
benley    8927  0.0  0.0 407852  1828 ?        Ssl  Oct18   0:00 /nix/store/b5ygvwilcx91k36v8szz7zaxvb2k3kqq-gvfs-1.36.2/libexec/gvfsd
benley    8936  0.0  0.0 415620     0 ?        Sl   Oct18   0:00 /nix/store/b5ygvwilcx91k36v8szz7zaxvb2k3kqq-gvfs-1.36.2/libexec/gvfsd-fuse /run/user/1000/gvfs -f -o big_writes
benley    8966  0.0  0.0  93300   424 ?        Ss   Oct18   0:00 /nix/store/c22vhr0b2782k7skrlppba8ls4mphf3i-bluez-5.50/libexec/bluetooth/obexd
benley    8968  0.0  0.0 583024  4048 ?        Ssl  Oct18   0:00 /nix/store/b5ygvwilcx91k36v8szz7zaxvb2k3kqq-gvfs-1.36.2/libexec/gvfs-udisks2-volume-monitor
benley    8972  0.0  0.0 392144  2756 ?        Ssl  Oct18   0:00 /nix/store/b5ygvwilcx91k36v8szz7zaxvb2k3kqq-gvfs-1.36.2/libexec/gvfs-mtp-volume-monitor
benley    8976  0.0  0.0 492696  1824 ?        Ssl  Oct18   0:00 /nix/store/b5ygvwilcx91k36v8szz7zaxvb2k3kqq-gvfs-1.36.2/libexec/gvfs-afc-volume-monitor
benley    8981  0.0  0.0 404916  2128 ?        Ssl  Oct18   0:00 /nix/store/b5ygvwilcx91k36v8szz7zaxvb2k3kqq-gvfs-1.36.2/libexec/gvfs-gphoto2-volume-monitor
benley    8986  0.0  0.0 389964  2080 ?        Ssl  Oct18   0:00 /nix/store/b5ygvwilcx91k36v8szz7zaxvb2k3kqq-gvfs-1.36.2/libexec/gvfs-goa-volume-monitor
benley    8990  0.0  0.0 118320528 2372 ?      Sl   Oct18   0:00 /nix/store/sakbd0d8x8mnwq0gca3zyvlwar7mxkjl-gnome-online-accounts-3.28.0/libexec/goa-daemon
benley    9013  0.0  0.0 549944   372 ?        Sl   Oct18   0:01 /nix/store/sakbd0d8x8mnwq0gca3zyvlwar7mxkjl-gnome-online-accounts-3.28.0/libexec/goa-identity-service
benley   10364  0.0  0.0 483756  3280 ?        Sl   Oct18   0:00 /nix/store/b5ygvwilcx91k36v8szz7zaxvb2k3kqq-gvfs-1.36.2/libexec/gvfsd-trash --spawner :1.9 /org/gtk/gvfs/exec_spaw/0
benley   19675  0.0  0.0 1286708 3336 ?        Sl   Oct18   0:00 /nix/store/b5ygvwilcx91k36v8szz7zaxvb2k3kqq-gvfs-1.36.2/libexec/gvfsd-network --spawner :1.9 /org/gtk/gvfs/exec_spaw/1
benley   19687  0.0  0.0 185764     0 ?        Sl   Oct18   0:00 /nix/store/2748vmjlxxx5nqs001wnpk3ascm4zkf3-dconf-0.28.0-lib/libexec/dconf-service
benley   25574  0.0  0.0 528860  1396 ?        SLl  Oct19   0:00 scdaemon --multi-server
benley   27523  0.0  0.0 372676  8328 ?        Ssl  11:25   0:00 /nix/store/7blj51q6s47jh14mm2zdsb0jv9cmpas9-pulseaudio-12.2/bin/pulseaudio --daemonize=no
benley   27524  0.0  0.0 355412  7776 ?        Sl   11:25   0:00 /nix/store/7blj51q6s47jh14mm2zdsb0jv9cmpas9-pulseaudio-12.2/libexec/pulse/gsettings-helper
root     28145  0.0  0.0 133228  1552 tty1     S+   11:26   0:00 grep benley

So what I end up doing is running loginctl kill-session for my stale user session after each time I logout. I know I could set KillUserProcesses=yes in logind’s config, but that has its own downsides. There’s got to be a better way, right?

This is the case when lingering is enabled for a user (which should be not the default?).
You can disable loginctl disable-linger <user>.

1 Like

Linger is already disabled for my user (by default):

$ loginctl show-user benley
UID=1000
GID=100
Name=benley
Timestamp=Thu 2018-11-01 17:23:37 EDT
TimestampMonotonic=103467965446
RuntimePath=/run/user/1000
Service=user@1000.service
Slice=user-1000.slice
Display=23
State=active
Sessions=23
IdleHint=no
IdleSinceHint=0
IdleSinceHintMonotonic=0
Linger=no

Linger seems like it might be not quite the right thing for this case. Quote from loginctl(5):

enable-linger [ USER …], disable-linger [ USER …]

Enable/disable user lingering for one or more users. If enabled for a specific user, a user manager is spawned for the user at boot and kept around after logouts. This allows users who are not logged in to run long-running services. Takes one or more user names or numeric UIDs as argument. If no argument is specified, enables/disables lingering for the user of the session of the caller.

See also KillUserProcesses= setting in logind.conf(5).

Versus the referenced section in logind.conf(5):

KillUserProcesses=

Takes a boolean argument. Configures whether the processes of a user should be killed when the user logs out. If true, the scope unit corresponding to the session and all processes inside that scope will be terminated. If false, the scope is “abandoned”, see systemd.scope(5), and processes are not killed. Defaults to " yes ", but see the options KillOnlyUsers= and KillExcludeUsers= below.

In addition to session processes, user process may run under the user manager unit user@.service . Depending on the linger settings, this may allow users to run processes independent of their login sessions. See the description of enable-linger in loginctl(1).

Note that setting KillUserProcesses=yes will break tools like screen(1) and tmux(1), unless they are moved out of the session scope. See example in systemd-run(1).

So maybe I just want to enable KillUserProcesses after all, and remember to use systemd-run if I want to have things like tmux sessions persist after logout.