From ea5d69d1c090f4c1b53f8be60a7981b839d81bff Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Sat, 5 Dec 2026 15:33:57 +0100 Subject: [PATCH] Fix: keep the device gate outside the MCP pending lock Release the DeviceTransferGate only after leaving m_PendingMutex in the three MCP begin paths, matching the documented gate -> pending -> xfer order (the gate mutex is brief, but this keeps the order explicit and avoids future nesting mistakes). Also short-circuit ObserveChunk when there are no preview requests or tombstones, so ordinary V2 chunk paths do not scan the preview maps. Co-Authored-By: deepseek-flash --- server/2015Remote/FilePreview.cpp | 2 ++ server/2015Remote/McpServer.cpp | 32 ++++++++++++++++++------------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/server/2015Remote/FilePreview.cpp b/server/2015Remote/FilePreview.cpp index 3fc9991..ffc913e 100644 --- a/server/2015Remote/FilePreview.cpp +++ b/server/2015Remote/FilePreview.cpp @@ -278,6 +278,8 @@ bool CFilePreviewMgr::ObserveChunk(CONTEXT_OBJECT* ctx, const FileChunkPacketV2* std::string safeName(name, nameLen); std::lock_guard lk(m_mtx); + if (m_reqs.empty() && m_tombstones.empty()) + return true; // 普通 V2 传输的热路径:没有预览会话时不做任何扫描 Request* req = FindByTransferLocked(ctx, pkt->transferID); if (req) { diff --git a/server/2015Remote/McpServer.cpp b/server/2015Remote/McpServer.cpp index fe81f1e..e542750 100644 --- a/server/2015Remote/McpServer.cpp +++ b/server/2015Remote/McpServer.cpp @@ -14607,12 +14607,14 @@ bool CMcpServer::BeginFileTransferPending(uint64_t device_id, const std::string& if (!DeviceTransferGate::Instance().TryClaim(device_id, owner)) return false; // 与一次性挂起请求互斥(F2):先查对方注册表再查自己的,避免跨锁嵌套死锁。 + bool pendingBusy = false; { std::lock_guard lk(m_PendingMutex); - if (m_Pending.find(device_id) != m_Pending.end()) { - DeviceTransferGate::Instance().Release(device_id, owner); - return false; - } + pendingBusy = m_Pending.find(device_id) != m_Pending.end(); + } + if (pendingBusy) { + DeviceTransferGate::Instance().Release(device_id, owner); + return false; } bool registered = false; { @@ -14644,12 +14646,14 @@ bool CMcpServer::BeginFileUpload(uint64_t device_id, uint64_t transfer_id) { if (!DeviceTransferGate::Instance().TryClaim(device_id, owner)) return false; // 与一次性挂起请求互斥(F2):先查对方注册表再查自己的,避免跨锁嵌套死锁。 + bool pendingBusy = false; { std::lock_guard lk(m_PendingMutex); - if (m_Pending.find(device_id) != m_Pending.end()) { - DeviceTransferGate::Instance().Release(device_id, owner); - return false; - } + pendingBusy = m_Pending.find(device_id) != m_Pending.end(); + } + if (pendingBusy) { + DeviceTransferGate::Instance().Release(device_id, owner); + return false; } bool registered = false; { @@ -14685,14 +14689,16 @@ bool CMcpServer::BeginC2CTransfer(uint64_t src_id, uint64_t dst_id, uint64_t tra const uint64_t owner = s_ownerSeq.fetch_add(1); if (!DeviceTransferGate::Instance().TryClaimPair(src_id, dst_id, owner)) return false; + bool pendingBusy = false; { // 与一次性挂起请求互斥(F2):先查对方注册表再查自己的,避免跨锁嵌套死锁。 std::lock_guard lk(m_PendingMutex); - if (m_Pending.find(src_id) != m_Pending.end() || - m_Pending.find(dst_id) != m_Pending.end()) { - DeviceTransferGate::Instance().ReleasePair(src_id, dst_id, owner); - return false; - } + pendingBusy = (m_Pending.find(src_id) != m_Pending.end() || + m_Pending.find(dst_id) != m_Pending.end()); + } + if (pendingBusy) { + DeviceTransferGate::Instance().ReleasePair(src_id, dst_id, owner); + return false; } bool registered = false; {