Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Kanata crashes and is unable to start on macOS if sidecar is on #1342

Open
1 task done
PresidentialAnt opened this issue Nov 10, 2024 · 7 comments
Open
1 task done
Labels
bug Something isn't working macos Issue pertains to macos; jtroo has no macOS devices and does not maintain the support for this OS.

Comments

@PresidentialAnt
Copy link

PresidentialAnt commented Nov 10, 2024

Requirements

Describe the bug

Turning on Sidecar (connecting wirelessly to an iPad as an external display) while Kanata is running will cause Kanata to crash with the error zsh: trace trap sudo ./myPrograms/kanata_macos_cmd_allowed_arm64 -c .

note: I start kanata by runningsudo ./myPrograms/kanata_macos_cmd_allowed_arm64 -c ~/.config/kanata/kanata.kbd

Attempting to restart Kanata while connected to Sidecar fails with the same error.

Kanata is able to restart after disconnecting from Sidecar.

Relevant kanata config

(defcfg
  danger-enable-cmd yes
)

(defsrc
  esc
  f1   f2   f3   f4   f5   f6   f7   f8   f9   f10  f11  f12
  1 2 3 4 5 6 7 8 9 0 - = bspc
  caps
  lsft
  rsft
  ralt
  f
  x
  grv
  c
  e
  w
  z

)

(defalias
  escctrl (tap-hold-press 0 150 esc (layer-toggle two))
  term (cmd open -n -a Alacritty)
  floorp (cmd open -a Floorp)
  finder (cmd open /Users/username)
  code (cmd open -a "Visual Studio Code")
  outlook (cmd open -a "Microsoft Outlook")
  whatsapp (cmd open -a WhatsApp)
  zotero (cmd open -a Zotero)

)

(deflayer base
  esc
  🔅   🔆    C-A-ArrowUp    C-1    C-2    C-3    ◀◀   ▶⏸   ▶▶   🔇   🔉   🔊
  1 2 3 4 5 6 7 8 9 0 - = bspc
  @escctrl
  lsft
  rsft
  rctl
  f
  x
  grv
  c
  e
  w
  z
)

(deflayer two
  del
  f1   f2   f3   f4   f5   f6   f7   f8   f9   f10  f11  f12
  kp1 kp2 kp3 kp4 kp5 kp6 kp7 kp8 kp9 kp0 kp- kp+ nlck
  @escctrl
  caps
  caps
  rctl
  @floorp
  @finder
  @term
  @code
  @outlook
  @whatsapp
  @zotero
)

To Reproduce

  1. Start Kanata
  2. Connect to Sidecar
  3. Try to restart Kanata
  4. Disconnect from Sidecar
  5. Restart Kanata

Expected behavior

Kanata to function regardless of sidecar connectivity.

Kanata version

kanata 1.7.0

Debug logs

No response

Operating system

macOS

Additional context

No response

@PresidentialAnt PresidentialAnt added the bug Something isn't working label Nov 10, 2024
@jtroo jtroo added the macos Issue pertains to macos; jtroo has no macOS devices and does not maintain the support for this OS. label Nov 10, 2024
@jteng2127
Copy link

Same here, I'm on macOS 14.6.1 with kanata 1.7.0

The full log is here

