From 900bab10d6e6d27e848ba6eecb0e3d45fbee2a06 Mon Sep 17 00:00:00 2001 From: KOGA Mitsuhiro Date: Sun, 7 Jun 2026 16:17:27 +0900 Subject: [PATCH] Fix RefCell borrow conflict when toggling IME on Windows (#7529) * Fix RefCell borrow conflict when toggling IME on Windows When IME is active, the `key` function was holding a borrow on the window's inner RefCell while calling `TranslateMessage`. This Windows API can trigger other window messages (like WM_SIZE) via `CtfImeCreateInputContext`, which then attempt to borrow the same RefCell, causing a panic with "RefCell already borrowed". Similarly, `ime_set_context` was holding a borrow while calling `DefWindowProcW`, which can also trigger recursive window messages. Fix by dropping the borrow prior to triggering potentially recursive calls. Fixes the crash that occurred when toggling IME (e.g., pressing Hankaku/Zenkaku key) after splitting a pane on Windows. --- window/src/os/windows/window.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/window/src/os/windows/window.rs b/window/src/os/windows/window.rs index 69e516d14..adce38928 100644 --- a/window/src/os/windows/window.rs +++ b/window/src/os/windows/window.rs @@ -2068,14 +2068,19 @@ unsafe fn ime_set_context( wparam: WPARAM, lparam: LPARAM, ) -> Option { - let inner = rc_from_hwnd(hwnd)?; - let inner = inner.borrow_mut(); + let use_system_rendering = { + let inner = rc_from_hwnd(hwnd)?; + let inner = inner.borrow(); + inner.config.ime_preedit_rendering == ImePreeditRendering::System + }; - if inner.config.ime_preedit_rendering == ImePreeditRendering::System { + if use_system_rendering { return None; } - // Don't show system CompositionWindow because application itself draws it + // Don't show system CompositionWindow because application itself draws it. + // Note: DefWindowProcW may trigger other window messages, so we must + // release the borrow before calling it. let lparam = lparam & !(ISC_SHOWUICOMPOSITIONWINDOW as LPARAM); let result = DefWindowProcW(hwnd, msg, wparam, lparam); Some(result) @@ -2523,6 +2528,11 @@ unsafe fn key(hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM) -> Option