Skip to content

fix(WeaselUI,WeaselIPC): avoid UI-thread stalls and cross-thread device resets - #1989

Draft
takomitkm wants to merge 1 commit into
rime:masterfrom
takomitkm:ui-ipc-stall
Draft

takomitkm wants to merge 1 commit into
rime:masterfrom
takomitkm:ui-ipc-stall

Conversation

@takomitkm

@takomitkm takomitkm commented Nov 22, 2026 •

Copy link
Copy Markdown

概要

nightly(0.17.4.98.f96ed24)上偶发「打字卡一下 / 只出英文 / 候选窗不刷新,切一次输入法就恢复」。本 PR 修的是四处会让 UI 线程与 IPC 队列互相拖死、以及跨线程释放 D3D 设备的点;不改 IPC 协议、不改外观。

问题描述与现有证据在 #1988;成因脉络(2011 消息线程 → 2018 命名管道 + 每连接线程 → 2025 g_api_mutex → 2026-10 DComp 同步 Present)也补在 #1988 的讨论里。

这是 draft:先就方向征求意见,认可后再转 ready。

改动(5 文件,+58 / −21)

  1. WeaselUI/WeaselPanel.cpp:Present(1, 0) → Present(0, 0)
    DoPaint() 跑在服务端消息线程。候选窗被遮挡 / DWM 节流 / 显示器休眠时,同步 Present 可能长时间不返回;这段时间管道线程正卡在 Refresh() → _ResizeWindow() → SetWindowPos(跨线程同步发送)上等消息线程,而它仍持有 g_api_mutex —— 该宿主后续 IPC 全部排队,宿主里的 weasel.dll 又在无超时的 ReadFile 上阻塞。改为不等垂直同步,并把 DXGI_ERROR_WAS_STILL_DRAWING 当正常情况忽略。

  2. WeaselPanel.cpp / WeaselUI/d2d.cpp:设备释放与重建只在消息线程
    Refresh()(管道线程)与 D2D::OnResize()(同样由 Refresh 进入)原本会 DeviceResources::Get().Reset() + InitDirect2D() —— 在非窗口线程释放、重建共享 D3D/DComposition 设备,而消息线程可能正同时在 DoPaint() 里使用它。现在这两处不再动设备,恢复统一放到 DoPaint()(WM_PAINT,消息线程);同时把 DoPaint() 的恢复判断提前到 resource-null 守卫之前,避免「上次初始化失败后便再也不重试」。

  3. WeaselUI/d2d.{h,cpp}:DeviceResources 加锁
    进程级单例原本无锁,EnsureInitialized() 与 Reset() 加 std::mutex,防止两线程并发创建/释放。

  4. include/PipeChannel.h、WeaselIPC/PipeChannel.cpp:两处传输层缺陷

    • _WritePipe(_GetPipeHandle(), …) 把 HANDLE* 当 HANDLE(形参是 HANDLE)交给 WriteFile,重连后的重试必然失败;且异常会跳过 ClearBufferStream(),把 has_body / 写流残留进下一次发送。改为 *_GetPipeHandle(),并保证异常路径也清 send-context。
    • _Connect() 的 while (_Invalid(pipe = _TryConnect())) WaitNamedPipe(name, 500); 无上限,且运行在宿主 UI 线程;服务端实例持续全忙时会死循环。改为约 2s 上限后放弃(退化成「这一次不吃键」,而不是冻死)。

验证

fork 上以与上游相同的工具链(windows-2022 / VS2022 v143)完整构建:成功

说明:上游 ci.yml 里的 get-rime.ps1 未带 token,在 fork 环境容易撞匿名 rate limit 并静默失败(fork 的 build.yml 注释里记录了这个坑),所以上面的链接用的是 fork 里那份等价 workflow;本 PR 不包含该 workflow 文件。

没有做的事(边界)

  • 没有做「全量编组」:即把 Show / Hide / MoveTo / _CreateLayout / Refresh 全部投递回窗口线程,并用快照替代 Context& / Status& / UIStyle& 这些引用。那是更彻底的解法,但改动面大;降低按键延迟并修复服务生命周期通知 #1867 的作者也指出 Refresh() 涉及字符串引用,「不能只靠 PostMessage 简单搬线程」。本 PR 只做「去掉所有无界阻塞 + 去掉所有跨线程设备释放」。
  • 首次 attach 时 AttachWindow → InitDirect2D 仍在管道线程(一次性、窗口尚未显示),未改动。

与已有条目的关系(提 PR 前已查重)

Root-cause hardening for 'typing freezes / English-only / no candidate window
until I switch IME' on the DirectComposition rewrite (nightly 0.17.4.98).

- WeaselPanel: use a non-vsync Present(0,0) instead of the synchronous
  Present(1,0). DoPaint runs on the server message thread; while occluded /
  compositor-throttled / display asleep the sync Present can block for a long
  time, during which the server holds g_api_mutex and every queued IPC request
  from the host stalls (keys stop being eaten -> letters go straight through).
- WeaselPanel/D2D: never release or recreate DeviceResources from the IPC pipe
  thread. Refresh() and D2D::OnResize() deferred recovery to DoPaint(), which
  runs on the window's own message thread, so device release/recreation is
  single-threaded (fixes use-after-recreate / torn frames).
- DeviceResources: guard EnsureInitialized()/Reset() with a mutex.
- PipeChannel: _WritePipe(_GetPipeHandle(), ...) passed a HANDLE* where a
  HANDLE was expected, so the post-reconnect retry always failed and skipped
  ClearBufferStream(); dereference it and always clear the send context.
- PipeChannel: bound the client connect retry loop (~2s) instead of spinning
  forever on the host UI thread.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant