#1039857 podman crashes my systemd-managed sway session on exit

Package:
sway
Source:
sway
Description:
i3-compatible Wayland compositor
Submitter:
Antoine Beaupre
Date:
2023-09-23 02:09:03 UTC
Severity:
normal
Tags:
#1039857#5
Date:
2023-06-28 21:42:18 UTC
From:
To:
This is a rather hairy problem.

I have made the perhaps-ill-advised configuration of hooking up my
wayland / sway session as a systemd --user unit.

When i run containers with podman and they exit with an error, the whole session crashes.

The last lines before death are:


jun 28 16:53:39 angela podman[298210]: 2023-06-28 16:53:39.53329688 -0400 EDT m=+1.000110624 container died de96bebba97ba50229c01a93e1a0928c34b954b50ca1ea295c20c1d3c2970137 (image=quay.io/minio/mc:latest [...]
jun 28 16:53:39 angela podman[298287]: 2023-06-28 16:53:39.624658051 -0400 EDT m=+0.086326242 container cleanup [...]
jun 28 16:53:39 angela systemd[296508]: sway.service: Main process exited, code=exited, status=1/FAILURE


The podman lines have way more stuff in there, but i spare you the
details...

My .config/systemd/user/ setup is documented in https://anarc.at/software/desktop/wayland/#systemd-integration

A plain:

    podman run docker.io/library/debian:bookworm-slim

... can reproduce the issue. Note that the command needs to be ran
from a terminal spawned by Sway itself, say with $mod-Return. If I
start it from a separate unit or with `systemd-run`, it doesn't crash
the session on exit.

If I run:

    podman run -it --rm docker.io/library/debian:bookworm-slim

I notice that `conmon(8)` takes over the Main PID which is
essentially the core of the problem here. Because Sway refuses to
support systemd:

https://github.com/swaywm/sway/issues/5160

... it does not send a proper "READY" state through sd_notify as other
services might normally do.

In general, that's not *too* much of a problem: systemd can start and
manage services fine without this. But in this case, I'm starting
other units that *do* depend on Sway being ready to run, so I *do*
need this state to properly chain dependencies here.

So I have this horror in my sway config:

exec dbus-update-activation-environment --systemd XDG_CURRENT_DESKTOP=sway \
  && systemctl --user import-environment SWAYSOCK \
                                         DISPLAY \
                                         I3SOCK \
                                         WAYLAND_DISPLAY \
                                         XCURSOR_SIZE \
                                         XCURSOR_THEME \
  && systemd-notify --ready


And the systemd sway.service has:

    NotifyAccess=all

... which is how conmon is able to hijack the Main PID.

A workaround is to change the Sway keybindings to start processes with
systemd, like this:

    # start a terminal
    #
    # we start it as a systemd unit on the fly otherwise podman takes over
    # the Main PID of the sway.service and container exits crash the whole
    # session
    bindsym $mod+Return exec systemd-run --user foot

The real fix here would be for Sway to send something to sd-daemon. Of
course, the easiest way to do this would be to do a clever call to
sd_notify() just at the right place in the Sway source code, but
there's little chance such a patch would be merged upstream...

I looked at the code, and the actual sd_notify() call is not *that*
complex. It opens a UNIX socket and shoves a message in there,
probably just READY=1. Surely Sway could figure out how to write to a
socket on its own?

Hell, it could even just vendor this stupid piece of code here:

https://sources.debian.org/src/systemd/253-4/src/libsystemd/sd-daemon/sd-daemon.c/#L453-L602

Anyway, I don't mean to troll here, but this was a really complicated
bug to figure out and to fix for me (thanks #debian-systemd folks!),
so I figured I would at *least* document it here somehow.

The best case scenario for me is that sway gets sd_notify support. A
working compromise could be an update to the README.Debian file or
even better a sample sway config that has the right knobs.

Failing that, I'm happy to just see this bug closed and add this to
the pile of custom weird shit I carry around everywhere.

Cheers! :)

#1039857#10
Date:
2023-06-29 03:22:36 UTC
From:
To:
On 2023-06-28 17:42:18, Antoine Beaupre wrote:

[...]

Actually, it looks like Sway *already* links against libsystemd or at
least *can* link against it, if I parse the meson build files right. So
we could "just" have a patch that does indeed call sd_notify properly
without adding anything new to the dependency tree.

anarcat@angela:sway-1.7$ rg -i systemd
debian/sway.install
10:debian/config.d/50-systemd-user.conf /etc/sway/config.d/

debian/control
20:               libsystemd-dev (>= 239) | libelogind-dev (>= 239),

debian/changelog
97:  * Add a config file to set environment variables in systemd
208:   - Make build-deps libsystemd-dev and libelogind-dev alternatives
256:    Thus explicitly listing wayland-protocols and libsystemd as

meson_options.txt
11:option('sd-bus-provider', type: 'combo', choices: ['auto', 'libsystemd', 'libelogind', 'basu'], value: 'auto', description: 'Provider of the sd-bus library')

meson.build
95:	sdbus = dependency(['libsystemd', 'libelogind'],
116:conf_data.set10('HAVE_LIBSYSTEMD', sdbus.found() and sdbus.name() == 'libsystemd')

debian/config.d/50-systemd-user.conf
4:# and https://github.com/systemd/systemd/blob/dfc637d0ff756889e8e5b7cb4ec991eb06069aa1/xorg/50-systemd-user.sh
9:        dbus-update-activation-environment --systemd DISPLAY WAYLAND_DISPLAY SWAYSOCK

include/swaybar/tray/tray.h
5:#if HAVE_LIBSYSTEMD
6:#include <systemd/sd-bus.h>

#1039857#15
Date:
2023-07-03 16:07:01 UTC
From:
To:
Control: tags -1 +patch

And here's the patch to fix this.

I'll also send this as a MR to salsa.

#1039857#26
Date:
2023-09-21 16:56:51 UTC
From:
To:
Hi!

Could I get some feedback on this patch? I've been running this for
months now without any problems and it improves systemd support quite a
bit...

Right now I'm relying on this for my desktop session which makes
upgrades rather... brittle. :)

a.

#1039857#31
Date:
2023-09-22 19:56:53 UTC
From:
To:
Hi anarcat,

I'd rather not diverge from upstream and carry a patch for a bugfix that
is a wontfix on upstreams side.
My hope is that upstream either reconsiders or that there is a another
solution for that problem, that does not require us patching every new
version of sway.

cheers,
Birger

#1039857#36
Date:
2023-09-23 02:06:30 UTC
From:
To:
Thanks. I'll rollback my patch and try to find another way.

I strongly doubt upstream will reconsider, they seem to be pretty stuck
up on that one...

a.