Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Bug: Quickly connected and disconnected device causes crash (failed read: No such device (os error 19) #1683

@laxsjo

Description

@laxsjo

Requirements

Describe the bug

When running kanata, quickly disconnecting and reconnecting an input device sometimes causes it to crash with the error failed read: No such device (os error 19). This is very timings dependent and usually takes a few tries.

Relevant kanata config

You can reproduce it with a minimal layout:

(defsrc)
(deflayer "base")

To Reproduce

  1. Check out main and cd into it (I used dd02c3e, but I've also confirmed with 1.8.0).
  2. Build using cargo build.
  3. Run kanata using sudo ./target/debug/kanata --cfg path/to/layout.kbd (the sudo is optional, but it becomes much harder to trigger the bug for me without it).
  4. Repeatedly disconnect and reconnect a USB input device until the error appears. (This is pretty finicky and is very timings dependent.)

Expected behavior

Kanata not crashing under these circumstances.

Kanata version

1.8.0 to main (haven't tried lower)

Debug logs

17:40:48.0341 [INFO] kanata v1.9.0 starting
17:40:48.0347 [WARN] No defcfg is defined. Consider whether the process-unmapped-keys defcfg option should be yes vs. no. Adding defcfg with process-unmapped-keys defined will remove this warning.
17:40:48.0348 [INFO] process unmapped keys: false
17:40:48.0349 [INFO] NOTE: kanata was compiled to never allow cmd
17:40:48.0353 [INFO] config file is valid
17:40:48.0364 [INFO] Created device "/dev/input/event27"
17:40:48.0365 [INFO] Sleeping for 2s. Please release all keys and don't press additional ones. Run kanata with --help to see how understand more and how to disable this sleep.
17:40:50.0367 [INFO] entering the processing loop
17:40:50.0371 [INFO] entering the event loop
17:40:50.0371 [INFO] looking for devices in /dev/input
17:40:50.0372 [INFO] Init: catching only releases and sending immediately
17:40:50.3581 [INFO] registering /dev/input/event9: "ZSA Technology Labs Moonlander Mark I Keyboard"
17:40:50.3880 [INFO] registering /dev/input/event7: "ZSA Technology Labs Moonlander Mark I Consumer Control"
17:40:50.4101 [INFO] registering /dev/input/event5: "ZSA Technology Labs Moonlander Mark I System Control"
17:40:50.4390 [INFO] registering /dev/input/event3: "ZSA Technology Labs Moonlander Mark I"
17:40:50.4601 [INFO] registering /dev/input/event25: "Corsair CORSAIR VIRTUOSO SE Wireless Gaming Headset Consumer Control"
17:40:50.4951 [INFO] registering /dev/input/event23: "DP-5"
17:40:50.5221 [INFO] registering /dev/input/event21: "Framework Laptop 16 Keyboard Module - ISO Keyboard"
17:40:50.5472 [INFO] registering /dev/input/event19: "Framework Laptop 16 Keyboard Module - ISO Consumer Control"
17:40:50.5705 [INFO] Starting kanata proper
17:40:50.5706 [INFO] You may forcefully exit kanata by pressing lctl+spc+esc at any time. These keys refer to defsrc input, meaning BEFORE kanata remaps keys.
17:40:50.5750 [INFO] registering /dev/input/event18: "Framework Laptop 16 Keyboard Module - ISO System Control"
17:40:50.6011 [INFO] registering /dev/input/event17: "Framework Laptop 16 Keyboard Module - ISO"
17:40:54.0363 [WARN] removing kbd device: /dev/input/event3
17:40:54.1824 [WARN] removing kbd device: /dev/input/event5
17:40:54.2122 [WARN] removing kbd device: /dev/input/event7
17:40:54.2855 [WARN] removing kbd device: /dev/input/event9
17:40:54.9951 [INFO] watch found file changes, looking for new devices
17:40:54.9951 [INFO] sleeping for a moment to let devices become ready
17:40:55.1952 [INFO] looking for devices in /dev/input
17:40:55.5950 [INFO] registering /dev/input/event9: "ZSA Technology Labs Moonlander Mark I Keyboard"
17:40:55.6270 [INFO] registering /dev/input/event7: "ZSA Technology Labs Moonlander Mark I Consumer Control"
17:40:55.6502 [INFO] registering /dev/input/event5: "ZSA Technology Labs Moonlander Mark I System Control"
17:40:55.6740 [INFO] registering /dev/input/event3: "ZSA Technology Labs Moonlander Mark I"
17:40:55.7960 [INFO] watch found file changes, looking for new devices
17:40:55.7960 [INFO] sleeping for a moment to let devices become ready
17:40:55.9961 [INFO] looking for devices in /dev/input
17:41:02.2271 [WARN] removing kbd device: /dev/input/event3
17:41:02.3555 [WARN] removing kbd device: /dev/input/event5
17:41:02.3913 [WARN] removing kbd device: /dev/input/event7
17:41:02.4553 [WARN] removing kbd device: /dev/input/event9
17:41:04.3070 [INFO] watch found file changes, looking for new devices
17:41:04.3071 [INFO] sleeping for a moment to let devices become ready
17:41:04.5071 [INFO] looking for devices in /dev/input
17:41:04.8940 [INFO] registering /dev/input/event9: "ZSA Technology Labs Moonlander Mark I Keyboard"
17:41:05.2442 [ERROR] failed read: No such device (os error 19)


Press enter to exit
17:41:05.2443 [ERROR] channel disconnected

Note: Running with --debug didn't seem to give any additional relevant information, so I decided to not add it here to keep this log shorter. But if you think it may be relevant I can add it.

Operating system

Linux running NixOS

Additional context

As I said this is pretty timings dependent, and (based on the logs) seems to occur when the device is connected and removed quicker than the interval between the watcher detecting the changed input device and actually trying to register the new device (since it prints something about sleeping for a short while after detecting changed files).

For some reason it's much easier to reproduce the bug when running with sudo. When running with sudo I'm able to trigger it within 3 tries of replugging my keyboard, while it takes 10+ without it... For context, I've created a group uinput which my user is a part of. I have the following udev rule, which I think is what which assigns input devices to the group.

KERNEL=="uinput", MODE="0660", GROUP="uinput", OPTIONS+="static_node=uinput"

Metadata

Metadata

Assignees

Labels

PRs welcomejtroo has no plans to work on this at present, but PRs are welcomebugSomething isn't workinglinuxIssue pertains to Linux only

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions