Bug 483137

Summary: Screencast plugin fails if PipeWire is started after KWin
Product: [Plasma] kwin Reporter: Naomi Calabretta <tony0000.ac>
Component: wayland-genericAssignee: KWin default assignee <kwin-bugs-null>
Status: RESOLVED FIXED    
Severity: major CC: bugs+kde, idgr, jared, kde, lamarque, leodream2008, lopit321, mkyral, morsmortium, mrb, nate, sokann, thomas.garnier, tux.rising
Priority: NOR Keywords: regression
Version: 6.0.1   
Target Milestone: ---   
Platform: Other   
OS: Linux   
Latest Commit: Version Fixed In: 6.1
Sentry Crash Report:
Attachments: revert kwin commit 37d2a7914329c65361eedfd995f25bd6867b68bc to fix screensharing on Gentoo Linux
Revert commit 37d2a7914329c65361eedfd995f25bd6867b68bc for kwin 6.0.3.1

Description Naomi Calabretta 2024-03-10 17:14:53 UTC
SUMMARY
On Artix Linux we use XDG Autostart to start services - such as PipeWire. There has been a change between 5.27.10 and 6.0.1 that now makes KWin not reinitialize the screencast plugin in the rare case it won't find the /run/user/1000/pipewire-0 socket. This causes all sorts of breakages- from Spectacle and OBS not being able to screen record to chat apps not being able to screen share, plus task thumbnails in plasmashell will be missing.


STEPS TO REPRODUCE
1. Start Plasma on Artix Linux or any Linux distribution which starts Plasma before PipeWire
2. Observe logs (I used PIPEWIRE_DEBUG=4 as an env variable)
3. Observe screencast plugin being bugged out

OBSERVED RESULT
Screencast doesn't work with "Failed to connect PipeWire context" as an error.

EXPECTED RESULT
Screencast works anyway.

SOFTWARE/OS VERSIONS
Linux/KDE Plasma: Artix Linux
(available in About System)
KDE Plasma Version: 6.0.1
KDE Frameworks Version: 6.0.0
Qt Version: 6.6.2

ADDITIONAL INFORMATION
Not applicable
Comment 1 Naomi Calabretta 2024-03-10 17:25:59 UTC
Breakage is supposed to start from this commit here: https://invent.kde.org/plasma/kwin/-/commit/37d2a7914329c65361eedfd995f25bd6867b68bc
Comment 2 Chris Pfuhl 2024-03-11 16:51:16 UTC
Created attachment 166969 [details]
revert kwin commit 37d2a7914329c65361eedfd995f25bd6867b68bc to fix screensharing on Gentoo Linux

thank you very much for finding that patch.
i can confirm reverting the patch makes screensharing work for me on Gentoo Linux.
Since updating to Plasma 6.0.0 i had the same behavior, the display selection menu came
up but when pressed any combination received "Failed to connect PipeWire context".
With the attached patch built into the package "kde-plasma/kwin-6.0.1-r2" from the main gentoo tree
screensharing seems to work flawlessly so far after a restart of the display server.
Comment 3 Naomi Calabretta 2024-03-11 17:19:06 UTC
That commit is to be either reverted or looked upon because it makes two assumptions:
1. All Plasma users have a session which is managed by Systemd or similar (user service supervision) which of course can start some services to reach a target, for example the graphical target, at which point KWin starts;
2. PipeWire will never ever be restarted. If that was the case, the error would probably surface again, even on supervised sessions.

This is what I can conclude from a raw look at the code; though it should not be hard to check if initialization has failed and at least attempt it again at some other point in time.
Comment 4 David Edmundson 2024-03-27 09:48:37 UTC
>2. PipeWire will never ever be restarted. If that was the case, the error would probably surface again, even on supervised sessions.

You are right, and that is something we need to fix
Comment 5 Marian Kyral 2024-04-10 17:37:07 UTC
I can confirm it is still broken in 6.0.3 and the unfortunately applying revert patch does not work anymore. So I have to call kwin_wayland --replace, but this kills running application including plasmashell.
Comment 6 Lamarque V. Souza 2024-04-10 17:56:17 UTC
Created attachment 168354 [details]
Revert commit 37d2a7914329c65361eedfd995f25bd6867b68bc for kwin 6.0.3.1

