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
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-12-05 15:33:57 +01:00
1 parent 3683b4ebeb
commit ea5d69d1c0
2 files changed
+21 -13

No files matched your search

+2
View File
@@ -278,6 +278,8 @@ bool CFilePreviewMgr::ObserveChunk(CONTEXT_OBJECT* ctx, const FileChunkPacketV2*
std::string safeName(name, nameLen); std::string safeName(name, nameLen);
std::lock_guard<std::mutex> lk(m_mtx); std::lock_guard<std::mutex> lk(m_mtx);
if (m_reqs.empty() && m_tombstones.empty())
return true; // 普通 V2 传输的热路径:没有预览会话时不做任何扫描
Request* req = FindByTransferLocked(ctx, pkt->transferID); Request* req = FindByTransferLocked(ctx, pkt->transferID);
if (req) { if (req) {
+19 -13
View File
@@ -14607,12 +14607,14 @@ bool CMcpServer::BeginFileTransferPending(uint64_t device_id, const std::string&
if (!DeviceTransferGate::Instance().TryClaim(device_id, owner)) return false; if (!DeviceTransferGate::Instance().TryClaim(device_id, owner)) return false;
// 与一次性挂起请求互斥(F2):先查对方注册表再查自己的,避免跨锁嵌套死锁。 // 与一次性挂起请求互斥(F2):先查对方注册表再查自己的,避免跨锁嵌套死锁。
bool pendingBusy = false;
{ {
std::lock_guard<std::mutex> lk(m_PendingMutex); std::lock_guard<std::mutex> lk(m_PendingMutex);
if (m_Pending.find(device_id) != m_Pending.end()) { pendingBusy = m_Pending.find(device_id) != m_Pending.end();
DeviceTransferGate::Instance().Release(device_id, owner); }
return false; if (pendingBusy) {
} DeviceTransferGate::Instance().Release(device_id, owner);
return false;
} }
bool registered = 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; if (!DeviceTransferGate::Instance().TryClaim(device_id, owner)) return false;
// 与一次性挂起请求互斥(F2):先查对方注册表再查自己的,避免跨锁嵌套死锁。 // 与一次性挂起请求互斥(F2):先查对方注册表再查自己的,避免跨锁嵌套死锁。
bool pendingBusy = false;
{ {
std::lock_guard<std::mutex> lk(m_PendingMutex); std::lock_guard<std::mutex> lk(m_PendingMutex);
if (m_Pending.find(device_id) != m_Pending.end()) { pendingBusy = m_Pending.find(device_id) != m_Pending.end();
DeviceTransferGate::Instance().Release(device_id, owner); }
return false; if (pendingBusy) {
} DeviceTransferGate::Instance().Release(device_id, owner);
return false;
} }
bool registered = 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); const uint64_t owner = s_ownerSeq.fetch_add(1);
if (!DeviceTransferGate::Instance().TryClaimPair(src_id, dst_id, owner)) return false; if (!DeviceTransferGate::Instance().TryClaimPair(src_id, dst_id, owner)) return false;
bool pendingBusy = false;
{ {
// 与一次性挂起请求互斥(F2):先查对方注册表再查自己的,避免跨锁嵌套死锁。 // 与一次性挂起请求互斥(F2):先查对方注册表再查自己的,避免跨锁嵌套死锁。
std::lock_guard<std::mutex> lk(m_PendingMutex); std::lock_guard<std::mutex> lk(m_PendingMutex);
if (m_Pending.find(src_id) != m_Pending.end() || pendingBusy = (m_Pending.find(src_id) != m_Pending.end() ||
m_Pending.find(dst_id) != m_Pending.end()) { m_Pending.find(dst_id) != m_Pending.end());
DeviceTransferGate::Instance().ReleasePair(src_id, dst_id, owner); }
return false; if (pendingBusy) {
} DeviceTransferGate::Instance().ReleasePair(src_id, dst_id, owner);
return false;
} }
bool registered = false; bool registered = false;
{ {