Fix: Stop MCP tool timeouts from popping MFC dialogs

A sub-connection's first packet doubles as the readiness packet: the file,
service, registry, process, log and terminal tools all get answered that way.
Once a tool has given up (timeout or failure) its pending entry is gone, so if
the client only gets around to sending that first packet afterwards,
MessageHandle cannot tell it apart from a human click and opens the matching
modeless dialog - a File Manager window appeared while timeouts were being
exercised on this machine.

The wait paths now record, per device, which first-packet token the abandoned
tool would have sent (MarkLateFirstPacketForTool from WaitPending,
WaitPendingRegistry, WaitPendingWrite, WaitFileTransferDone and
WaitTerminalReady), and every dialog-opening case consumes that marker before
falling back to the MFC path. The marker is one-shot, expires after five
minutes and is never set on the success path, so normal use is unaffected; the
worst case is that a single manual open on a host that just had an MCP timeout
is swallowed.

ClientMsg packets are dropped with a log line while a tool is pending for that
host: new clients still send the legacy popup message alongside the structured
failure token, which would otherwise put a modal box on the server UI during an
MCP call.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-10-25 18:12:56 +01:00
1 parent 1456882f16
commit b4cb34fea9
4 files changed
+131 -2

No files matched your search

+21 -1
View File
@@ -322,6 +322,15 @@ public:
// 工具线程:等待写确认包(第二个同 token 包);超时/空数据清挂起并 CancelIO 子连接。
bool WaitPendingWrite(uint64_t device_id, std::vector<BYTE>& out, int timeoutMs);
// ===== 迟到首包吸收(工具放弃后不再弹出 MFC 对话框)=====
// 工具超时/失败清掉挂起后,客户端仍可能才把应答首包发上来(列盘、服务列表、注册表、
// 进程列表、终端都是「子连接建成即发」)。那时已无挂起会话,MessageHandle 会把它当成
// 人手点击而打开文件管理器/服务管理器/注册表/进程窗口/日志/终端窗口。
// 工具放弃时按设备登记该工具的应答首包 token;包到达时一次性吸收(调用方关子连接、
// 不弹窗)。命中即消费,过期即失效,绝不长期压制人工操作。
// MessageHandle 各对话框分支开头调用(早于挂起判断:成功路径同样消费,不留残留)。
bool TakeToolFirstPacket(uint64_t device_id, BYTE token);
// OfflineProc 调用:接管的子连接断开时清挂起(置 done 并清 subCtx,唤醒等待线程)。
// 不擦表(由 WaitPendingWrite 持有迭代器时自行擦除,防迭代器失效);清 subCtx 使
// 超时路径不会对已回收(连接池复用)的 context 误 CancelIO。
@@ -359,7 +368,7 @@ public:
bool IsKeylogEnabled() const { return m_keylogEnabled; }
// ===== 工具默认等待超时([settings] McpToolTimeoutMs,ini-only 键)=====
// 慢主机上 20s 固定的工具超时太紧;调到 60s 更实用。范围 clamp 到 [1000, 120000]。
// 慢主机上 20s 固定的工具超时太紧;调到 60s 更实用。范围 clamp 到 [1000, 600000]。
// 改动需重启服务端生效(与其它 Mcp* 开关一致)。
void SetToolTimeoutMs(int ms);
int ToolTimeoutMs() const { return m_toolTimeoutMs; }
@@ -554,6 +563,17 @@ private:
// 清理过期墓碑(持 m_PendingMutex 调用)。
void PruneOrphanMarksLocked(std::chrono::steady_clock::time_point now);
// ===== 迟到首包吸收(受 m_FirstPacketMutex 保护,独立锁避免与挂起/传输表形成锁序)=====
// 等待函数在「超时/失败」分支调用:登记该工具放弃后可能迟到的应答首包 token。
struct FirstPacketMark {
std::set<BYTE> tokens;
std::chrono::steady_clock::time_point expiry;
};
std::mutex m_FirstPacketMutex;
std::map<uint64_t, FirstPacketMark> m_FirstPacketMarks;
void MarkLateFirstPacket(uint64_t device_id, std::initializer_list<BYTE> tokens);
void MarkLateFirstPacketForTool(uint64_t device_id, const std::string& tool);
// 路由表大小(已绑定 + 已吸收的子连接数)。MessageHandle 每个包都会调用
// OnManagedSubPacket,若无会话在飞则用它无锁快速返回,避免音频/屏幕等高频
// 子连接逐包争用 m_PendingMutex。只在持锁时更新(store)。