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:
1 parent
1456882f16
commit
b4cb34fea9
4 files changed
+131
-2
No files matched your search
@@ -6491,6 +6491,8 @@ bool CMcpServer::WaitTerminalReady(uint64_t device_id, context*& subCtx, bool& i
|
||||
bool signaled = m_TermCv.wait_for(lk, std::chrono::milliseconds(timeoutMs),
|
||||
[&] { return it->second.started; });
|
||||
if (!signaled) {
|
||||
// 放弃:迟到的 shell 子连接就绪包(TOKEN_SHELL_START/TOKEN_TERMINAL_START)不能弹终端窗口。
|
||||
MarkLateFirstPacket(device_id, { (BYTE)TOKEN_SHELL_START, (BYTE)TOKEN_TERMINAL_START });
|
||||
m_TermSessions.erase(it); // 超时 → 清理
|
||||
return false;
|
||||
}
|
||||
@@ -7220,6 +7222,8 @@ bool CMcpServer::WaitPending(uint64_t device_id, std::vector<BYTE>& out, int tim
|
||||
bool signaled = m_PendingCv.wait_for(lk, std::chrono::milliseconds(timeoutMs),
|
||||
[&] { return it->second.done; });
|
||||
if (!signaled || it->second.data.empty()) {
|
||||
// 放弃:客户端可能在我们返回后才把应答首包发上来,登记吸收,别让它弹 MFC 窗口。
|
||||
MarkLateFirstPacketForTool(device_id, it->second.tool);
|
||||
m_Pending.erase(it); // 超时/空数据 → 清理
|
||||
return false;
|
||||
}
|
||||
@@ -7233,6 +7237,54 @@ void CMcpServer::ClearPending(uint64_t device_id) {
|
||||
m_Pending.erase(device_id);
|
||||
}
|
||||
|
||||
// ===== 迟到首包吸收 =====
|
||||
// 工具放弃后 5 分钟内到达的「应答首包」只吸收一次;过期即失效(不长期压制人工操作)。
|
||||
static const int kFirstPacketMarkMs = 5 * 60 * 1000;
|
||||
|
||||
void CMcpServer::MarkLateFirstPacket(uint64_t device_id, std::initializer_list<BYTE> tokens) {
|
||||
if (tokens.size() == 0) return;
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
std::lock_guard<std::mutex> lk(m_FirstPacketMutex);
|
||||
for (auto it = m_FirstPacketMarks.begin(); it != m_FirstPacketMarks.end();) {
|
||||
if (it->second.expiry <= now) it = m_FirstPacketMarks.erase(it);
|
||||
else ++it;
|
||||
}
|
||||
FirstPacketMark mk;
|
||||
mk.tokens.insert(tokens.begin(), tokens.end());
|
||||
mk.expiry = now + std::chrono::milliseconds(kFirstPacketMarkMs);
|
||||
m_FirstPacketMarks[device_id] = std::move(mk);
|
||||
}
|
||||
|
||||
// 工具 → 应答首包 token。只有「子连接第一个包会弹 MFC 窗口」的工具需要登记。
|
||||
void CMcpServer::MarkLateFirstPacketForTool(uint64_t device_id, const std::string& tool) {
|
||||
if (tool == "list_files" || tool == "download_file" || tool == "upload_file" || tool == "search_file")
|
||||
MarkLateFirstPacket(device_id, { (BYTE)TOKEN_DRIVE_LIST });
|
||||
else if (tool == "list_registry")
|
||||
MarkLateFirstPacket(device_id, { (BYTE)TOKEN_REGEDIT });
|
||||
else if (tool == "list_services" || tool == "control_service")
|
||||
MarkLateFirstPacket(device_id, { (BYTE)TOKEN_SERVERLIST });
|
||||
else if (tool == "list_processes" || tool == "kill_process")
|
||||
MarkLateFirstPacket(device_id, { (BYTE)TOKEN_PSLIST });
|
||||
else if (tool == "list_windows" || tool == "operate_window")
|
||||
MarkLateFirstPacket(device_id, { (BYTE)TOKEN_WSLIST });
|
||||
else if (tool == "get_client_log")
|
||||
MarkLateFirstPacket(device_id, { (BYTE)TOKEN_REPORT_LOG });
|
||||
else if (tool == "get_activity_history")
|
||||
MarkLateFirstPacket(device_id, { (BYTE)TOKEN_REPORT_ACTIVITY });
|
||||
}
|
||||
|
||||
bool CMcpServer::TakeToolFirstPacket(uint64_t device_id, BYTE token) {
|
||||
std::lock_guard<std::mutex> lk(m_FirstPacketMutex);
|
||||
auto it = m_FirstPacketMarks.find(device_id);
|
||||
if (it == m_FirstPacketMarks.end()) return false;
|
||||
if (it->second.expiry <= std::chrono::steady_clock::now()) { m_FirstPacketMarks.erase(it); return false; }
|
||||
if (it->second.tokens.find(token) == it->second.tokens.end()) return false; // 不是该工具的应答首包
|
||||
m_FirstPacketMarks.erase(it); // 一次性消费
|
||||
Mprintf("[MCP] absorb late first token=%d dev=%llu (tool gave up earlier)\n",
|
||||
(int)token, (unsigned long long)device_id);
|
||||
return true;
|
||||
}
|
||||
|
||||
// ===== P2c:list_files / get_screenshot 扩展 =====
|
||||
|
||||
bool CMcpServer::BeginPending(uint64_t device_id, const std::string& tool, const std::string& path) {
|
||||
@@ -7479,6 +7531,7 @@ bool CMcpServer::WaitPendingRegistry(uint64_t device_id, std::vector<BYTE>& subk
|
||||
bool signaled = m_PendingCv.wait_for(lk, std::chrono::milliseconds(timeoutMs),
|
||||
[&] { return it->second.done; });
|
||||
if (!signaled) {
|
||||
MarkLateFirstPacketForTool(device_id, it->second.tool); // 放弃后的 TOKEN_REGEDIT 不弹注册表窗口
|
||||
m_Pending.erase(it); // 超时 → 清理
|
||||
return false;
|
||||
}
|
||||
@@ -7566,6 +7619,7 @@ bool CMcpServer::WaitPendingWrite(uint64_t device_id, std::vector<BYTE>& out, in
|
||||
subCtx = it->second.subCtx;
|
||||
Mprintf("[MCP-P7] WaitPendingWrite dev=%llu TIMEOUT: signaled=%d dataEmpty=%d subCtx=%p\n",
|
||||
device_id, signaled ? 1 : 0, it->second.data.empty() ? 1 : 0, (void*)subCtx);
|
||||
MarkLateFirstPacketForTool(device_id, it->second.tool); // 就绪包迟到 → 吸收,不弹进程/服务窗口
|
||||
m_Pending.erase(it); // 超时/空数据 → 清理
|
||||
lk.unlock();
|
||||
if (subCtx) subCtx->CancelIO(); // 锁外关子连接,防孤儿子连接
|
||||
@@ -7873,7 +7927,10 @@ bool CMcpServer::WaitFileTransferDone(uint64_t device_id, int timeoutMs,
|
||||
for (auto& kv : s.fileEntries) files.push_back(kv.second);
|
||||
skipped = (int)s.skipped.size();
|
||||
|
||||
return s.done && s.error == 0;
|
||||
const bool ok = s.done && s.error == 0;
|
||||
// 放弃的文件工具:迟到的 TOKEN_DRIVE_LIST(文件管理器子链接首包)不能弹文件管理器窗口。
|
||||
if (!ok) MarkLateFirstPacketForTool(device_id, s.tool);
|
||||
return ok;
|
||||
}
|
||||
|
||||
void CMcpServer::ClearFileTransfer(uint64_t device_id) {
|
||||
|
||||
Reference in new issue
Block a user