X11: every keyboard layout switch freezes the compositor for up to ~1 s (reload_keybindings regrabs per-window keybindings on every window)

未关闭
#886 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

@KAMI911 已经在做这个了。

开始于 2026年11月11日。

  • #890 来自 @KAMI911 —— 未关闭

评估

难度
4/5
预计耗时
3-5 天
新手友好度
55/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
c, linux

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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-changed signal is connected to reload_keybindings() (line 5042).
  • reload_keybindings() (line 1163) calls ungrab_key_bindings(), reload_modmap(), reload_combos() and grab_key_bindings().
  • grab_key_bindings() (line 920) iterates over all windows in META_LIST_DEFAULT, including minimized ones, and calls meta_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
  1. Configure two keyboard layouts in an X11 Cinnamon session.
  2. 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()"
  3. 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:

  1. Keep reload_modmap() and reload_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.
  2. 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.

主要语言
C
星标
249
派生
133
平均合并
17 分钟
30 天内合并 PR
1

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

linuxmint/muffin 的其他 Issue

查看 linuxmint/muffin 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。