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

+1
View File
@@ -400,3 +400,4 @@ int n = RecvFileChunkV2((char*)szBuffer, ulLength, m_conn, nullptr, m_hash, m_hm
| # | 问题 | 修复 |
|---|---|---|
| R1 | **纯目录树下载必超时**:完成判定 `filesDone >= totalFiles - directoryCount` 只在 `OnFileCompleteV2` 里跑,而客户端对目录项不发 `COMMAND_FILE_COMPLETE_V2`。空目录/只有子目录的树期望文件数为 0,永远等不到 COMPLETE → 只能干等 `timeout_ms`(默认 600s)后报 `1002` | `OnFileChunkV2` 的目录分支里,`directoryCount >= totalFiles`(即全部条目都是目录)时直接置 `done` + notify。实测空目录 0.2s 返回、3 文件目录树回归正常 |
| R2 | **断线无清理 → 悬空子连接指针**:`FileTransferSession.fmSubCtx / streamSubCtx` 是裸 `context*`,`OfflineProc` 只清了终端/远程控制/聊天,没有文件传输的钩子(§6.5 声称的「断线擦会话」实现缺失)。客户端传输中掉线后 connection 被连接池回收,工具线程超时路径仍对其 `CancelIO()` → 悬空指针/误取消他人连接 | 新增 `m_FileXferContextToDevice` 反查表(受 `m_FileXferMutex` 保护,`ClearFileTransfer` 同步擦除),`OfflineProc` 增 `OnFileTransferContextClosed`:清指针并移除反查项;**流式子连接**断开即判失败(`error=1003`,立即唤醒工具线程),文件管理器子连接断开只清指针(数据走流式子连接,不判失败)。实测传输中杀掉客户端:1s 返回 `Connection lost`(原来要等满 `timeout_ms`),服务端存活;重启客户端后连续两次下载、一次上传均正常 |
+6
View File
@@ -5546,6 +5546,12 @@ BOOL CALLBACK CMy2015RemoteDlg::OfflineProc(CONTEXT_OBJECT* ContextObject)
McpServer().OnManagedContextClosed(ContextObject);
}
// MCP P6 文件传输的文件管理器/流式子连接断开:清反查表并置空裸指针,避免工具线程
// 超时路径对连接池回收后的 context 误 CancelIO;流式子链接断开同时判传输失败。
if (McpServer().IsRunning()) {
McpServer().OnFileTransferContextClosed(ContextObject);
}
// MCP P9 远程交谈:子连接(或主连接)断开时清路由/标记会话 closed 并唤醒 chat_wait。
// 同样必须在 RemoveFromHostList 之前,避免连接池复用同一 context 指针后误路由。
if (McpServer().IsRunning()) {
+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();
}
}
+8 -1
View File
@@ -278,6 +278,11 @@ public:
// 收尾:擦会话 + 锁外 CancelIO 文件管理器/流式两条子链接(幂等)。
void ClearFileTransfer(uint64_t device_id);
// 文件传输子链接断开(OfflineProc 调用):清反查表 + 置空裸指针,避免工具线程超时路径
// 对已回收(连接池复用)的 context 误 CancelIO。流式子链接断开 = 传输失败(1003);
// 文件管理器子链接断开只是少了收尾对象,传输数据走流式子链接,不判失败。
void OnFileTransferContextClosed(context* subCtx);
// ===== P7:设备管理控制(kill_process / operate_window / control_service / power_action)=====
// 写工具复用 m_Pending 单飞行;followUp/followUpSent/subCtx 标记接管进度。
// 「就绪 token」与「确认 token」是同一 token(客户端子连接构造即回一包,写后再回一包),
@@ -572,7 +577,7 @@ private:
uint32_t directoryCount = 0; // 目录项计数(目录不发 COMPLETE 包)
uint32_t filesDone = 0; // 已完成的文件数(含跳过的)
bool done = false; // 全部完成或出错
int error = 0; // 0=ok;8=哈希不匹配;1001=路径逃逸;1002=超时
int error = 0; // 0=ok;8=哈希不匹配;1001=路径逃逸;1002=超时;1003=流式子连接断线
std::map<uint32_t, FileTransferEntry> fileEntries; // fileIndex → {path,size,sha256}
std::set<uint32_t> skipIndexes; // overwrite=false 跳过的 fileIndex
std::vector<FileTransferEntry> skipped; // 跳过的文件(供输出)
@@ -581,6 +586,8 @@ private:
std::mutex m_FileXferMutex;
std::condition_variable m_FileXferCv;
std::map<uint64_t, FileTransferSession> m_FileXferSessions; // device_id → 会话
// 子连接反查(受 m_FileXferMutex 保护):OfflineProc 拿不到 device_id,只能按 context* 找会话。
std::map<context*, uint64_t> m_FileXferContextToDevice;
// ===== P9:远程交谈(受 m_ChatMutex 保护;单设备单会话)=====
// 用 shared_ptr 持有会话:chat_wait / chat_send 在工作线程里复制一份后,即使 map 条目