Skip to content

service/tray: fix duplicated tray items after the watcher service changes owner - #1554

Open
sujoshua wants to merge 2 commits into
quickshell-mirror:masterfrom
sujoshua:fix/sni-watcher-owner-change
Open

sujoshua wants to merge 2 commits into
quickshell-mirror:masterfrom
sujoshua:fix/sni-watcher-owner-change

Conversation

@sujoshua

@sujoshua sujoshua commented Nov 25, 2026 •

Copy link
Copy Markdown

Describe your changes

The host only listens for the serviceRegistered and serviceUnregistered signals. When a client such as kded6 restarts, the new instance asks for the org.kde.StatusNotifierWatcher service while the old instance still owns it, so the request is queued in the bus. The old instance then exits and the bus hands the service over to the new one directly. From the bus's point of view the service is never unowned, so Qt only sends serviceOwnerChanged. Normally the service would be unregistered when the old instance exits and registered again once an owner asks for it, but here the request was already queued, so neither of those two signals is sent.

The problem is that the host does not listen for serviceOwnerChanged at all. To cover this edge case, we now listen for that event as well and trigger our own unregistration and registration handling inside the host.

quickshell's own watcher has the same problem from the other side. Once it has registered the service it goes on assuming that it is still the owner, and it does not listen for the event that tells it that the service has been taken from it. It now listens for that event as well, and when the service is taken from it, it steps aside and clears its own state. Once it finds out that nobody is the watcher any more, it tries to register again.

Details and descisions

QDBusServiceWatcher turns the ownership change of a watched service into three different signals, and it only sends serviceRegistered and serviceUnregistered when one side of the change is empty:

void QDBusServiceWatcherPrivate::_q_serviceOwnerChanged(
    const QString& service, const QString& oldOwner, const QString& newOwner)
{
    emit q->serviceOwnerChanged(service, oldOwner, newOwner);

    if (oldOwner.isEmpty())
        emit q->serviceRegistered(service);
    else if (newOwner.isEmpty())
        emit q->serviceUnregistered(service);
}

The service is never unowned in the case described above, so neither of the two signals the host listens for is sent. The bus reports the ownership change in a single event instead:

NameOwnerChanged('org.kde.StatusNotifierWatcher', ':1.34359', ':1.34392')

The host therefore never noticed that the watcher changed, and the items of the previous watcher stayed in the model. They point at bus names that no longer exist, and the items the new watcher knows about were added next to them, which shows up as two icons for one application, one of them dead.

The host now also watches for serviceOwnerChanged and rebuilds its item list when the watcher changes owner, which is what the two existing handlers already do for the other two cases:

void StatusNotifierHost::onWatcherOwnerChanged(
    const QString& /*unused*/,
    const QString& oldOwner,
    const QString& newOwner
) {
    if (oldOwner.isEmpty()) return;
    if (newOwner.isEmpty()) return;
    if (oldOwner == newOwner) return;

    this->onWatcherUnregistered();
    this->onWatcherRegistered();
}

The watcher side has the same gap. Its QDBusServiceWatcher used WatchForUnregistration, whose match rule is arg0='<service>', arg2='', so it only saw events where the new owner is empty. WatchForOwnerChange widens the rule to arg0='<service>', which delivers ownership changes as well, and the watcher now drops its item and host lists and clears registered when the service is taken from it. It takes the service back with tryRegister() once it is free again, which is what it already did before.

Open concerns

No ?

Testing

The reproduction needs a watcher that is replaced while a replacement is already asking for the service, so the service is never unowned. A stand-in that queues for it is enough:

python3 - <<'EOF'
import dbus, time
bus = dbus.SessionBus()
iface = dbus.Interface(bus.get_object("org.freedesktop.DBus", "/org/freedesktop/DBus"), "org.freedesktop.DBus")
# flags 0 means the request is queued instead of failing
print(iface.RequestName("org.kde.StatusNotifierWatcher", 0))  # 2 = queued
time.sleep(60)
EOF

# now kill the process that owns the service; it is handed to the queued requester
kill -9 $(pgrep -x kded6)

With the shell running against the current watcher, the change shows up in the log of the shell. Before the change, nothing is logged and the old items stay in the model:

Registering StatusNotifierItem org.kde.StatusNotifierItem-621466-1/StatusNotifierItem to host
Registering StatusNotifierItem org.kde.StatusNotifierItem-4029076-1/StatusNotifierItem to host
Registering StatusNotifierItem :1.35079/StatusNotifierItem to host
Registering StatusNotifierItem :1.35084/StatusNotifierItem to host

The tray then shows two identical icons. After the change, the change is noticed and the items are dropped and read again:

StatusNotifierWatcher changed owner, refreshing items
Unregistering StatusNotifierItems from old watcher
Unregistered StatusNotifierItem org.kde.StatusNotifierItem-4029076-1/StatusNotifierItem from host
Unregistered StatusNotifierItem org.kde.StatusNotifierItem-621466-1/StatusNotifierItem from host
Unregistered StatusNotifierItem :1.35084/StatusNotifierItem from host
Unregistered StatusNotifierItem :1.35079/StatusNotifierItem from host
Registering host with active StatusNotifierWatcher

Both were run against a Hyprland session where kded6 provides the watcher and the shell is restarted regularly, which is what produced the duplicated icons in the first place.

Participation

I am on Hyprland, using end-4's Hyprland dotfiles, which ship a quickshell configuration for the bar and the panels. I noticed that every time quickshell dies and is restarted, my tray ends up showing every icon twice. I had some time today, so I dug into it.

What I found first is that the end-4 dots kill kded6 on every quickshell start. Their conflict killer runs killall kded6 while the shell is starting, and kded6 is restarted afterwards, so the new one takes the StatusNotifierWatcher service over from the old one without it ever being unowned.

I could open a pull request against the end-4 dots to stop kded6 and start it again instead, which would leave the service unowned for a moment. But the missing handling of that state is the more fundamental problem here, so I am opening this pull request instead.

I will be following the PR. The maintainer is welcome to suggest changes or to push commits to the branch directly. If there are any questions or things left to do, I am happy to take care of them.

…er change

The host only listens for the `serviceRegistered` and `serviceUnregistered`
signals. When a client such as kded6 restarts, the new instance asks for the
`org.kde.StatusNotifierWatcher` service while the old instance still owns it,
so the request is queued in the bus. The old instance then exits and the bus
hands the service over to the new one directly. From the bus's point of view
the service is never unowned, so Qt only sends `serviceOwnerChanged`.
Normally the service would be unregistered when the old instance exits and
registered again once an owner asks for it, but here the request was already
queued, so neither of those two signals is sent.

The problem is that the host does not listen for `serviceOwnerChanged` at
all. To cover this edge case, we now listen for that event as well and
trigger our own unregistration and registration handling inside the host.
Once the watcher has registered the org.kde.StatusNotifierWatcher service, it
goes on assuming that it is still the owner, and it does not listen for the
event that tells it that the service has been taken from it.

We now make it listen for that event as well. When the service is taken from
it, it steps aside and clears its own state. Once it finds out that nobody is
the watcher any more, it tries to register again.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant