Visual Studio Code, rebuilt in Rust
Rust
fix(keyboard): serialize TIS reads, concurrent first calls abort
rokartur042b7013ff05
diff --git a/native/crates/app/src/keyboard_layout.rs b/native/crates/app/src/keyboard_layout.rsindex a42d9c0af87..a3e33f941c7 100644--- a/native/crates/app/src/keyboard_layout.rs+++ b/native/crates/app/src/keyboard_layout.rs@@ -97,6 +97,17 @@ mod tests {assert_eq!(mapping["Space"]["value"], " ");}+ /// libtest runs tests on many threads: without the lock, the first TIS calls of the process aborted it with+ /// no message (macOS 26 and 27) about 1 run in 5 with 2 threads and every run with 8.+ #[cfg(target_os = "macos")]+ #[test]+ fn layout_reads_from_many_threads_at_once_do_not_abort() {+ let threads: Vec<_> = (0..8).map(|_| std::thread::spawn(|| (current_layout_info(), current_raw_mapping()))).collect();+ for thread in threads {+ assert_eq!(thread.join().unwrap().1["Space"]["value"], " ");+ }+ }+/// Without an X display native-keymap gives no layout and an empty map; Xvfb starts with the `evdev` rules and/// the `us` layout.#[cfg(target_os = "linux")]diff --git a/native/crates/app/src/keyboard_layout/macos.rs b/native/crates/app/src/keyboard_layout/macos.rsindex fc908397c79..2625ba418ef 100644--- a/native/crates/app/src/keyboard_layout/macos.rs+++ b/native/crates/app/src/keyboard_layout/macos.rs@@ -1,6 +1,7 @@//! native-keymap's keyboard_mac.mm: Carbon's current input source and UCKeyTranslate, and AppKit's current event.use std::ffi::c_void;+use std::sync::Mutex;use objc2_foundation::{NSArray, NSString};use serde_json::{Map, Value};@@ -41,9 +42,14 @@ unsafe extern "C" {fn CFRelease(object: *const c_void);}+/// Concurrent first TIS calls of a process abort it with no message (macOS 26 and 27); tests read the layout on+/// libtest's threads.+static TIS: Mutex<()> = Mutex::new(());+/// `getCurrentKeyboardLayout()`: `{ id, localizedName, lang }` of the current input source (IMacKeyboardLayoutInfo).pub fn current_layout_info() -> Value {let mut info = Map::new();+ let _tis = TIS.lock().unwrap();// SAFETY: TIS calls on the copied input source, released once below; each property is null or the type it is cast to (TextInputSources.h).unsafe {let source = TISCopyCurrentKeyboardInputSource();@@ -67,6 +73,7 @@ pub fn current_layout_info() -> Value {/// has no Unicode layout data.pub fn current_raw_mapping() -> Value {let mut mapping = Map::new();+ let _tis = TIS.lock().unwrap();// SAFETY: as above; `layout` points into the layout data, which `source` keeps alive until its release below.unsafe {let mut source = TISCopyCurrentKeyboardInputSource();diff --git a/port/notes.md b/port/notes.mdindex 9718ed14592..b9d86e752e4 100644--- a/port/notes.md+++ b/port/notes.md@@ -237,6 +237,7 @@ Scope as in its Work row of port/ledger.md. Done in this milestone: the command- shortcut: unbound commands with `args` metadata are listed; the port's command registry has no metadata.- Command titles come from the reference: `port/tools/ref-commands` dumps 1.147.0's `MenuRegistry` commands, editor action labels and `MenuId.CommandPalette` items into `command_palette/reference-commands.json`. The Keyboard Shortcuts editor labels a command as `getActionsLabels` does (an extension's `MenuRegistry` title, else the reference label), so `copyFilePath` reads "File: Copy Path of Active File". The palette lists a workbench item only when the port registers its command; extension items always, as they register on activation. `viewContainer.<id>.enabled`, `<view>.active`, `activeEditorIsLastInGroup` and `viewHasSomeCollapsibleResult` are set so the items' `when` clauses hold as in the reference. Rerun the tool when the reference moves.- `keyboard_layout.rs` reads the layout as native-keymap's keyboard_mac.mm does (TIS input source, UCKeyTranslate per DOM code with the dead-key double press and the `iscntrl` filter); its Developer: Inspect Key Mappings (JSON) is byte-identical to native-keymap's `getCurrentKeyboardLayout` and `getKeyMap` on the Studio's Polish Pro layout. The keyboard layout picker is web-only (`workbench.web.main.ts`), so desktop has none.+- Trap: the first TIS calls of a process, made on two threads at once, abort it with SIGABRT and no message (macOS 26 and 27: 2 threads abort about 1 run in 5, 8 threads every run). The two keyboard_layout tests meeting on libtest's threads aborted the app test binary in CI at about test 1240; `keyboard_layout::macos` holds one lock over each TIS read, and `layout_reads_from_many_threads_at_once_do_not_abort` aborts without it.- `keyboard_mapper.rs` ports MacLinuxKeyboardMapper (the macOS branch): scan code combos <-> key code combos from the layout's characters, the missing Latin letters override, CJK punctuation redirects, labels, user settings labels, Electron accelerators, WYSIWYG and dispatch strings, and `dumpDebugInfo`, byte-identical to the mac_de_ch, mac_en_us, mac_zh_hant and mac_zh_hant2 fixtures (`cargo test -p app keyboard_mapper`, the macOS cases of macLinuxKeyboardMapper.test.ts). The mapper of the current input source is process-wide (`keyboard_mapper::current`), made as nativeKeyboardLayout.ts `createKeyboardMapper` does (none, so US key code dispatch, for `keyboard.dispatch: keyCode`, a layout without a key map and Dvorak - QWERTY ⌘) and made again when the input source or those settings change, checked on each native key down.- Keys dispatch by scan code: a key down's DOM `code` and modifiers come from AppKit's current NSEvent (`keyboard_layout::current_key_event`; GPUI's `Keystroke` has no key code), `resolveKeyboardEvent` turns them into a `[Code]` chord, and the resolver matches dispatch strings (`meta+[KeyY]`). With a mapper each rule becomes one rule per scan code keybinding that types it (`_resolveKeybindingItems`), so labels everywhere (menus, palette, Keyboard Shortcuts editor, chords) are the layout's: Cmd+Z reads ⌘Z and dispatches on KeyY on Swiss German. Tests run without a mapper, on the US key code dispatch of FallbackKeyboardMapper. `Chord` is a key code or a scan code chord (`[Comma]` in keybindings.json stays a scan code), equal only to its own kind as `getHashCode`.- Developer: Inspect Key Mappings opens `_dumpDebugInfo`: layout info, the default and user keybindings as the layout resolves them (`_dumpResolveKeybindingDebugInfo`, every row starting with its input as upstream's `isFirst` never clears), the mapper's table and the raw key map.