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

Crash on macOS when using Window::set_ime_position from another thread #2756

Closed
amatho opened this issue Apr 3, 2023 · 2 comments · Fixed by #2757
Closed

Crash on macOS when using Window::set_ime_position from another thread #2756

amatho opened this issue Apr 3, 2023 · 2 comments · Fixed by #2757
Labels
B - bug Dang, that shouldn't have happened DS - macos

Comments

@amatho
Copy link
Contributor

amatho commented Apr 3, 2023

Calling Window::set_ime_position in another thread crashes the program on macOS. The issue is not present on Linux (and presumably not on Windows either). I'm running on aarch64 but I would guess this also happens on x86_64. Minimal repro:

use std::{sync::Arc, thread, time::Duration};

use winit::{
    dpi::LogicalPosition,
    event::{Event, WindowEvent},
    event_loop::EventLoop,
    window::WindowBuilder,
};

fn main() {
    let event_loop = EventLoop::new();
    let window = WindowBuilder::new().build(&event_loop).unwrap();
    let window = Arc::new(window);

    thread::spawn(move || {
        // Wait for event loop to initialize
        thread::sleep(Duration::from_secs(1));

        // This crashes the program on macOS
        window.set_ime_position(LogicalPosition::new(42, 42));
    });

    event_loop.run(move |event, _, control_flow| {
        control_flow.set_wait();

        if let Event::WindowEvent {
            event: WindowEvent::CloseRequested,
            ..
        } = event
        {
            control_flow.set_exit();
        }
    });
}

Backtrace from LLDB:

* thread #4, stop reason = EXC_BREAKPOINT (code=1, subcode=0x19e4ac924)
  * frame #0: 0x000000019e4ac924 libdispatch.dylib`_dispatch_assert_queue_fail + 120
    frame #1: 0x000000019e4ac8ac libdispatch.dylib`dispatch_assert_queue + 196
    frame #2: 0x00000001a7df26a4 HIToolbox`islGetInputSourceListWithAdditions + 160
    frame #3: 0x00000001a7df4ccc HIToolbox`isValidateInputSourceRef + 92
    frame #4: 0x00000001a7df4b8c HIToolbox`TSMGetInputSourceProperty + 44
    frame #5: 0x00000001a7e0b7b8 HIToolbox`utInputSourceIsKeyboardInputMethod + 36
    frame #6: 0x00000001a7e1059c HIToolbox`utInvalidateThisDocsInputMethodClientGeometry + 48
    frame #7: 0x00000001a7e10510 HIToolbox`TSMInvalidateClientGeometry + 208
    frame #8: 0x00000001a1b27274 AppKit`-[NSTextInputContext invalidateCharacterCoordinates] + 60
    frame #9: 0x0000000100086d84 winit-macos-ime-crash`_$LT$$LP$$RP$$u20$as$u20$objc2..message..MessageArguments$GT$::__invoke::h435e508f485b56a3(imp=(libobjc.A.dylib`objc_msgSend), obj=0x0000600002c03900, sel=Sel @ 0x000000017011e9c0, (null)=<unavailable>) at mod.rs:390:26
    frame #10: 0x0000000100087a78 winit-macos-ime-crash`objc2::message::platform::send_unverified::_$u7b$$u7b$closure$u7d$$u7d$::h14e3c02a894abdde at mod.rs:36:33
    frame #11: 0x0000000100086b0c winit-macos-ime-crash`objc2::message::conditional_try::hd7f23106e54cd850(f=<unavailable>) at mod.rs:30:5
    frame #12: 0x0000000100087258 winit-macos-ime-crash`objc2::message::platform::send_unverified::h15e9a754cf8be473(receiver=0x0000600002c03900, sel=Sel @ 0x000000017011ea40, args=<unavailable>) at mod.rs:36:14
    frame #13: 0x0000000100038164 winit-macos-ime-crash`objc2::message::MessageReceiver::send_message::h90f62d9a95d2ee1a(self=0x0000600002c03900, sel=Sel @ 0x000000017011eaa8, args=<unavailable>) at mod.rs:195:46
    frame #14: 0x000000010003c1fc winit-macos-ime-crash`winit::platform_impl::platform::appkit::text_input_context::NSTextInputContext::invalidateCharacterCoordinates::h781262856ac9771d(self=0x0000600002c03900) at __rewrite_self_arg.rs:42:21
    frame #15: 0x000000010001ed44 winit-macos-ime-crash`winit::platform_impl::platform::view::WinitView::set_ime_position::h0c0e80aaf57481f7(self=0x000000010040f100, position=(x = 42, y = 42)) at view.rs:940:9
    frame #16: 0x000000010000b70c winit-macos-ime-crash`winit::platform_impl::platform::window::WinitWindow::set_ime_position::h970e43686f4e6761(self=0x000000010053ab90, spot=Position @ 0x000000017011ebf0) at window.rs:1169:9
    frame #17: 0x0000000100005a98 winit-macos-ime-crash`winit::window::Window::set_ime_position::h77c1fa87ea2725a2(self=0x00006000002306f0, position=(x = 42, y = 42)) at window.rs:1027:9
    frame #18: 0x0000000100008f68 winit-macos-ime-crash`winit_macos_ime_crash::main::_$u7b$$u7b$closure$u7d$$u7d$::h9e9d3fab29932e26 at main.rs:20:9
...

Would have loved to create a PR, but I unfortunately have no experience with Objective-C or OS windowing APIs in general. Hope this helps!

@kchibisov kchibisov added B - bug Dang, that shouldn't have happened DS - macos labels Apr 3, 2023
@kchibisov
Copy link
Member

Seems like we need to wrap the call to execute it on the main thread (we already do that for multiple stuff on macOS).

@amatho
Copy link
Contributor Author

amatho commented Apr 3, 2023

I've tried my hand at fixing this in #2757, feel free to give feedback! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
B - bug Dang, that shouldn't have happened DS - macos
Development

Successfully merging a pull request may close this issue.

2 participants