repeatedly polls D-Bus names, should watch NameOwnerChanged instead
Hi, forwarding a bug from a Debian user and copy-pasting the report:
While using debian-live-11.0.0-amd64-xfce.iso to try to reproduce an unrelated issue, I noticed that the pulseaudio plugin for the panel calls the ListNames() D-Bus method approximately once per second. This causes the system to wake up unnecessarily, and is likely to be a waste of electricity on an otherwise idle system. It also makes it more difficult to debug the unrelated issue with dbus-monitor :-)
Looking at upstream source code, this seems to be because it calls pulseaudio_mpris_tick_cb() once per second, which calls pulseaudio_mpris_get_available_players(), which calls ListNames.
This shouldn't be necessary: D-Bus is designed to be completely idle (no messages sent, no messages received, no wakeups) if nothing interesting is happening, similar to other AF_UNIX-based protocols such as X11.
Workaround: remove the Pulseaudio plugin (the volume control) from the panel while debugging D-Bus-related issues.
I tried to report this upstream, but so far I have been unable to set up an account on XFCE's Gitlab (presumably the email confirmation has been greylisted).
For protocols like MPRIS that use an equivalence class of names starting with a prefix, the intended pattern since dbus-daemon 1.6.0 (stable release in 2012) is this:
-
Add a match rule for the NameOwnerChanged signal with arg0namespace="org.mpris.MediaPlayer2"
- in GDBus, this is something like: g_dbus_connection_signal_subscribe (connection, "org.freedesktop.DBus", "org.freedesktop.DBus", "NameOwnerChanged", "/org/freedesktop/DBus", "org.mpris.MediaPlayer2", G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, name_owner_changed_cb, ...);
-
When the signal is received, its arguments are the name, the old owner, and the new owner
- if the new owner is "", the name is disappearing from the bus: remove it from the list of MPRIS media players
- otherwise, the name is appearing on the bus: add it to the list of MPRIS media players
-
After connecting to the signal, call ListNames once to populate the initial list of MPRIS media players