fix(client): stop the reconnect loops from spinning when the master restarts

On a master restart the client receives SERVER_EXIT and KernelManager sets
g_bExit = S_SERVER_EXIT. IsClientAppRunning() compares against S_CLIENT_NORMAL,
so it flips to false immediately and the worker starts tearing down -- but
g_bThreadExit is only cleared at the very end of StartClient. Throughout that
window TestRun() still sees IsThreadRun() == TRUE and takes its early-return
path.

The loader's reconnect loop deliberately does not wait in the S_SERVER_EXIT case
("reconnect immediately, do not wait for share instances"), and its else branch
was empty, so it re-entered TestRun() with no delay whatsoever. Measured on a
live host: 919 identical "TestRun SetServer[91.99.165.207:443]" lines inside a
single second, 960 on its second instance. The client ring buffer holds 1000
lines, so that burst erased the entire history -- which is why nothing from
before the restart could be inspected afterwards. IsStoped() cannot gate this:
it returns true unconditionally while g_bExit == S_SERVER_EXIT, conflating
"worker exited" with "master restarted".

- test.cpp: sleep 20ms in the S_SERVER_EXIT branch. The sleep runs concurrently
  with the worker's teardown rather than after it, so reconnecting is delayed by
  at most one 20ms tick.
- ClientDll.cpp TestRun(): log only when SetServer() reports the address really
  changed. It returns false for a null address, which also silences AutoRun's
  TestRun(NULL, 0) calls. Nothing is lost: the restart itself is already logged
  as "======> Server exit" in KernelManager.
- ClientDll.cpp AutoRun(): its do/while had no sleep either. Gate it on "still
  in S_SERVER_EXIT and a worker is still running" so the normal path stays
  zero-delay.

Together these turn a burst that wiped the log buffer into silence, and cap the
loop at 50 iterations per second instead of roughly 900. A worker that hangs
during teardown still burns a core; curing that needs the thread-alive state
exposed separately from IsStoped(), which is an interface change and is left out.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-10-30 23:39:05 +01:00
1 parent cbb84809d8
commit 36ae4b3057
2 files changed
+17 -2

No files matched your search

+10 -2
View File
@@ -373,6 +373,11 @@ DWORD WINAPI AutoRun(LPVOID param)
{
do {
TestRun(NULL, 0);
// 旧工作线程还没退完时 TestRun 会立刻返回(此时连日志都不打,见其内部的
// SetServer 返回值判断),不睡就会满速空转、白占一个核且毫无提示。
// 只在真会空转的条件下睡,正常重启路径零延迟。
if (S_SERVER_EXIT == g_MyApp.g_bExit && g_MyApp.IsThreadRun())
Sleep(20);
} while (S_SERVER_EXIT == g_MyApp.g_bExit);
if (g_MyApp.g_Connection->ClientType() == CLIENT_TYPE_SHELLCODE) {
@@ -415,8 +420,11 @@ extern "C" __declspec(dllexport) void TestRun(char* szServerIP,int uPort)
ClientApp& app(g_MyApp);
CONNECT_ADDRESS& settings(*(app.g_Connection));
if (app.IsThreadRun()) {
settings.SetServer(szServerIP, uPort);
Mprintf("TestRun SetServer[%s:%d]\n", szServerIP ? szServerIP : "", uPort);
// 只在地址真的变了才记日志。主控端重启后,加载器和 AutoRun 会反复调用本函数
// 直到旧工作线程退完,每次都打一行会把客户端 1000 行环形日志冲光。
// SetServer 返回 false 表示地址没变,同时也涵盖 TestRun(NULL, 0)(空地址直接返回)。
if (settings.SetServer(szServerIP, uPort))
Mprintf("TestRun SetServer[%s:%d]\n", szServerIP ? szServerIP : "", uPort);
return;
}
app.SetThreadRun(TRUE);
+7
View File
@@ -501,6 +501,13 @@ BOOL Run(const char* argv1, int argv2, const std::string &runCmd)
// 卸载 DLL 时残留分享实例线程。主控端重启(S_SERVER_EXIT)则立即重连,不等仍存活的分享实例。
while (bStop && !bStop() && 0 == status)
Sleep(20);
} else {
// 主控端重启:不等分享实例,但上一个工作线程未必已经退完,TestRun 这时
// 会立刻返回。不睡就成了每秒上千次的空转——实测 1 秒刷了近千行,把客户端
// 1000 行环形日志冲光,其它日志全看不到。
// 注意不能用 bStop() 挡:IsStoped() 在 S_SERVER_EXIT 下恒返回 true,挡不住。
// 20ms 与线程收尾是并行的,重连延迟净增不超过 20ms。
Sleep(20);
}
} while (bExit && !bExit() && 0 == status);