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
94 lines
5.1 KiB
C++
94 lines
5.1 KiB
C++
// sub_conn_thread.h
|
||
// Linux/macOS 客户端子连接 worker 线程的统一骨架。
|
||
//
|
||
// 各 worker 线程(Shell / ScreenSpy / FileManager / SystemManager 等)共有的步骤:
|
||
// 1. new IOCPClient(g_bExit, exit_while_disconnect=true)
|
||
// 2. Enter log
|
||
// 3. EnableSubConnAuth(true, g_myClientID)(子连接强制 ConnAuth)
|
||
// 4. ConnectServer(内部会执行 PerformConnAuth;失败返 false)
|
||
// 5. 创建 platform handler
|
||
// 6. setManagerCallBack 装回调
|
||
// 7. 调 onReady(发首包:TOKEN_TERMINAL_START / SendBitmapInfo() 等)
|
||
// 8. while (running && connected && !g_bExit) Sleep
|
||
// 9. 清回调防止 dangling
|
||
// 10. Leave log
|
||
// 11. catch exceptions
|
||
//
|
||
// 平台差异(通过 lambda 注入):
|
||
// - HandlerT:PTYHandler / ScreenHandler / SystemManager / FileManager
|
||
// - createHandler 可返回 nullptr 表示初始化失败(如 macOS ScreenHandler 无录屏权限)
|
||
// - onReady 完成首包发送或额外 setup
|
||
//
|
||
// 用法见 linux/main.cpp / macos/main.mm 的 *workingThread 调用点。
|
||
#pragma once
|
||
|
||
#include <memory>
|
||
#include <stdexcept>
|
||
|
||
#include "client/IOCPClient.h"
|
||
#include "common/commands.h"
|
||
#include "common/logger.h"
|
||
|
||
extern State g_bExit;
|
||
extern uint64_t g_myClientID;
|
||
extern CONNECT_ADDRESS g_SETTINGS;
|
||
|
||
// 子连接 worker 线程通用骨架。
|
||
//
|
||
// CreateFn 签名: std::unique_ptr<HandlerT>(IOCPClient*)
|
||
// 返回 nullptr 表示初始化失败(如权限拒绝),线程会跳过 callback 安装直接 leave。
|
||
// OnReadyFn 签名: void(IOCPClient*, HandlerT*)
|
||
// handler 装上 callback 后立即调用,可在此发送首包或做额外 setup。
|
||
template <class HandlerT, class CreateFn, class OnReadyFn>
|
||
inline void RunSubConnThread(const char* threadName, CreateFn createHandler, OnReadyFn onReady)
|
||
{
|
||
try {
|
||
std::unique_ptr<IOCPClient> ClientObject(new IOCPClient(g_bExit, true));
|
||
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);
|
||
|
||
if (!g_bExit && ClientObject->ConnectServer(g_SETTINGS.ServerIP(), g_SETTINGS.ServerPort())) {
|
||
std::unique_ptr<HandlerT> handler = createHandler(ClientObject.get());
|
||
if (handler) {
|
||
ClientObject->setManagerCallBack(handler.get(),
|
||
IOCPManager::DataProcess,
|
||
IOCPManager::ReconnectProcess);
|
||
onReady(ClientObject.get(), handler.get());
|
||
while (ClientObject->IsRunning() && ClientObject->IsConnected() && S_CLIENT_NORMAL == g_bExit)
|
||
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());
|
||
}
|
||
}
|