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

+23
View File
@@ -47,6 +47,18 @@ inline void RunSubConnThread(const char* threadName, CreateFn createHandler, OnR
void* clientAddr = ClientObject.get();
Mprintf(">>> Enter %s [%p]\n", threadName, clientAddr);
// 传输协议必须显式喂进去,否则子连接与主连接走的不是同一条路:
// Linux/macOS 的 IOCPClient 不带 m_conn,ConnectServer 里协议来源是
// m_defaultProto(默认 -1 = 明文 TCP)。主连接在 main 里已 SetDefaultProto
// (macos/main.mm / linux/main.cpp),子连接漏掉这一步时,配成 WS/WSS 的客户端
// 会出现「主连接正常、所有子连接静默按明文直连」——经反代/HTTP 代理接入时,
// 明文 TCP 在到达主控之前就被丢掉,服务端连一条 [ConnAuth] 都看不到,
// 而客户端只会卡在 "[ConnAuth] 等待响应超时"。见 main.mm 里同一段注释。
ClientObject->SetDefaultProto((int)(unsigned char)g_SETTINGS.protoType);
Mprintf(">>> %s: proto=%d (%s:%d)\n", threadName,
(int)(unsigned char)g_SETTINGS.protoType,
g_SETTINGS.ServerIP(), g_SETTINGS.ServerPort());
// 子连接:开启 auth。Linux/macOS IOCPClient 不带 m_conn,显式传入 g_myClientID。
ClientObject->EnableSubConnAuth(true, g_myClientID);
@@ -61,8 +73,19 @@ inline void RunSubConnThread(const char* threadName, CreateFn createHandler, OnR
Sleep(1000);
// 清除回调,防止重连线程访问已销毁的 handler
ClientObject->setManagerCallBack(nullptr, nullptr, nullptr);
} else {
// createHandler 返回 nullptr = 本机侧初始化失败(如 macOS 无录屏权限)。
// 子连接已经建立、鉴权也已通过,这里不关掉的话它会一直挂着,服务端那边
// 表现为「子连接在线但永远不发首包」,而客户端日志里原本一个字都没有。
Mprintf("*** %s: handler init failed, dropping sub-connection ***\n", threadName);
}
} else if (S_CLIENT_NORMAL == g_bExit) {
// 只有 g_bExit 仍是 NORMAL 时才算真失败(TCP 连接、握手、ConnAuth 任一环节,
// ConnectServer 内部已各自打过具体原因)。这里补一条归属明确的收尾日志,
// 否则「子连接没建起来」在日志里只剩一行 Leave,无从判断卡在哪一步。
Mprintf("*** %s: ConnectServer failed ***\n", threadName);
}
// g_bExit 已置位 = 客户端正在退出,子连接没建起来是预期行为,不打错误日志。
Mprintf(">>> Leave %s [%p]\n", threadName, clientAddr);
} catch (const std::exception& e) {
Mprintf("*** %s exception: %s ***\n", threadName, e.what());