Fix(mcp): harden terminal teardown and fill annotation gaps
OnTerminalClosed now erases the shell route and nulls the session pointer before OfflineProc returns that context to the pool, and WaitTerminalReady re-looks-up the session on every wake, failing with cleanup when it is gone, closed, or has no sub-connection. Before, a waiter could CancelIO a recycled context, or a late cleanup could unroute a connection that reused it. terminal_close only set closed and expected a waiter to clean up, so after a caller timeout the remote shell kept running and the slot stayed busy for an hour. A waiters counter now separates the cases: with a waiter the close hands off, without one it erases the session and cancels IO at once. WaitTermCommand's closed branch keeps a truncated tail pollable instead of dropping it, cancelling IO immediately, and the drained poll cleans up. The write side had the same hazard. exec_command, terminal_exec and http_probe wrote the command line after the prompt wait without rechecking the sub-connection; IsTermSubCtxLive now skips the write when the shell has died, and WaitTerminalDone reports closed on every failure path so callers never CancelIO a pointer that may be back in the pool. list_hosts, read_file and http_probe were missing from kToolHints and got destructive defaults; the first two are now read-only/idempotent, http_probe stays writable because its method can mutate the probed service. Verified on the isolated Debug instance: the truncated tail drains without gaps (12514 lines), a waiterless close frees the slot in 0.1s, and the terminal, exec_command, http_probe, 8a/8b/8c and verify_tools checks pass. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
5841f27df9
commit
425d737ca0
3 files changed
+111
-22
No files matched your search
@@ -227,6 +227,11 @@ public:
|
||||
// 终端子连接是否已被 MCP 接管(MessageHandle 顶部据此把 shell 输出路由到 OnTerminalData)。
|
||||
bool IsTerminalContext(context* subCtx);
|
||||
|
||||
// 工具线程:写命令前确认 subCtx 仍是该会话在用的子连接。等待提示符期间 shell 可能已退出,
|
||||
// 子连接被 OfflineProc 归还连接池后可能已被新连接复用,此时再写就是往别人的连接里灌命令。
|
||||
// sessionId 非空时还要求会话是持久的且 token 一致。
|
||||
bool IsTermSubCtxLive(uint64_t device_id, const std::string& sessionId, context* subCtx);
|
||||
|
||||
// 追加 shell 输出并做哨兵检测(命中置 done 并唤醒等待者)。
|
||||
void OnTerminalData(context* subCtx, const BYTE* data, ULONG len);
|
||||
|
||||
@@ -241,6 +246,7 @@ public:
|
||||
|
||||
// 工具线程:等待输出收集完成(哨兵命中 / 进程退出 / 超时)。
|
||||
// 成功 true:out=截断到哨兵前的原始字节、exitCode=0/1(进程退出无哨兵时 -1)、closed=是否进程退出。
|
||||
// 失败时也尽量照实回填 closed:为 true 表示子连接已断开或去向未知,调用方不得再对它 CancelIO。
|
||||
bool WaitTerminalDone(uint64_t device_id, std::vector<BYTE>& out, int& exitCode, bool& closed, int timeoutMs);
|
||||
|
||||
// 工具线程:写命令前等 shell 启动完成(收到第一屏输出,通常是提示符)。
|
||||
@@ -284,7 +290,8 @@ public:
|
||||
|
||||
// 等待本条命令结束或超时,并交付 [delivered, ...) 的增量输出。
|
||||
// pollMode=true 表示本次调用没有携带新命令(只轮询),此时会话空闲 = 调用方用错,返回 7。
|
||||
// 返回:0=命令结束(done=true;closed=true 表示 shell 退出,会话已清理)
|
||||
// 返回:0=命令结束(done=true;closed=true 表示 shell 退出;本次被 maxOutputBytes 截断时
|
||||
// 会话保留到轮询取完尾巴再清理,否则当场清理)
|
||||
// 1=等待超时,命令仍在运行(done=false;会话保持 busy,可再次轮询)
|
||||
// 2=请求被取消,会话已销毁(与旧语义一致:取消方不会再轮询)
|
||||
// 3=会话不存在/未就绪/等待期间被销毁(细节看 DescribeTermDestroyed)
|
||||
@@ -854,6 +861,8 @@ private:
|
||||
size_t delivered = 0; // 已交付给调用方的 data 前缀长度(超时/轮询增量回传)
|
||||
size_t droppedBytes = 0; // 因 data 硬上限被丢弃的最旧字节数(如实上报)
|
||||
std::chrono::steady_clock::time_point busySince{}; // busy 起点(长时间在飞的强制回收兜底)
|
||||
int waiters = 0; // 阻塞在 WaitTermCommand 里的线程数;terminal_close 据此
|
||||
// 判断 closed 之后是否还有人负责收尾(没有就自己清)
|
||||
};
|
||||
std::mutex m_TermMutex;
|
||||
std::condition_variable m_TermCv;
|
||||
|
||||
Reference in new issue
Block a user