X11: every keyboard layout switch freezes the compositor for up to ~1 s (reload_keybindings regrabs per-window keybindings on every window)
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 55/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- c, linux
- Ambito
- desktop, performance
Direzione di ricerca
Start in src/core/keybindings.c at reload_keybindings(), then trace how reload_combos() resolves keycodes and how grab_key_bindings() regrabs per-window bindings. Compare resolved combos across layout changes and use the issue’s X11 reproduction with many windows to measure the freeze. Done means layout switching avoids unnecessary synchronous per-window grabs while still regrabbing combos whose keycodes or masks change.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Distribution
Linux Mint 22.3 Cinnamon (X11), reproduced on two machines
Package version
Cinnamon 6.6.9, muffin 6.6.3+zena
Graphics hardware in use
- Machine A: AMD Radeon Phoenix iGPU (Ryzen, 16 threads); layouts pl, ru
- Machine B: Intel i7-12800HX (24 threads); layouts us, ru, pl
Frequency
Always
Bug description
On X11, every keyboard layout switch blocks Cinnamon's main loop. Because Cinnamon is the compositor, the whole screen stops updating for that time; this was visible on machine A. On machine A, keys typed right after Super+Space were also lost (see Measurements).
The freeze grows linearly with the number of open windows and roughly linearly with the number of assigned per-window keybindings. On machine B (16 per-window bindings) it is ~290 ms with 32 windows and 800–960 ms with 149 windows. Many windows are realistic: on machine A, 116 of the 149 open windows are browser windows.
Cause (src/core/keybindings.c, line numbers from tag 6.6.3; the same code is on master as of 71814a8fe7):
- The
keymap-layout-group-changedsignal is connected toreload_keybindings()(line 5042). reload_keybindings()(line 1163) callsungrab_key_bindings(),reload_modmap(),reload_combos()andgrab_key_bindings().grab_key_bindings()(line 920) iterates over all windows inMETA_LIST_DEFAULT, including minimized ones, and callsmeta_window_grab_keys()(line 1635) for each.- The call chain continues:
change_window_keygrabs()→meta_change_keygrab()→XIGrabKeycode()for every keycode of every per-window binding. XIGrabKeycode()waits for a server reply, so each grab is a synchronous round trip.
Cost per switch ≈ windows × per-window bindings round trips. On machine B, Xorg CPU time rises during the switch while Cinnamon's CPU stays flat: Cinnamon is waiting on X replies. Other X clients still get replies in 5–7 ms.
Why the regrab exists: reload_combos() resolves each keysym to keycodes in the current layout group. For non-Latin layouts it also uses a secondary US layout (reload_active_keyboard_layouts(), line 763). So a combo's keycodes can change when the group changes. But the per-window combos here use only F-keys, arrows and Space: Alt+F4, Alt+Space, Super+arrows and the like. Their keycodes are identical in every layout, so the full per-window regrab does no useful work.
This is inherited from mutter: the same signal hookup and ungrab/regrab in reload_keybindings() exist in mutter 3.36.9 (muffin's base) and still in mutter gnome-48, the last branch with an X11 session. Current mutter main has dropped the X11 backend, so there is no upstream fix to port.
The per-window part is X11-only: meta_window_grab_keys() returns early on Wayland (line 1640).
Steps to reproduce
- Configure two keyboard layouts in an X11 Cinnamon session.
- Open ~100+ windows. Empty GTK windows are enough, minimized or not, for example:
python3 -c "import gi;gi.require_version('Gtk','3.0');from gi.repository import Gtk;[Gtk.Window(title=str(i)).show() for i in range(100)];Gtk.main()" - Press Super+Space and immediately start typing. The first characters are lost and the screen stutters.
To measure, run this script; it assumes two layouts. It switches layouts via org.Cinnamon.ActivateInputSourceIndex and polls Cinnamon over D-Bus every ~10 ms. It prints the longest D-Bus reply time within 1 s after each switch.
pingsw.py
import time,threading,ctypes,ctypes.util
from gi.repository import Gio,GLib
X=ctypes.CDLL(ctypes.util.find_library('X11'));X.XOpenDisplay.restype=ctypes.c_void_p;d=ctypes.c_void_p(X.XOpenDisplay(None))
def grp():
b=(ctypes.c_ubyte*32)();X.XkbGetState(d,0x100,b);return b[0]
bus=Gio.bus_get_sync(Gio.BusType.SESSION,None)
def ping():
t=time.time(); bus.call_sync('org.Cinnamon','/org/Cinnamon','org.freedesktop.DBus.Properties','Get',GLib.Variant('(ss)',('org.Cinnamon','CinnamonVersion')),None,0,5000,None); return (time.time()-t)*1000
sw=[];lat=[];stop=[False]
def pinger():
while not stop[0]:
t=time.time(); l=ping(); lat.append((t,l)); time.sleep(0.01)
th=threading.Thread(target=pinger);th.start()
for i in range(10):
time.sleep(1.2); t=time.time()
bus.call_sync('org.Cinnamon','/org/Cinnamon','org.Cinnamon','ActivateInputSourceIndex',GLib.Variant('(i)',(1-grp() if grp()<2 else 0,)),None,0,5000,None)
sw.append(t)
time.sleep(1); stop[0]=True; th.join()
for t in sw:
print("max cinnamon ping within 1s after switch: %4.0f ms" % max(l for pt,l in lat if t-0.05<=pt<=t+1.0))
base=[l for pt,l in lat if all(abs(pt-t)>1.2 for t in sw)]
print("baseline max ping:", round(max(base)) if base else None)
Measurements. Without switching, Cinnamon replies over D-Bus in 2–9 ms.
Machine B, 16 per-window bindings assigned (Super+arrows, Super+Shift+arrows, Ctrl+Shift+Alt+arrows, Alt+F4, Alt+Space, Shift+Alt+F7/F8):
| Windows | Freeze per switch (median) |
|---|---|
| 32 | 284–302 ms |
| 62 | 441 ms |
| 92 | 586 ms |
| 149 | 800–960 ms |
This fits ≈ 130–160 ms + 4.4–5 ms per window (r = 0.998). Window decorations, minimized state and the target layout make no difference.
Machine A, 149 windows, varying the per-window bindings assigned in org.cinnamon.desktop.keybindings.wm (same session):
| Per-window bindings assigned | Freeze per switch |
|---|---|
| 18 (original configuration) | ~450 ms |
| 10 | ~235 ms |
| 7 | ~170 ms |
| 0 | ~65 ms |
Also on machine A with 18 bindings: 149 windows gave ~450 ms, 209 gave ~600 ms, and 269 gave ~650–770 ms. Absolute numbers vary between sessions (another run with the same setup gave 650–735 ms); the trends are stable.
The freeze reproduces when calling Meta.get_backend().lock_layout_group(n) directly via org.Cinnamon.Eval. Timing the JS side of InputSourceManager.activateInputSource() (apply, signal emission, D-Bus emit) shows only 3–4 ms in total. The time is spent in muffin's reload_keybindings(), blocked on synchronous X requests.
In one test, Super+Space was injected via XTest and a key was typed every 25 ms right after it. With a ~700 ms freeze, about 26 of 60 characters were lost.
Expected behavior
Switching between layout groups that are already loaded should not block the compositor or drop input. Possible fixes:
- Keep
reload_modmap()andreload_combos(), which make no per-window X requests, but compare the new resolved combos with the old ones. Ungrab and grab only the combos whose keycodes or mask changed. Non-Latin layouts keep working, because every combo whose keycodes do change is still regrabbed. For the per-window combos above, nothing changes, so no per-window X requests are needed. - At minimum, skip the per-window loop in
grab_key_bindings()/ungrab_key_bindings()when no per-window combo changed.
Additional information
Pre-submission checks (machine A):
- Existing reports: none found in muffin issues (all states). Possibly related user reports: linuxmint/cinnamon#13428, linuxmint/cinnamon#13488.
- Third-party xlets: I disabled all five third-party applets (
temperature@fevimu,batterypower@joka42,sysmonitor@orcus,system-controls@rcalixte,[email protected]) and restarted Cinnamon. There were no desklets or extensions to disable. Result with 149 windows and 7 per-window bindings: freeze 99–187 ms, median ~170 ms. With the applets re-enabled after another restart: 166–184 ms, median ~169 ms. No difference. - ~/.xsession-errors: a series of 10 switches added no lines.
Unrelated cleanup noticed: calc_grab_modifiers() (line 1204) tests ignored_mask & keys->ignored_modifier_mask instead of a subset test ((ignored_mask & ~keys->ignored_modifier_mask) == 0), so extra modifier combinations are grabbed. In a quick test (Shift+Alt+F4 with NumLock on) I found no user-visible effect; I did not investigate why.
- Lingua principale
- C
- Stelle
- 249
- Fork
- 133
- Merge medio
- 17m
- PR unite (30g)
- 1
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di linuxmint/muffin
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 43/100
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 18/100
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 30/100
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
Tutte le issue di linuxmint/muffin
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
389ds/389-ds-base#8152 ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
isolation2 lockmodes fails on cassert builds: extra AccessShareLock on partition rootForse già presa @tuhaihe l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
apache/cloudberry#2356 ·
I maintainer di solito rispondono entro 2 giorni
-
windows-artifacts: the win-arm64 job uses a Visual Studio generator missing from windows-11-armAperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 86/100
-
documentation
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 91/100