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:
1 parent
80c7d1ed53
commit
3d87ec6720
4 files changed
+100
-19
No files matched your search
+21
-1
@@ -766,6 +766,17 @@ BOOL IOCPClient::ConnectServer(const char* szServerIP, unsigned short uPort)
|
||||
pthread_t id = 0;
|
||||
int ret = pthread_create(&id, nullptr, (void* (*)(void*))IOCPClient::WorkThreadProc, this);
|
||||
if (ret == 0) {
|
||||
// 必须 detach:POSIX 上 CloseHandle / SAFE_CLOSE_HANDLE 是空宏
|
||||
// (common/commands.h:77-78),析构函数里的 SAFE_CLOSE_HANDLE(m_hWorkThread)
|
||||
// 什么都不做,也没有任何地方 pthread_join 过这个线程。未 detach 的 pthread
|
||||
// 退出后不会释放线程结构体和栈,于是**每个 IOCPClient 实例永久泄漏一个线程**
|
||||
// —— 而子连接(屏幕/文件/终端/日志)每次点击都新建一个 IOCPClient。
|
||||
// 泄漏累积到 RLIMIT_NPROC 附近后 pthread_create 开始返回 EAGAIN:
|
||||
// 主连接因为已有线程还活着不受影响,但新开的子连接全部失败,
|
||||
// 表现为「重启客户端就好、点按钮毫无反应」。detach 后线程退出即回收。
|
||||
// 安全性:析构函数仍靠 m_bWorkThread 等待线程收尾;m_hWorkThread 在 POSIX 上
|
||||
// 只当「线程已存在」的标志位用,从不被当作内核句柄解引用。
|
||||
pthread_detach(id);
|
||||
m_bWorkThread = S_RUN;
|
||||
m_bIsRunning = TRUE;
|
||||
// Store pthread_t so subsequent ConnectServer calls (reconnects) find
|
||||
@@ -775,6 +786,11 @@ BOOL IOCPClient::ConnectServer(const char* szServerIP, unsigned short uPort)
|
||||
// SAFE_CLOSE_HANDLE and CloseHandle are no-ops on Android, so the value
|
||||
// is never dereferenced as a kernel handle.
|
||||
m_hWorkThread = reinterpret_cast<HANDLE>(static_cast<uintptr_t>(id));
|
||||
} else {
|
||||
// pthread_create 直接返回错误码(不经 errno):EAGAIN(35)/EPERM(1) 基本就是
|
||||
// 进程/用户的线程数到顶。这条路径原本完全静默——连接看起来「成功」了,
|
||||
// 但没有收包线程,后续 ConnAuth 必然超时,日志里只剩一句"等待响应超时"。
|
||||
Mprintf("[ERROR] pthread_create(WorkThread) failed: ret=%d\n", ret);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -853,9 +869,13 @@ DWORD WINAPI IOCPClient::WorkThreadProc(LPVOID lParam)
|
||||
}
|
||||
SAFE_CLOSE_HANDLE(This->m_hWorkThread);
|
||||
This->m_hWorkThread = NULL;
|
||||
This->m_bWorkThread = S_STOP;
|
||||
This->m_bIsRunning = FALSE;
|
||||
delete[] szBuffer;
|
||||
// 最后一步才发布 S_STOP:析构函数以「m_bWorkThread != S_RUN」判断线程已完全退出,
|
||||
// 看到它之后就会释放成员并返回,调用方随即 delete this。反过来写(先 S_STOP 再
|
||||
// m_bIsRunning)等于给析构函数一个提前放行的窗口——最后那句 m_bIsRunning 会写进
|
||||
// 已释放的内存。此后本函数不得再读写 This 的任何成员。
|
||||
This->m_bWorkThread = S_STOP;
|
||||
|
||||
return 0xDEAD;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user