I have updated the revert patch to apply to kwin 6.0.3.1 so we can use screencast until the problem is properly fixed.
Comment 7 Chris Pfuhl 2024-04-10 18:45:59 UTC
i'm running the same patch according to diff (just with slightly different "index ..." lines which hopefully aren't that relevant to applying)
For what it's worth what also worked for me without a patch at least for gentoo was to login as your user account in a shell
and run "dbus-run-session gentoo-pipewire-launcher" and then start your display manager and login with the same user.
not very pretty but i guess this should also work for other distributions with the correct pipewire launch command.

i also wanted to try putting a pipewire launch script in ~/.config/plasma-workspace/env
but i'm not very hopeful that this runs early enough. In the meantime i also found a somewhat experimental 
and pretty complex looking guide in the gentoo wiki  to run user services with openrc:
https://wiki.gentoo.org/wiki/OpenRC/User_services
maybe at some point i'll get around to trying this.
Comment 8 Nate Graham 2024-04-11 03:31:29 UTC
*** Bug 483668 has been marked as a duplicate of this bug. ***
Comment 9 Lamarque V. Souza 2024-04-27 22:26:29 UTC
I have found another workaround: Screencast works if I launch pipewire through a script in $HOME/.config/plasma-workspace/env/. In Gentoo the script is as simple as:

/usr/bin/gentoo-pipewire-launcher &

PS:  there is no need for the script to be executable since plasma-session sources it. I also had to disable pipewire autostart by removing the file /etc/xdg/autostart/pipewire.desktop to avoid pipewire being restarted during startup.
Comment 10 Randall Winkhart 2024-04-29 20:25:23 UTC
The $HOME/.config/plasma-workspace/env workaround works on Artix with the following script:

#!/bin/sh
/usr/bin/pipewire &
/usr/bin/pipewire-pulse &
/usr/bin/wireplumber &
Comment 11 Randall Winkhart 2024-04-30 08:24:42 UTC
(In reply to Randall Winkhart from comment #10)
> The $HOME/.config/plasma-workspace/env workaround works on Artix with the
> following script:
> 
> #!/bin/sh
> /usr/bin/pipewire &
> /usr/bin/pipewire-pulse &
> /usr/bin/wireplumber &

After more extensive testing, it seems launching Pipewire in this way causes it to randomly crash. I've had two crashes now, one after a couple hours, another after a bunch more hours :/
Comment 12 Bug Janitor Service 2024-05-09 14:04:44 UTC
A possibly relevant merge request was started @ https://invent.kde.org/plasma/kwin/-/merge_requests/5706
Comment 13 Bug Janitor Service 2024-05-10 07:57:09 UTC
A possibly relevant merge request was started @ https://invent.kde.org/plasma/kwin/-/merge_requests/5708
Comment 14 David Edmundson 2024-05-10 14:35:17 UTC
Git commit 013e69988ffdbed1d3c684a536c0d4b79c7de680 by David Edmundson.
Committed on 10/05/2024 at 14:35.
Pushed by davidedmundson into branch 'master'.

Reconnect Pipewire on Failure

Our connection to pipewire can go down if the pipewire service restarts, it's on us to tear down and reconnect.
To ensure Streams can tear down on their own properly, this patch leaves existing streams with a defunct connection
and creates a new connection for new streams, sharing the connection between them.

This also implicitly fixes the case for distributions without working socket activation.

M  +7    -0    src/plugins/screencast/pipewirecore.cpp
M  +4    -0    src/plugins/screencast/pipewirecore.h
M  +19   -5    src/plugins/screencast/screencastmanager.cpp
M  +3    -1    src/plugins/screencast/screencastmanager.h

https://invent.kde.org/plasma/kwin/-/commit/013e69988ffdbed1d3c684a536c0d4b79c7de680