Fix(client): detach the receive thread instead of leaking one per connection

CloseHandle and SAFE_CLOSE_HANDLE are empty off Windows, so the POSIX
receive thread is never released: nothing detaches it and nothing joins
it. Every IOCPClient keeps one thread for good, and each screen, file,
shell or log sub-connection constructs a fresh IOCPClient. Once the leak
reaches RLIMIT_NPROC, pthread_create returns EAGAIN, std::thread throws,
and OnServerReceiving's catch(...) swallows it: every command that spawns
a worker goes quiet while COMMAND_SYSTEM keeps working. Observed on a Mac
after six weeks: 2048 threads against a 1333 limit.

Detach the thread right after creation so it is reclaimed when it
returns; the destructor still waits on m_bWorkThread, so nothing else
changes. Also log the pthread_create failure that used to be silent, and
publish S_STOP as the very last write in WorkThreadProc: the destructor
reads it as "thread is gone" and frees this, so the m_bIsRunning that
used to follow it could land in freed memory.

Two smaller fixes on the same sub-connection path:

- common/sub_conn_thread.h never called SetDefaultProto, so sub-connects
  fell back to plaintext TCP whatever g_SETTINGS.protoType said: behind a
  reverse proxy the packet dies before the master (no [ConnAuth] on the
  server, only "waiting for ack timed out" on the client). Android did.
- macos/main.mm and linux/main.cpp log the spawn before starting the
  worker and catch std::system_error, reporting e.code().value() rather
  than errno, which libc++/libstdc++ never set here. The macOS skeleton
  also reports ConnectServer failures and a null createHandler.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-14 11:43:04 +01:00
1 parent 80c7d1ed53
commit 3d87ec6720
4 files changed
+100 -19

No files matched your search

+26 -10
View File
@@ -9,6 +9,7 @@
#include <string.h>
#include <termios.h>
#include <thread>
#include <system_error> // std::system_error:SpawnSubConnWorker 捕获线程创建失败
#include <fcntl.h>
#include <atomic>
#include <mutex>
@@ -647,6 +648,26 @@ static void ActivityReportThread()
g_activityReportsInFlight.fetch_sub(1, std::memory_order_relaxed);
}
// 子连接 worker 的唯一启动点(与 macos/main.mm 的 SpawnSubConnWorker 同语义)。
// 线程创建失败(RLIMIT_NPROC 到顶时 pthread_create 返回 EAGAIN/EPERM,std::thread
// 构造抛 std::system_error)必须在这里接住:否则异常穿过 DataProcess 落到
// OnServerReceiving 的 catch(...),日志里只剩一行笼统的
// "[ERROR] OnServerReceiving catch an error",同一批 recv 里的其它完整包也会被丢弃。
// 表现是「点了终端/桌面/文件毫无反应,连收到命令的日志都没有」。
static void SpawnSubConnWorker(const char* name, void* (*entry)(void*))
{
Mprintf("** Received '%s' command, spawning worker ***\n", name);
try {
std::thread(entry, nullptr).detach();
} catch (const std::system_error& e) {
// pthread_create 的返回值被 libc++/libstdc++ 直接包进 error_code,errno 不可信
Mprintf("*** spawn '%s' worker FAILED: %s (code=%d) ***\n",
name, e.what(), e.code().value());
} catch (const std::exception& e) {
Mprintf("*** spawn '%s' worker FAILED: %s ***\n", name, e.what());
}
}
int DataProcess(void* user, PBYTE szBuffer, ULONG ulLength)
{
if (szBuffer == nullptr || ulLength == 0)
@@ -662,22 +683,17 @@ int DataProcess(void* user, PBYTE szBuffer, ULONG ulLength)
Mprintf("*** [%p] Received Bye-Bye command ***\n", user);
g_bExit = S_CLIENT_EXIT;
} else if (szBuffer[0] == COMMAND_SHELL) {
std::thread(ShellworkingThread, nullptr).detach();
Mprintf("** [%p] Received 'SHELL' command ***\n", user);
SpawnSubConnWorker("SHELL", ShellworkingThread);
} else if (szBuffer[0] == COMMAND_SCREEN_SPY) {
std::thread(ScreenworkingThread, nullptr).detach();
Mprintf("** [%p] Received 'SCREEN_SPY' command ***\n", user);
SpawnSubConnWorker("SCREEN_SPY", ScreenworkingThread);
} else if (szBuffer[0] == COMMAND_SYSTEM) {
std::thread(SystemManagerThread, nullptr).detach();
Mprintf("** [%p] Received 'SYSTEM' command ***\n", user);
SpawnSubConnWorker("SYSTEM", SystemManagerThread);
} else if (szBuffer[0] == COMMAND_LIST_DRIVE) {
std::thread(FileManagerThread, nullptr).detach();
Mprintf("** [%p] Received 'LIST_DRIVE' command ***\n", user);
SpawnSubConnWorker("LIST_DRIVE", FileManagerThread);
} else if (szBuffer[0] == COMMAND_QUERY_LOG) {
// 与 Windows 端一致:回日志必须开子连接。服务端取走首包后 CancelIO 关的是
// 子连接;若回在主连接上,被关掉的就是主连接本身。
std::thread(ClientLogThread, nullptr).detach();
Mprintf("** [%p] Received 'QUERY_LOG' command ***\n", user);
SpawnSubConnWorker("QUERY_LOG", ClientLogThread);
} else if (szBuffer[0] == COMMAND_QUERY_ACTIVITY) {
// 历史活动:主连接一次性快照(无子连接),有桌面走窗口采样,无桌面走登录+命令记录。
// 已经有报表在飞就丢掉这一次(服务端每主机单飞行,连点也只是重复请求)