[Bug] nightly(0.17.4.98)打字"卡一下"、有时只出英文或候选窗不刷新,切换输入法即恢复
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- Over a week
- Newbie friendliness
- 18/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- cpp
- Domain
- desktop-dev
Research direction
The suspected chain runs through WeaselUI/WeaselPanel.cpp (DoPaint, Present(1,0), Refresh, _ResizeWindow) and WeaselIPCServer/WeaselServerImpl.cpp under g_api_mutex, plus include/PipeChannel.h where _Send passes HANDLE* to _WritePipe. A first step is to check whether the _Send pointer-as-handle mistake is real and reproduce the dropped keystroke. Done means the reporter's chain is confirmed or refuted, with the maintainers' view on the snapshot approach; the full snapshot refactor is design work.
Written by the indexing model from the issue text.
Description
先说明立场:这些代码我不太懂,下面的机制链是翻源码(nightly
f96ed24)猜测;但「一卡一卡」是我本机反复吃
环境
- Windows 11 26H2(10.0.26300)
- 小狼毫 nightly
0.17.4.98.f96ed24(不是 0.17.4 stable) - 主要宿主:Firefox(在若干 Electron 宿主上也见过同类现象)
- 方案:魔然 moran;
inline_preedit: false WeaselServer.exe未崩溃:同一 PID 从 11-17 一直存活至今
症状:同一个"卡住"状态的三种外观
- 打字卡一下:键按下去没有反应,随后一串字母直接上屏,和纯英文输入一样;
- 有时候选窗/预编辑不刷新("预览不出来"),但会话并没有断;
- 换一次输入法(或切到别的窗口再切回来)立刻恢复,不需要重启服务;
- 全程没有崩溃提示,任务管理器里服务进程照旧。
我认为的机制链(推测,待证伪)
服务端线程模型是:UI 窗口归消息线程,IPC 每个连接一条 boost::thread,而所有 IPC 处理都串行在同一把全局锁下(WeaselIPCServer/WeaselServerImpl.cpp:std::lock_guard guard(g_api_mutex);)。
nightly 的 DirectComposition 重写(#1927)在 WeaselPanel::DoPaint() 里用了同步垂直同步呈现:
// WeaselUI/WeaselPanel.cpp(f96ed24)
HRESULT hrPresent = m_pD2D->swapChain->Present(1, 0); // sync
而重绘是由 IPC 管道线程驱动的:
_UpdateUI → UI::Update → WeaselPanel::Refresh
→ _ResizeWindow() → SetWindowPos(...) // 跨线程同步发送给消息线程
于是可能是这样一条链:
消息线程卡在 Present(1,0)(候选窗被遮挡 / DWM 节流 / 显示器休眠 / 显卡驱动抖动)
→ 管道线程卡在 SetWindowPos 等消息线程
→ 而它此刻仍持有 g_api_mutex
→ 该宿主后续的所有 IPC 全排队
→ 宿主进程里的 weasel.dll 在无超时的 ReadFile 上阻塞
→ 表现为「卡一下、不吃键」,UI 停在旧帧 → 「预览不出来」
切输入法 → Deactivate/Activate(重建 session 与管道)→ 整条链被换掉 → 恢复
有限佐证(不足以定论)
- 0.17.4 的绘制路径里
Present出现 0 次(GDI/GDI+),Present(1,0)是 nightly 才引入的。但 0.17.4 的刷新同样是跨线程的——所以"跨线程"不是新问题,"消息线程可能被长时间阻塞"才是。 - 本机
%TEMP%\rime.weasel有 16 个*-weasel.dll-*.dmp(ZCode / Qoder / Obsidian / DSH / 豆包 / msedgewebview2 / scrcpy,11-18 至 11-21)。解析后 15 个异常码是0x80000003 @ KERNELBASE(断点/中止型,不是访问违例)。但这只能算旁证:dllmain.cpp的SetUnhandledExceptionFilter是进程级的,也可能记录到宿主自身的异常。 - 服务端日志里有
service.cc:141] Recycled N stale sessions(librime 回收闲置会话)。不过会话回收本身只会丢一个键,撑不起"持续卡到切输入法"。
顺带发现一处确定的缺陷
include/PipeChannel.h 的 _Send() 在重连后重试:
_WritePipe(_GetPipeHandle(), data_sz, pbuff);
_GetPipeHandle() 返回 HANDLE*,而 _WritePipe(HANDLE, size_t, char*) 的形参是 HANDLE——指针被当成句柄交给 WriteFile,这次重试必然失败;而且异常会跳过 ClearBufferStream(),把 has_body/写流残留到下一次发送。
这条会让"管道恰好断开的那一次按键"直接丢掉:Echo() 拿到 0 → _EnsureServerConnected() 返回 false → pfEaten = FALSE → 字母直上屏。与症状 1 吻合。
建议的修法:全量编组(UI 线程快照模式)
核心思路:管道线程不再直接操作窗口/设备,只发布一份不可变快照,让窗口线程自己去改。
- 管道线程只做三件事:解析 IPC → 把
Context / Status / UIStyle深拷贝成快照 →PostMessage(hwnd, WM_APP_REFRESH, 0, snapshot)。- 这里必须拷贝,不能传引用:现在
WeaselPanel持有的是Context& / Status& / UIStyle&(引用 UI 对象),把引用搬到另一个线程读,会和管道线程的写入竞争。#1867 的作者也指过这一点("Refresh()涉及UIStyle/Status中的字符串引用,不能只靠PostMessage简单搬线程")。
- 这里必须拷贝,不能传引用:现在
- 消息线程收到后消费快照,把
AttachWindow / InitDirect2D / InitDirectWriteResources / _CreateLayout / DoLayout / _ResizeWindow / _Reposition / Show / Hide全部在这里做完,再释放快照。 - 设备丢失的
Reset() + InitDirect2D()也只在消息线程做。现在Refresh()和D2D::OnResize()都会在管道线程里 reset 共享设备(DeviceResources还是无锁全局单例),属于跨线程释放 COM 对象。
收益:SetWindowPos / ShowWindow / ResizeBuffers / Present 全部收敛到单一线程;g_api_mutex 不会因为"等 UI 线程"被长时间占住;剩余阻塞点只剩"服务端真在算"的那一段。
短期缓解(都能独立合入,与上面的重构正交)
Present(1, 0)→Present(0, 0)(去掉 vsync 等待),一行改动。_Send()里*_GetPipeHandle(),并保证异常路径也ClearBufferStream()。_Connect()的while (...) WaitNamedPipe(...)加上限(现在是无界循环)。与 #1923 / #1914 同方向,不冲突。
与已有条目的关系(发帖前已查重,供维护者判断)
- #1927(已合):DirectComposition 重写,
Present(1,0)正是这次引入的。 - #1867(CLOSED,未合):已经提出过"UI 更新迁到 UI 线程"的方向,并在"残余风险"里写明未做。本 issue 想把它补上,并给出快照方案。
- #1923(CLOSED,未合):客户端 pipe 有界 I/O;#1914(OPEN):同方向仍未合。
- #1487(CLOSED):管道线程并发调 librime,
g_api_mutex就是那次加的。
想请维护者确认
- 这条"消息线程被
Present卡住 → 持锁的管道线程在SetWindowPos上等 → 整个 IPC 队列阻塞"的链条,是否符合当前设计的实际行为? - 全量编组(快照 +
PostMessage)这个方向能不能接受? - 如果我哪里读错了,请直接说,我按你的纠正重做。
补充:本机
weasel.dll在 System32 与 Program Files 的时间戳一致(都是 10-31 构建),不存在 server/client 半安装混装,所以这次不是 #1983/#1985 那条链。
- Dominant language
- C++
- Stars
- 8.4k
- Forks
- 878
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from rime/weasel
-
Bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
不能鼠标选词OpenBug
Difficulty 4/5 3-5 days Newbie friendliness 48/100
-
Bug
Difficulty 3/5 1-2 days Newbie friendliness 22/100
-
Bug
Difficulty 3/5 1-2 days Newbie friendliness 48/100
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
microsoft/DirectXShaderCompiler#9173 ·
Maintainers usually reply within 2 days
-
feature:config kind:docs os:windows toolchain:cmake triaged
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
arch-based bug niri
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
noctalia-dev/noctalia#5552 ·
Maintainers usually reply within 1 day