https://bugs.kde.org/show_bug.cgi?id=528567

            Bug ID: 528567
           Summary: Global shortcuts stop working when a second
                    kwin_wayland exits (kglobalacceld shares one present
                    flag)
    Classification: Plasma
           Product: kwin
      Version First 6.8.2
       Reported In:
          Platform: CachyOS
                OS: Linux
            Status: REPORTED
          Severity: normal
          Priority: NOR
         Component: general
          Assignee: [email protected]
          Reporter: [email protected]
  Target Milestone: ---

> AI disclosure: this report and the attached example patch were produced by an
> AI agent (OpenCode, DeepSeek V4.1 Flash) from a live debugging session, and
> are provided as an example for a contributor who can own the change. The
> patch is not a merge request and has not been reviewed by a KDE contributor.

## Summary

After a second `kwin_wayland` runs on the same session bus (headless test
harness, recording setup, or `kwin_wayland --replace`), the first compositor's
global shortcuts stop firing. `Alt+Tab` and `Meta+Tab` do nothing, although the
keys reach KWin (visible in the Debug Console's *Input Events* tab) and
`kglobalshortcutsrc` still contains the bindings.

The whole `kwin` shortcut component is inactive:

```text
$ qdbus6 org.kde.kglobalaccel /component/kwin \
    org.kde.kglobalaccel.Component.isActive
false
```

`GlobalShortcut::setActive()` refuses to grab any keys when `_isPresent` is
false, so every KWin shortcut is dead at once.

## Root cause

Presence is a single boolean per component/action, shared by all clients:

```cpp
// kglobalacceld src/globalshortcut.h
bool _isPresent : 1;
```

On QAction destruction the client library calls the D-Bus method
`setInactive` (`kglobalaccel/src/kglobalaccel.cpp:269-275`), and the daemon
clears that shared flag for everybody:

```cpp
// kglobalacceld src/kglobalacceld.cpp:438-446
void KGlobalAccelD::setInactive(const QStringList &actionId)
{
    GlobalShortcut *shortcut = d->findAction(actionId);
    if (shortcut) {
        shortcut->setIsPresent(false);   // all clients lose it
    }
}
```

A second process that registers the same `kwin` actions (a nested compositor,
or two instances of an app sharing a component/action) therefore kills the
first process's shortcuts simply by exiting. `--no-global-shortcuts` is not
enough: it only gates the input filter
(`src/main_wayland.cpp:631`, `src/input.cpp:3194`), not the
`KGlobalAccel::setGlobalShortcut()` calls, so the nested instance still
registers and still clears presence when it exits.

## Steps to reproduce (isolated, private bus)

Everything below uses a private `dbus-daemon`; do **not** point a nested
`kwin_wayland` at a real session bus — that breaks the developer's shortcuts.

1. Start `kwin_wayland --virtual` on a private bus (compositor A). Open two
   windows. `Alt+Tab` opens the switcher.
2. Start a second `kwin_wayland --virtual` on the same private bus (compositor
   B, optionally with `--no-global-shortcuts`), then let it exit.
3. Press `Alt+Tab` in compositor A: the switcher no longer appears, and
   `Component.isActive` for the `kwin` component is `false`.

Minimal form without a compositor: two D-Bus clients call
`setShortcutKeys(..., SetPresent)` for the same action and one calls
`setInactive`. With the unpatched library the action becomes inactive when the
second client leaves; with the example patch it stays active.

## Expected

A client going away must not clear a shortcut another client still presents.
Presence should be per client (or the second compositor must not register at
all).

## Workaround

Re-mark the actions present in the running daemon (no compositor restart, no
key changes):

```text
$ python3 repair-running-session.py
present again: 46 kwin shortcuts
isActive: (true,)
```

First aid only: the next foreign compositor exit can clear them again.

## Example fix

`kglobalacceld-per-client-present.patch` (targets
`invent.kde.org/plasma/kglobalacceld`) tracks a set of present D-Bus senders
per
shortcut:

- `setShortcut(..., SetPresent)` adds `message().service()`;
- `setInactive()` removes only that sender;
- the shortcut goes inactive only when the last owner leaves.

Validated here by building kglobalacceld 6.8.2 with the patch and running the
two-client test:

```text
[system] active after first client: (true,) | after second client left:
(false,)
[patched] active after first client: (true,) | after second client left:
(true,)
```

Known gap in the example: owners are not reaped when a client disconnects
without calling `setInactive` (crash); a `NameOwnerChanged` watcher should drop
vanished senders. A proper patch should also add an autotest.

## Attachments / links

- Gist (report, patch, reproducers, repair script):
  https://gist.github.com/VIGO-VI/bfab9dcd2ea958c40dbf0feb0845e672
- `kglobalacceld-per-client-present.patch`
- `switcher-before.png` (switcher works) / `switcher-after.png` (after the
  second compositor exits)

-- 
You are receiving this mail because:
You are watching all bug changes.

Reply via email to