Slow keyboard rescan causes seconds of keyboard lag
Salut, I'd like to start with some praise for this project. I'm very happy with the transcription and the seamless setup of the app. This is exactly what I was looking for and I don't want to go back to typing, so please keep up the good work!
on my debian 13 with wayland and a 24-core Ryzen, I get the following messages in my log:
Sep 11 22:32:53 ryzen sg[2143]: [ptt] WARNING slow keyboard rescan: 1424ms (3 keyboards) [issue #8]
Sep 11 22:33:03 ryzen sg[2143]: [ptt] WARNING slow keyboard rescan: 1382ms (3 keyboards) [issue #8]
Sep 11 22:33:14 ryzen sg[2143]: [ptt] WARNING slow keyboard rescan: 1339ms (3 keyboards) [issue #8]
Sep 11 22:33:24 ryzen sg[2143]: [ptt] WARNING slow keyboard rescan: 1314ms (3 keyboards) [issue #8]
Sep 11 22:33:35 ryzen sg[2143]: [ptt] WARNING slow keyboard rescan: 1183ms (3 keyboards) [issue #8]
Sep 11 22:33:45 ryzen sg[2143]: [ptt] WARNING slow keyboard rescan: 1707ms (3 keyboards) [issue #8]
Sep 11 22:33:56 ryzen sg[2143]: [ptt] WARNING slow keyboard rescan: 1530ms (3 keyboards) [issue #8]
Sep 11 22:34:06 ryzen sg[2143]: [ptt] WARNING slow keyboard rescan: 1337ms (3 keyboards) [issue #8]
Sep 11 22:34:20 ryzen sg[2143]: [ptt] WARNING slow keyboard rescan: 1024ms (3 keyboards) [issue #8]
Sep 11 22:34:30 ryzen sg[2143]: [ptt] WARNING slow keyboard rescan: 1357ms (3 keyboards) [issue #8]
Sep 11 22:34:42 ryzen sg[2143]: [ptt] WARNING slow keyboard rescan: 1084ms (3 keyboards) [issue #8]
The issue is with the PTT python script.
AI says the issue is in the device rescan that happens every 10 seconds. Apparently, closing the device file descriptors is delayed at the kernel by "an RCU grace period in evdev's release path". I have many devices that it all closes sequentially, so this causes a massive delay. AI really wants to drive home that even though the log prints only 3 keyboards, the problem is that there are 24 other devices that each get opened and closed again on a rescan, which accumulates to this massive blocking stall.
In issue #8 , the stuttering symptom was fixed by moving the rescan into a different part of the code. But the root cause of the blocking stall remains, as you can see from the massive stalls (almost a second) in the user's log:
It works like a charm 🥇
journalctl --user -u dictee-ptt | grep "issue #8"mai 25 20:44:23 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 939ms (5 claviers) [issue #8] mai 25 20:44:35 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 857ms (5 claviers) [issue #8] mai 25 20:44:48 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 870ms (5 claviers) [issue #8] mai 25 20:45:03 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 906ms (5 claviers) [issue #8] mai 25 20:45:13 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 919ms (5 claviers) [issue #8] mai 25 20:45:25 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 905ms (5 claviers) [issue #8] mai 25 20:45:35 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 816ms (5 claviers) [issue #8] mai 25 20:45:47 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 922ms (5 claviers) [issue #8] mai 25 20:45:57 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 840ms (5 claviers) [issue #8] mai 25 20:46:08 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 814ms (5 claviers) [issue #8] mai 25 20:46:20 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 838ms (5 claviers) [issue #8] mai 25 20:46:30 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 790ms (5 claviers) [issue #8] mai 25 20:46:41 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 874ms (5 claviers) [issue #8] mai 25 20:46:52 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 929ms (5 claviers) [issue #8] mai 25 20:47:02 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 897ms (5 claviers) [issue #8] mai 25 20:47:18 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 921ms (5 claviers) [issue #8] mai 25 20:47:28 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 844ms (5 claviers) [issue #8] mai 25 20:47:39 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 860ms (5 claviers) [issue #8] mai 25 20:47:49 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 867ms (5 claviers) [issue #8] mai 25 20:48:01 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 849ms (5 claviers) [issue #8] mai 25 20:48:13 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 852ms (5 claviers) [issue #8] mai 25 20:48:23 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 901ms (5 claviers) [issue #8] mai 25 20:48:36 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 865ms (5 claviers) [issue #8] mai 25 20:48:47 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 893ms (5 claviers) [issue #8] mai 25 20:48:58 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 792ms (5 claviers) [issue #8] mai 25 20:49:19 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 784ms (5 claviers) [issue #8] mai 25 20:49:48 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 804ms (5 claviers) [issue #8] mai 25 20:49:59 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 884ms (5 claviers) [issue #8] mai 25 20:50:09 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 807ms (5 claviers) [issue #8] mai 25 20:50:20 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 859ms (5 claviers) [issue #8] mai 25 20:50:31 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 916ms (5 claviers) [issue #8] mai 25 20:50:42 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 828ms (5 claviers) [issue #8] mai 25 20:50:53 specialized sg[230265]: [ptt] WARNING rescan claviers lent: 896ms (5 claviers) [issue #8]
Originally posted by @Pada0 in #8
AI told me you could solve this by either not polling the devices (but having some sort of notification when a new device is detected, for example), or by not opening all the device file descriptors for scanning their contents, but instead detecting their attributes through /proc/bus/input/devices. This seems like a reasonable quick fix to me.
This whole thing honestly made me question the PTT Python script that sits between my keystrokes and my desktop. It seems very dangerous to me to have a script relaying my keystrokes to the rest of my machine just for grabbing a hotkey push and release. There's so much that could go wrong and break the keyboard on desktop. Some research revealed that there's a "XDG GlobalShortcuts portal" that could be used as the backend instead of "evdev" or "raw". On the desktops where this is supported, it would give a much safer way of implementing PTT by responding to key push and release events coming from the desktop. I strongly believe it would be best to use a framework like GlobalShortcuts instead of messing with the kernel interface.
Finally, here's a script AI wants me to share with you to measure the time it takes to close all these file descriptors. It's supposed to showcase how much faster it would be by just making the operations concurrent:
import os, time, threading, glob
paths = sorted(glob.glob("/dev/input/event*"))
def open_all():
fds = []
for p in paths:
try: fds.append(os.open(p, os.O_RDONLY | os.O_NONBLOCK))
except OSError: pass
return fds
fds = open_all(); n = len(fds)
t0 = time.monotonic()
for fd in fds: os.close(fd)
seq = time.monotonic() - t0
fds = open_all()
t0 = time.monotonic()
ts = [threading.Thread(target=os.close, args=(fd,)) for fd in fds]
for t in ts: t.start()
for t in ts: t.join()
par = time.monotonic() - t0
fd = os.open("/dev/null", os.O_RDONLY); t0 = time.monotonic(); os.close(fd)
null = time.monotonic() - t0
print(f"input nodes : {n}")
print(f"close() sequential : {seq*1000:7.1f} ms ({seq/n*1000:.1f} ms per fd)")
print(f"close() concurrent : {par*1000:7.1f} ms ({par/n*1000:.1f} ms per fd)")
print(f"ratio : {seq/par:.1f}x")
print(f"close() /dev/null : {null*1000:7.3f} ms (control)")
print(f"CPUs : {os.cpu_count()}")
On my machine:
input nodes : 27
close() sequential : 1128.0 ms (41.8 ms per fd)
close() concurrent : 60.4 ms (2.2 ms per fd)
ratio : 18.7x
close() /dev/null : 0.002 ms (control)
CPUs : 24
Good catch, and thanks for the kind words.
You're right about where the time goes, and it's the closing that costs, not the opening. I ran the 1.3.6 code here: opening a node is instant, closing one takes 12 to 20 ms, and a rescan over my 24 input nodes takes 270 ms. Your 27 devices put you in the same place, at the top of the range you're seeing.
Fixed on the 1.3 branch. What a node can do never changes, so it's examined once and never opened again. Only nodes that appeared since the last scan get opened. A rescan with no hotplug now costs a single list_devices() call and closes nothing: 270 ms down to 0.4 ms on the same machine. Hotplug still works, and there's a test now that fails if a quiet rescan ever opens a node again.
About the portal: fair point, and it's on the list for 1.4. evdev will have to stay as a fallback for setups where the portal isn't available, but it shouldn't be the only path.
This ships in 1.3.7.
1.3.7-rc4 is out as a pre-release with the rescan change: only the devices that appeared since the last scan are opened, so a quiet rescan costs nothing. If you can try it on the machine with all those input devices, I'd like to know if the lag is gone:
curl -fsSL https://github.com/rcspam/dictee/releases/download/v1.3.7-rc4/install.sh | bash -s -- --version 1.3.7-rc4
1.3.7 is out as the final release. The code is the same as rc4, only the version number changed. If you already run rc4, you don't need to do anything. Otherwise, to update:
curl -fsSL https://github.com/rcspam/dictee/releases/download/v1.3.7/install.sh | bash
If something still doesn't work for you, tell me here.