❯ sudo kanata -c kanata.kbd
16:12:41.3765 [INFO] kanata v1.7.0 starting
16:12:41.3768 [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.
16:12:41.3768 [INFO] process unmapped keys: false
16:12:41.3769 [INFO] NOTE: kanata was compiled to never allow cmd
16:12:41.3775 [INFO] config file is valid
16:12:41.3775 [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.
16:12:43.3826 [INFO] entering the processing loop
16:12:43.3829 [INFO] entering the event loop
16:12:43.3830 [INFO] Init: catching only releases and sending immediately
zsh: trace trap  sudo kanata -c kanata.kbd

@infely
Copy link

infely commented Nov 18, 2024

Same here. I'm on macOS 15.1/15.2 with kanata 1.7.0

This bug makes kanata completely unusable with the Vision Pro

@sgript
Copy link

sgript commented Nov 18, 2024

Same issue on 1.7.0 MacOS 15.1

@FukkenShit
Copy link

FukkenShit commented Nov 25, 2024

Tryed kanata today for the first time.
Getting the same behaviour without sidecar but with external display.

Kanata installed from homebrew
Macbook Air M2 macOS 15.0.1
External Monitor: LG Ultrafine 5K 27MD5kL
Keyboard: LOGI Pop Keys
Connected through Unifying receiver plugged into monitor USB Port

% sudo kanata -tc .kanata
06:14:27.0478 [INFO] kanata v1.7.0 starting
06:14:27.0484 [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.
06:14:27.0484 [INFO] process unmapped keys: false
06:14:27.0485 [INFO] NOTE: kanata was compiled to never allow cmd
06:14:27.0485 [DEBUG] (1) kanata_parser::cfg::alloc: freeing allocations of length 0
06:14:27.0486 [INFO] config file is valid
06:14:27.0486 [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.
06:14:29.0537 [INFO] entering the processing loop
06:14:29.0548 [INFO] entering the event loop
06:14:29.0552 [INFO] Init: catching only releases and sending immediately
zsh: trace trap  sudo kanata -tc .kanata

Kanata stopped crashing after these steps:

  1. Disconnect external monitor
  2. Insert Unifying receiver directly into macbook USB port
  3. Launch kanata one more time (already doesn't crash)
  4. Stop kanata
  5. Insert Unifying receiver back into monitor and connect monitor to the macbook
  6. Start kanata

@jtroo jtroo removed their assignment Nov 25, 2024
@jtroo
Copy link
Owner

jtroo commented Nov 25, 2024

Someone will have to investigate a bug, likely in the https://github.com/psych3r/driverkit dependency, but might be in the kanata repo as well.

A core dump might be able to be generated for a bit of insight into what's triggering the crash: https://stackoverflow.com/q/56513700

@KeroppiMomo
Copy link

After debugging with lldb, I believe the issue is that Sidecar creates a keyboard device that has a null product string, and driverkit tries to call CFRelease on the null pointer, which causes a runtime error.

Sidecar off:

$ kanata -l
Apple Internal Keyboard / Trackpad

Sidecar on: (notice the extra newline)

$ kanata -l
Apple Internal Keyboard / Trackpad

lldb output

* thread #1, name = 'main', queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=1, subcode=0x19f390060)
    frame #0: 0x000000019f390060 CoreFoundation`CFRelease.cold.1 + 16
CoreFoundation`CFRelease.cold.1:
->  0x19f390060 <+16>: brk    #0x1

CoreFoundation`__CFStringCollectionCopy.cold.1:
    0x19f390064 <+0>:  adrp   x8, 428
    0x19f390068 <+4>:  add    x8, x8, #0xad2 ; "*** __CFStringCollectionCopy() called with NULL ***"
    0x19f39006c <+8>:  adrp   x9, 426228
Target 0: (kanata) stopped.
(lldb) bt
* thread #1, name = 'main', queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=1, subcode=0x19f390060)
  * frame #0: 0x000000019f390060 CoreFoundation`CFRelease.cold.1 + 16
    frame #1: 0x000000019f1b1e18 CoreFoundation`CFRelease + 132
    frame #2: 0x00000001000eee8c kanata`void release_strings<__CFString const*, __CFString const*, __CFString const*>(strings="Karabiner", strings=0x0000000000000000, strings="") at driverkit.cpp:264:6
    frame #3: 0x00000001000ee7e0 kanata`open_device_if_match(product=0x0000000000000000, device=15619) at driverkit.cpp:280:9
    frame #4: 0x00000001002196e8 kanata`register_device::$_0::operator()(this=0x000000016fdfde40, c=15619) const at driverkit.cpp:305:69
    frame #5: 0x00000001000ef084 kanata`bool consume_kb_iter<register_device::$_0>(consume=(product = 0x0000000000000000)) at driverkit.cpp:249:18
    frame #6: 0x00000001000eef8c kanata`register_device(product=0x0000000000000000) at driverkit.cpp:305:19
    frame #7: 0x00000001000ec390 kanata`karabiner_driverkit::register_device::h063c0f8550bd003f(product=(data_ptr = "", length = 0)) at lib.rs:82:31
    frame #8: 0x00000001000356ac kanata`kanata_state_machine::oskbd::macos::KbdIn::new::h80d73476f570212c(include_names=Option<alloc::vec::Vec<alloc::string::String, alloc::alloc::Global>> @ 0x000000016fdfe550, exclude_names=Option<alloc::vec::Vec<alloc::string::String, alloc::alloc::Global>> @ 0x000000016fdfe568) at macos.rs:125:40
    frame #9: 0x0000000100065674 kanata`kanata_state_machine::kanata::macos::_$LT$impl$u20$kanata_state_machine..kanata..Kanata$GT$::event_loop::h55858d0114cb8292(kanata=Arc<lock_api::mutex::Mutex<parking_lot::raw_mutex::RawMutex, kanata_state_machine::kanata::Kanata>, alloc::alloc::Global> @ 0x000000016fdfe4b0, tx=SyncSender<kanata_state_machine::oskbd::KeyEvent> @ 0x000000016fdfe4b8) at macos.rs:19:28
    frame #10: 0x0000000100006de4 kanata`kanata::cli::main_impl::h8959fed58563b148 at main.rs:237:9
    frame #11: 0x000000010000b890 kanata`kanata::main::h06f19936078c2382 at main.rs:243:15
    frame #12: 0x0000000100012a2c kanata`core::ops::function::FnOnce::call_once::h084996949d09e969((null)=(kanata`kanata::main::h06f19936078c2382 at main.rs:242), (null)=<unavailable>) at function.rs:250:5
    frame #13: 0x000000010000535c kanata`std::sys_common::backtrace::__rust_begin_short_backtrace::h897b1b9fc820556a(f=(kanata`kanata::main::h06f19936078c2382 at main.rs:242)) at backtrace.rs:155:18
    frame #14: 0x00000001000047d4 kanata`std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::he4fc448df6097e5b at rt.rs:159:18
    frame #15: 0x00000001004ed0fc kanata`std::rt::lang_start_internal::hecc68fef83c8f44d [inlined] core::ops::function::impls::_$LT$impl$u20$core..ops..function..FnOnce$LT$A$GT$$u20$for$u20$$RF$F$GT$::call_once::h2f86a413382a979d at function.rs:284:13 [opt]
    frame #16: 0x00000001004ed0f4 kanata`std::rt::lang_start_internal::hecc68fef83c8f44d [inlined] std::panicking::try::do_call::hd40c9eb4d233b111 at panicking.rs:559:40 [opt]
    frame #17: 0x00000001004ed0f4 kanata`std::rt::lang_start_internal::hecc68fef83c8f44d [inlined] std::panicking::try::h13ac68ffa70c387b at panicking.rs:523:19 [opt]
    frame #18: 0x00000001004ed0f4 kanata`std::rt::lang_start_internal::hecc68fef83c8f44d [inlined] std::panic::catch_unwind::habea7b6fc986e966 at panic.rs:149:14 [opt]
    frame #19: 0x00000001004ed0f4 kanata`std::rt::lang_start_internal::hecc68fef83c8f44d [inlined] std::rt::lang_start_internal::_$u7b$$u7b$closure$u7d$$u7d$::h6b16436250c3cf62 at rt.rs:141:48 [opt]
    frame #20: 0x00000001004ed0f4 kanata`std::rt::lang_start_internal::hecc68fef83c8f44d [inlined] std::panicking::try::do_call::h9970b928a0b20951 at panicking.rs:559:40 [opt]
    frame #21: 0x00000001004ed0f0 kanata`std::rt::lang_start_internal::hecc68fef83c8f44d [inlined] std::panicking::try::h4dfbe3cb4cc8f253 at panicking.rs:523:19 [opt]
    frame #22: 0x00000001004ed0f0 kanata`std::rt::lang_start_internal::hecc68fef83c8f44d [inlined] std::panic::catch_unwind::hf6a5e1e8ce5a10f5 at panic.rs:149:14 [opt]
    frame #23: 0x00000001004ed0f0 kanata`std::rt::lang_start_internal::hecc68fef83c8f44d at rt.rs:141:20 [opt]
    frame #24: 0x00000001000047a0 kanata`std::rt::lang_start::h02008da58a4aad62(main=(kanata`kanata::main::h06f19936078c2382 at main.rs:242), argc=3, argv=0x000000016fdff580, sigpipe='\0') at rt.rs:158:17
    frame #25: 0x000000010000dfd0 kanata`main + 36
    frame #26: 0x000000019edbb154 dyld`start + 2476

The solution is to patch the driverkit dependency and build kanata yourself:

  1. Clone the driverkit repository.
  2. Inside the directory, update the submodules by running
    git submodule update --init --recursive
  3. Edit line 264 in c_str/driverkit.cpp as follows:
@@ -261,7 +261,7 @@ CFStringRef get_property(mach_port_t item, const char* property) {

 template<typename... Args>
 void release_strings(Args... strings) {
-    (CFRelease(strings), ...);
+    ((strings ? CFRelease(strings) : void()), ...);
 }

 bool isSubstring(CFStringRef subString, CFStringRef mainString) {
  1. Go to somewhere else and clone kanata.
  2. Add the following to the end of Cargo.toml to reference the patched copy of driverkit:
@@ -146,3 +146,6 @@ opt-level = "z"
 lto = "fat"
 panic = "abort"
 codegen-units = 1
+
+[patch.crates-io]
+karabiner-driverkit = { path = "/PATH/TO/DRIVERKIT" }
  1. Run cargo build.
  2. The binary should be at target/debug/kanata.

I have got this to work on my macOS 14.7.1. Here's my compiled binary: binary.zip

Hope this helps!

@sgript
Copy link

sgript commented Feb 1, 2025

Thank you @KeroppiMomo you're a hero, worked for me. However, had to do some other tinkering, on MacOS 15.2:

Any other combination did not work for me, but hopefully helps someone else if they have problems also with Karabiner-DriverKit-VirtualHIDDevice 5.0.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working macos Issue pertains to macos; jtroo has no macOS devices and does not maintain the support for this OS.
Projects
None yet
Development

No branches or pull requests

7 participants