Fix: Clear the file transfer session when its sub-connection drops

A file transfer session holds raw pointers to its file-manager and
stream sub-connections, but nothing on the offline path looked at them.
OfflineProc cleans up terminal, remote-control and chat sessions and
skipped file transfers entirely, so a client that disconnected in the
middle of a transfer left the session pointing at a context that went
back to the connection pool. When the tool thread timed out it called
CancelIO on that stale pointer, which could cancel somebody else's
connection.

Add a context-to-device table next to the sessions, kept under the same
mutex and erased by ClearFileTransfer so a recycled pointer is never
mistaken for a live one. OfflineProc now calls
OnFileTransferContextClosed, which drops both pointers. A dropped
stream sub-connection means the transfer cannot complete, so it also
fails the session with error 1003 and wakes the waiting thread; the
file-manager sub-connection only issues the command and its loss is not
treated as a failure of the transfer itself.

Verified against the debug client: killing it 1.2 s into a 500 MB
download returns "Connection lost: the transfer sub-connection closed"
after about 1 s instead of waiting out timeout_ms, and the server stays
up. After the client restarts, two consecutive downloads and one upload
still work.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-10-25 14:20:13 +01:00
1 parent 39f0928de7
commit 16f23e164c
4 files changed
+49 -2

No files matched your search

+34 -1
View File
@@ -4263,6 +4263,7 @@ std::string BuildDownloadFile(const Json::Value& id, const Json::Value& args, CM
const char* msg = (error == 1002) ? "Timeout waiting for file transfer"
: (error == 1001) ? "Path traversal detected in received path"
: (error == 1003) ? "Connection lost: the transfer sub-connection closed"
: (error == FEV2_HASH_MISMATCH) ? "File SHA-256 verification failed"
: "File transfer failed";
return BuildError(id, -32001, msg);
@@ -7014,6 +7015,7 @@ bool CMcpServer::OnDownloadDriveList(uint64_t device_id, context* fmSubCtx) {
FileTransferSession& s = it->second;
if (s.fmSubCtx != nullptr) return true; // 已下发过,防重复
s.fmSubCtx = fmSubCtx;
m_FileXferContextToDevice[fmSubCtx] = device_id; // OfflineProc 反查用
localDir = s.localDir;
remotePath = s.remotePath;
}
@@ -7048,7 +7050,10 @@ void CMcpServer::OnFileChunkV2(uint64_t device_id, context* streamSubCtx, const
if (s.tool != "download_file") return;
if (s.done) return; // 已出错/完成,丢弃迟到包
if (s.streamSubCtx == nullptr) s.streamSubCtx = streamSubCtx;
if (s.streamSubCtx == nullptr) {
s.streamSubCtx = streamSubCtx;
m_FileXferContextToDevice[streamSubCtx] = device_id; // OfflineProc 反查用
}
if (s.transferID == 0) {
s.transferID = pkt->transferID;
s.totalFiles = pkt->totalFiles;
@@ -7179,9 +7184,37 @@ void CMcpServer::ClearFileTransfer(uint64_t device_id) {
m_FileXferSessions.erase(it);
found = true;
}
// 反查表按 context* 键,必须随会话一起擦,否则连接池复用同一指针后会被误转成断线事件。
if (s.fmSubCtx) m_FileXferContextToDevice.erase(s.fmSubCtx);
if (s.streamSubCtx) m_FileXferContextToDevice.erase(s.streamSubCtx);
}
if (!found) return;
// 锁外关闭两条子链接(CancelIO 触发客户端断开,停掉仍在飞的流式数据)。
if (s.fmSubCtx) s.fmSubCtx->CancelIO();
if (s.streamSubCtx) s.streamSubCtx->CancelIO();
}
void CMcpServer::OnFileTransferContextClosed(context* subCtx) {
std::lock_guard<std::mutex> lk(m_FileXferMutex);
auto mapIt = m_FileXferContextToDevice.find(subCtx);
if (mapIt == m_FileXferContextToDevice.end()) return; // 不是文件传输子链接
uint64_t device_id = mapIt->second;
m_FileXferContextToDevice.erase(mapIt);
auto it = m_FileXferSessions.find(device_id);
if (it == m_FileXferSessions.end()) return;
FileTransferSession& s = it->second;
// 无论如何先清裸指针:此后工具线程的 ClearFileTransfer 绝不能再对这条已回收的连接 CancelIO。
bool wasStream = (s.streamSubCtx == subCtx);
if (s.fmSubCtx == subCtx) s.fmSubCtx = nullptr;
if (s.streamSubCtx == subCtx) s.streamSubCtx = nullptr;
// 流式子链接承载数据,它断了传输就不可能完成 → 立即判失败并唤醒工具线程(不用等 timeout)。
// 文件管理器子链接只用来下发命令,断开不代表传输失败,清指针即可。
if (wasStream && !s.done) {
s.error = 1003; // 断线
s.done = true;
m_FileXferCv.notify_all();
}
}