| @@ -59,6 +59,73 @@ STATIC int32_t ServerWaitStop(ServerHandle handle) | |||
| 59 | 59 | ||
| 60 | STATIC void ServerDestroyCommHandle(const CommHandle* handle) { AdxDestroyCommHandle((AdxCommHandle)handle); } | 60 | STATIC void ServerDestroyCommHandle(const CommHandle* handle) { AdxDestroyCommHandle((AdxCommHandle)handle); } |
| 61 | 61 | ||
| 62 | +STATIC ComponentType ServerGetComponentType(AdxCommConHandle handle) { return handle->comp; } | ||
| 63 | + | ||
| 64 | +// caller must hold handle->stateLock; monitorRunFlag is read unguarded on purpose: | ||
| 65 | +// it is a fast-fail hint only, the monitor loop reads it the same way | ||
| 66 | +STATIC bool ServerCommNodeAcquirableLocked(ServerHandle handle, const ServerCommNode* node, bool checkRunFlag) | ||
| 67 | +{ | ||
| 68 | + if ((node == NULL) || node->dying) { | ||
| 69 | + return false; | ||
| 70 | + } | ||
| 71 | + return !checkRunFlag || handle->monitorRunFlag; | ||
| 72 | +} | ||
| 73 | + | ||
| 74 | +/* | ||
| 75 | + * Take a reference on the manager's current session node under stateLock. | ||
| 76 | + * The returned node (and its comm handle) is guaranteed to stay alive until the matching | ||
| 77 | + * ServerReleaseCommNode, even if the session monitor detaches it concurrently. When | ||
| 78 | + * checkRunFlag is false the monitorRunFlag state is ignored: only the owner (session | ||
| 79 | + * monitor thread) acquires this way. Only stateLock is taken: this path must never wait | ||
| 80 | + * for lock, whose holder may be joining the session monitor thread. | ||
| 81 | + * References are counted per generation (per node): a concurrent new session publishes its | ||
| 82 | + * own node, in-flight references of different sessions never mix into one counter. | ||
| 83 | + */ | ||
| 84 | +STATIC ServerCommNode* ServerAcquireCommNode(ServerHandle handle, bool checkRunFlag) | ||
| 85 | +{ | ||
| 86 | + LOCK_WARN_LOG(&handle->stateLock); | ||
| 87 | + ServerCommNode* node = handle->current; | ||
| 88 | + if (!ServerCommNodeAcquirableLocked(handle, node, checkRunFlag)) { | ||
| 89 | + node = NULL; | ||
| 90 | + } else { | ||
| 91 | + node->refs++; | ||
| 92 | + } | ||
| 93 | + UNLOCK_WARN_LOG(&handle->stateLock); | ||
| 94 | + return node; | ||
| 95 | +} | ||
| 96 | + | ||
| 97 | +/* | ||
| 98 | + * Drop a reference taken by ServerAcquireCommNode. Nodes are referenced by pointer, each | ||
| 99 | + * generation is destroyed by its own last releaser: references held on other sessions are | ||
| 100 | + * counted on their own nodes and can neither delay nor trigger this node's destroy. | ||
| 101 | + * Destroy happens outside of stateLock. | ||
| 102 | + */ | ||
| 103 | +STATIC void ServerReleaseCommNode(ServerHandle handle, ServerCommNode* node) | ||
| 104 | +{ | ||
| 105 | + if (node == NULL) { | ||
| 106 | + return; | ||
| 107 | + } | ||
| 108 | + LOCK_WARN_LOG(&handle->stateLock); | ||
| 109 | + node->refs--; | ||
| 110 | + ServerCommNode* dying = NULL; | ||
| 111 | + if (node->dying && (node->refs == 0)) { | ||
| 112 | + ServerCommNode** slot = &handle->commList; | ||
| 113 | + while (*slot != NULL) { | ||
| 114 | + if (*slot == node) { | ||
| 115 | + dying = *slot; | ||
| 116 | + *slot = dying->next; | ||
| 117 | + break; | ||
| 118 | + } | ||
| 119 | + slot = &(*slot)->next; | ||
| 120 | + } | ||
| 121 | + } | ||
| 122 | + UNLOCK_WARN_LOG(&handle->stateLock); | ||
| 123 | + if (dying != NULL) { | ||
| 124 | + ServerDestroyCommHandle(dying->comm); | ||
| 125 | + XFREE(dying); | ||
| 126 | + } | ||
| 127 | +} | ||
| 128 | + | ||
| 62 | STATIC int32_t SessionMonitorIsHandleValid(const CommHandle* handle) | 129 | STATIC int32_t SessionMonitorIsHandleValid(const CommHandle* handle) |
| 63 | { | 130 | { |
| 64 | if (handle == NULL) { | 131 | if (handle == NULL) { |
| @@ -74,36 +141,86 @@ STATIC int32_t SessionMonitorIsHandleValid(const CommHandle* handle) | |||
| 74 | return LOG_SUCCESS; | 141 | return LOG_SUCCESS; |
| 75 | } | 142 | } |
| 76 | 143 | ||
| 144 | +// thread startup argument: binds the monitor to the exact generation node published by | ||
| 145 | +// its own ServerProcess, so the thread can never pick up a newer session from mgr->current | ||
| 146 | +// within the scheduling window between thread creation and its first instruction | ||
| 147 | +typedef struct { | ||
| 148 | + ServerHandle mgr; | ||
| 149 | + ServerCommNode* node; | ||
| 150 | +} SessionMonitorArg; | ||
| 151 | + | ||
| 77 | STATIC void* SessionMonitorProcess(void* arg) | 152 | STATIC void* SessionMonitorProcess(void* arg) |
| 78 | { | 153 | { |
| 79 | - ServerHandle mgr = (ServerHandle)arg; | 154 | + SessionMonitorArg* ctx = (SessionMonitorArg*)arg; |
| 155 | + ServerHandle mgr = ctx->mgr; | ||
| 156 | + ServerCommNode* mine = ctx->node; | ||
| 157 | + XFREE(ctx); | ||
| 158 | + // hold a lifetime reference on our own session node, so the comm handle stays valid | ||
| 159 | + // during the whole thread; the node pointer belongs to this generation alone, no | ||
| 160 | + // reference of any other session is ever counted on it | ||
| 161 | + LOCK_WARN_LOG(&mgr->stateLock); | ||
| 162 | + if (mine->dying) { | ||
| 163 | + // unreachable with the current call sequence (only this thread or the rolled-back | ||
| 164 | + // create path mark its own node dying), kept as a defensive fast bail-out | ||
| 165 | + mine = NULL; | ||
| 166 | + } else { | ||
| 167 | + mine->refs++; | ||
| 168 | + } | ||
| 169 | + UNLOCK_WARN_LOG(&mgr->stateLock); | ||
| 170 | + if (mine == NULL) { | ||
| 171 | + SELF_LOG_ERROR("session monitor: acquire comm node failed"); | ||
| 172 | + // roll back the link slot reserved by ServerProcess for this monitor: nobody else | ||
| 173 | + // decrements it on this path, a leaked slot would reject every later connection | ||
| 174 | + LOCK_WARN_LOG(&mgr->stateLock); | ||
| 175 | + if (mgr->linkedNum > 0) { | ||
| 176 | + mgr->linkedNum--; | ||
| 177 | + } | ||
| 178 | + UNLOCK_WARN_LOG(&mgr->stateLock); | ||
| 179 | + return NULL; | ||
| 180 | + } | ||
| 181 | + AdxCommConHandle comm = mine->comm; | ||
| 80 | char threadName[SESSION_THREAD_NAME_LEN] = {0}; | 182 | char threadName[SESSION_THREAD_NAME_LEN] = {0}; |
| 81 | - int32_t ret = sprintf_s(threadName, SESSION_THREAD_NAME_LEN, "SessionMonitor%d", (int32_t)mgr->handle->comp); | 183 | + int32_t ret = sprintf_s(threadName, SESSION_THREAD_NAME_LEN, "SessionMonitor%d", (int32_t)comm->comp); |
| 82 | NO_ACT_WARN_LOG(ret == -1, "thread name sprintf_s failed"); | 184 | NO_ACT_WARN_LOG(ret == -1, "thread name sprintf_s failed"); |
| 83 | NO_ACT_WARN_LOG(ToolSetThreadName(threadName) != SYS_OK, "can not set thread name(%s).", threadName); | 185 | NO_ACT_WARN_LOG(ToolSetThreadName(threadName) != SYS_OK, "can not set thread name(%s).", threadName); |
| 84 | 186 | ||
| 85 | while (mgr->monitorRunFlag) { | 187 | while (mgr->monitorRunFlag) { |
| 86 | - if (SessionMonitorIsHandleValid(mgr->handle) != LOG_SUCCESS) { | 188 | + if (SessionMonitorIsHandleValid(comm) != LOG_SUCCESS) { |
| 87 | SELF_LOG_ERROR("session monitor: handle is invalid"); | 189 | SELF_LOG_ERROR("session monitor: handle is invalid"); |
| 88 | mgr->monitorRunFlag = false; | 190 | mgr->monitorRunFlag = false; |
| 89 | break; | 191 | break; |
| 90 | } | 192 | } |
| 91 | (void)ToolSleep(SESSION_MONITOR_SLEEP_TIME); // 100ms | 193 | (void)ToolSleep(SESSION_MONITOR_SLEEP_TIME); // 100ms |
| 92 | } | 194 | } |
| 195 | + | ||
| 196 | + // detach the session: new sync/send/recv requests fail fast from now on instead of | ||
| 197 | + // racing with the destroy (use-after-free). The whole tail takes stateLock ONLY and | ||
| 198 | + // never waits for lock: a new connection may be joining this very thread under lock | ||
| 199 | + // (SessionMonitorCreate), sharing that lock here would deadlock the session turnover. | ||
| 200 | + // After dying is set, the handle is destroyed as soon as the last reference drops, | ||
| 201 | + // refs already counts only this generation's holders (monitor + in-flight requests). | ||
| 202 | + LOCK_WARN_LOG(&mgr->stateLock); | ||
| 203 | + if (mgr->current == mine) { | ||
| 204 | + mgr->current = NULL; | ||
| 205 | + } | ||
🔵 Low Priority 变更行(server_mgr.c:160-168)把「置空 handle、 与未改动代码的不一致: 失败模式:一旦出现 monitor 退出时 ![]() ![]() Incorrect? | |||
| 206 | + if (mgr->linkedNum > 0) { | ||
| 207 | + // roll back the slot reserved by our own ServerProcess even when a newer session | ||
| 208 | + // already replaced the handle: each generation owns exactly one slot | ||
| 209 | + mgr->linkedNum--; | ||
| 210 | + } | ||
| 211 | + mine->dying = true; | ||
| 212 | + UNLOCK_WARN_LOG(&mgr->stateLock); | ||
| 213 | + | ||
| 93 | mgr->stop(); | 214 | mgr->stop(); |
| 94 | - LOCK_WARN_LOG(&mgr->lock); | ||
| 95 | - ServerDestroyCommHandle(mgr->handle); | ||
| 96 | - mgr->handle = NULL; | ||
| 97 | - mgr->linkedNum--; | ||
| 98 | - UNLOCK_WARN_LOG(&mgr->lock); | ||
| 99 | ret = ServerWaitStop(mgr); | 215 | ret = ServerWaitStop(mgr); |
| 100 | NO_ACT_WARN_LOG(ret != LOG_SUCCESS, "start process exit exception"); | 216 | NO_ACT_WARN_LOG(ret != LOG_SUCCESS, "start process exit exception"); |
| 217 | + ServerReleaseCommNode(mgr, mine); // drops our lifetime reference, destroys when last | ||
| 101 | 218 | ||
| 102 | SELF_LOG_INFO("session monitor exit, flag: %d", (int32_t)(mgr->monitorRunFlag)); | 219 | SELF_LOG_INFO("session monitor exit, flag: %d", (int32_t)(mgr->monitorRunFlag)); |
| 103 | return NULL; | 220 | return NULL; |
| 104 | } | 221 | } |
| 105 | 222 | ||
| 106 | -static int32_t SessionMonitorCreate(ServerHandle handle) | 223 | +static int32_t SessionMonitorCreate(ServerHandle handle, ServerCommNode* node) |
| 107 | { | 224 | { |
| 108 | int32_t ret = LOG_SUCCESS; | 225 | int32_t ret = LOG_SUCCESS; |
| 109 | // join last thread if needed | 226 | // join last thread if needed |
| @@ -114,15 +231,27 @@ static int32_t SessionMonitorCreate(ServerHandle handle) | |||
| 114 | handle->monitorTid = 0; | 231 | handle->monitorTid = 0; |
| 115 | // start process | 232 | // start process |
| 116 | handle->monitorRunFlag = true; | 233 | handle->monitorRunFlag = true; |
| 234 | + // the argument binds the new monitor to this exact generation node: taking it from | ||
| 235 | + // mgr->current instead would race with a concurrent publish inside the scheduling | ||
| 236 | + // window and could make this monitor operate on (and detach) the wrong session | ||
| 237 | + SessionMonitorArg* arg = (SessionMonitorArg*)LogMalloc(sizeof(SessionMonitorArg)); | ||
| 238 | + if (arg == NULL) { | ||
| 239 | + handle->monitorRunFlag = false; | ||
| 240 | + SELF_LOG_ERROR("alloc monitor arg failed"); | ||
| 241 | + return LOG_FAILURE; | ||
| 242 | + } | ||
| 243 | + arg->mgr = handle; | ||
| 244 | + arg->node = node; | ||
| 117 | ToolThread tid = 0; | 245 | ToolThread tid = 0; |
| 118 | ToolUserBlock funcBlock; | 246 | ToolUserBlock funcBlock; |
| 119 | funcBlock.procFunc = SessionMonitorProcess; | 247 | funcBlock.procFunc = SessionMonitorProcess; |
| 120 | - funcBlock.pulArg = (void*)(handle); | 248 | + funcBlock.pulArg = (void*)(arg); |
| 121 | ToolThreadAttr threadAttr = SESSION_MONITOR_THREAD_ATTR; | 249 | ToolThreadAttr threadAttr = SESSION_MONITOR_THREAD_ATTR; |
| 122 | ret = ToolCreateTaskWithThreadAttr(&tid, &funcBlock, &threadAttr); | 250 | ret = ToolCreateTaskWithThreadAttr(&tid, &funcBlock, &threadAttr); |
| 123 | if (ret != LOG_SUCCESS) { | 251 | if (ret != LOG_SUCCESS) { |
| 124 | handle->monitorRunFlag = false; | 252 | handle->monitorRunFlag = false; |
| 125 | SELF_LOG_ERROR("create thread failed, result=%d.", ret); | 253 | SELF_LOG_ERROR("create thread failed, result=%d.", ret); |
| 254 | + XFREE(arg); | ||
| 126 | return LOG_FAILURE; | 255 | return LOG_FAILURE; |
| 127 | } | 256 | } |
| 128 | // record tid | 257 | // record tid |
| @@ -140,13 +269,19 @@ static int32_t SessionMonitorRelease(ServerHandle handle) | |||
| 140 | 269 | ||
| 141 | static void ServerMgrNodeReset(ComponentType type) | 270 | static void ServerMgrNodeReset(ComponentType type) |
| 142 | { | 271 | { |
| 272 | + // commList is deliberately preserved across the reset: ServerRelease callers may drain | ||
| 273 | + // in-flight requests only AFTER the release (lib_load upgrade path), so a pending dying | ||
| 274 | + // node must survive and let its own last releaser destroy the handle. Clearing the list | ||
| 275 | + // here would leak the handle and drop the pending references. | ||
| 276 | + LOCK_WARN_LOG(&g_serverMgr[type].stateLock); | ||
| 277 | + g_serverMgr[type].current = NULL; | ||
| 278 | + g_serverMgr[type].linkedNum = 0; | ||
| 279 | + UNLOCK_WARN_LOG(&g_serverMgr[type].stateLock); | ||
| 143 | g_serverMgr[type].init = false; | 280 | g_serverMgr[type].init = false; |
| 144 | g_serverMgr[type].monitorRunFlag = false; | 281 | g_serverMgr[type].monitorRunFlag = false; |
| 145 | g_serverMgr[type].processFlag = false; | 282 | g_serverMgr[type].processFlag = false; |
| 146 | g_serverMgr[type].monitorTid = 0; | 283 | g_serverMgr[type].monitorTid = 0; |
| 147 | - g_serverMgr[type].handle = NULL; | ||
| 148 | g_serverMgr[type].maxNum = 0; | 284 | g_serverMgr[type].maxNum = 0; |
| 149 | - g_serverMgr[type].linkedNum = 0; | ||
| 150 | g_serverMgr[type].linkType = 0; | 285 | g_serverMgr[type].linkType = 0; |
| 151 | g_serverMgr[type].start = NULL; | 286 | g_serverMgr[type].start = NULL; |
| 152 | g_serverMgr[type].stop = NULL; | 287 | g_serverMgr[type].stop = NULL; |
| @@ -157,11 +292,32 @@ static void ServerMgrNodeReset(ComponentType type) | |||
| 157 | int32_t ServerMgrInit(void) | 292 | int32_t ServerMgrInit(void) |
| 158 | { | 293 | { |
| 159 | for (int32_t i = 0; i < (int32_t)NR_COMPONENTS; ++i) { | 294 | for (int32_t i = 0; i < (int32_t)NR_COMPONENTS; ++i) { |
| 160 | - ServerMgrNodeReset(i); | ||
| 161 | if (ToolMutexInit(&g_serverMgr[i].lock) != SYS_OK) { | 295 | if (ToolMutexInit(&g_serverMgr[i].lock) != SYS_OK) { |
| 162 | SELF_LOG_ERROR("init mutex failed, num: %d, strerr: %s", i, strerror(ToolGetErrorCode())); | 296 | SELF_LOG_ERROR("init mutex failed, num: %d, strerr: %s", i, strerror(ToolGetErrorCode())); |
| 163 | return LOG_FAILURE; | 297 | return LOG_FAILURE; |
| 164 | } | 298 | } |
| 299 | + if (ToolMutexInit(&g_serverMgr[i].stateLock) != SYS_OK) { | ||
| 300 | + SELF_LOG_ERROR("init state mutex failed, num: %d, strerr: %s", i, strerror(ToolGetErrorCode())); | ||
| 301 | + return LOG_FAILURE; | ||
| 302 | + } | ||
| 303 | + ServerMgrNodeReset(i); | ||
| 304 | + // a fresh manager start drops every leftover generation node: NodeReset keeps them | ||
| 305 | + // for release-then-drain callers (lib_load), but no legitimate in-flight user can | ||
| 306 | + // exist at a fresh init, so pending nodes here are only stale state | ||
| 307 | + LOCK_WARN_LOG(&g_serverMgr[i].stateLock); | ||
| 308 | + ServerCommNode** slot = &g_serverMgr[i].commList; | ||
| 309 | + while (*slot != NULL) { | ||
| 310 | + ServerCommNode* node = *slot; | ||
| 311 | + if (node->refs != 0) { | ||
| 312 | + // never free a node with live holders: that would turn a leak into a UAF | ||
| 313 | + SELF_LOG_ERROR("server mgr init: pending comm node leaked, refs: %u", node->refs); | ||
| 314 | + slot = &node->next; | ||
| 315 | + continue; | ||
| 316 | + } | ||
| 317 | + *slot = node->next; | ||
| 318 | + XFREE(node); | ||
| 319 | + } | ||
| 320 | + UNLOCK_WARN_LOG(&g_serverMgr[i].stateLock); | ||
| 165 | } | 321 | } |
| 166 | return LOG_SUCCESS; | 322 | return LOG_SUCCESS; |
| 167 | } | 323 | } |
| @@ -202,8 +358,6 @@ STATIC int32_t ServerCheckContainer(const CommHandle* handle, ComponentType type | |||
| 202 | return LOG_FAILURE; | 358 | return LOG_FAILURE; |
| 203 | } | 359 | } |
| 204 | 360 | ||
| 205 | -STATIC ComponentType ServerGetComponentType(AdxCommConHandle handle) { return handle->comp; }; | ||
| 206 | - | ||
| 207 | STATIC int32_t ServerProcess(const CommHandle* handle, const void* msg, uint32_t len) | 361 | STATIC int32_t ServerProcess(const CommHandle* handle, const void* msg, uint32_t len) |
| 208 | { | 362 | { |
| 209 | (void)msg; | 363 | (void)msg; |
| @@ -223,7 +377,12 @@ STATIC int32_t ServerProcess(const CommHandle* handle, const void* msg, uint32_t | |||
| 223 | } | 377 | } |
| 224 | 378 | ||
| 225 | LOCK_WARN_LOG(&g_serverMgr[type].lock); | 379 | LOCK_WARN_LOG(&g_serverMgr[type].lock); |
| 380 | + // linkedNum/handle live under stateLock (nested under lock, never the other way round); | ||
| 381 | + // stateLock is NOT held across SessionMonitorCreate below: its join must not share a | ||
| 382 | + // lock with the old monitor tail, which takes stateLock only | ||
| 383 | + LOCK_WARN_LOG(&g_serverMgr[type].stateLock); | ||
| 226 | uint32_t num = g_serverMgr[type].linkedNum; | 384 | uint32_t num = g_serverMgr[type].linkedNum; |
| 385 | + UNLOCK_WARN_LOG(&g_serverMgr[type].stateLock); | ||
| 227 | if (num >= g_serverMgr[type].maxNum) { | 386 | if (num >= g_serverMgr[type].maxNum) { |
| 228 | (void)ServerReplyMsg(handle, SERVER_RESULT_OVERLOAD, SERVER_REPLY_OVERLOAD); | 387 | (void)ServerReplyMsg(handle, SERVER_RESULT_OVERLOAD, SERVER_REPLY_OVERLOAD); |
| 229 | SELF_LOG_ERROR("server process, overload, current num: %u, max num: %u", num, g_serverMgr[type].maxNum); | 388 | SELF_LOG_ERROR("server process, overload, current num: %u, max num: %u", num, g_serverMgr[type].maxNum); |
| @@ -250,12 +409,51 @@ STATIC int32_t ServerProcess(const CommHandle* handle, const void* msg, uint32_t | |||
| 250 | } | 409 | } |
| 251 | return LOG_SUCCESS; | 410 | return LOG_SUCCESS; |
| 252 | } | 411 | } |
| 253 | - g_serverMgr[type].handle = handle; | 412 | + // publish the session as a new generation node: every reference taken from now on |
| 413 | + // (monitor lifetime + in-flight requests) is counted on this node alone, references | ||
| 414 | + // of concurrent sessions land on their own nodes and never mix into one counter | ||
| 415 | + ServerCommNode* node = (ServerCommNode*)LogMalloc(sizeof(ServerCommNode)); | ||
| 416 | + if (node == NULL) { | ||
| 417 | + (void)ServerReplyMsg(handle, SERVER_RESULT_EXECUTION_ERROR, SERVER_REPLY_EXECUTION_ERROR); | ||
| 418 | + SELF_LOG_ERROR("server process, alloc comm node failed"); | ||
| 419 | + ServerDestroyCommHandle(handle); // nothing was published, release the framework handle | ||
| 420 | + UNLOCK_WARN_LOG(&g_serverMgr[type].lock); | ||
| 421 | + return LOG_FAILURE; | ||
| 422 | + } | ||
| 423 | + node->comm = handle; | ||
| 424 | + node->refs = 0; | ||
| 425 | + node->dying = false; | ||
| 426 | + LOCK_WARN_LOG(&g_serverMgr[type].stateLock); | ||
| 427 | + node->next = g_serverMgr[type].commList; | ||
| 428 | + g_serverMgr[type].commList = node; | ||
| 429 | + g_serverMgr[type].current = node; | ||
| 430 | + // reserve the link slot before creating the monitor thread, so a concurrent connect | ||
| 431 | + // is rejected by the overload check while this session is being set up. | ||
| 432 | + g_serverMgr[type].linkedNum++; | ||
| 433 | + UNLOCK_WARN_LOG(&g_serverMgr[type].stateLock); | ||
| 254 | 434 | ||
| 255 | - ret = SessionMonitorCreate(&g_serverMgr[type]); | 435 | + ret = SessionMonitorCreate(&g_serverMgr[type], node); |
| 256 | if (ret != LOG_SUCCESS) { | 436 | if (ret != LOG_SUCCESS) { |
| 257 | (void)ServerReplyMsg(handle, SERVER_RESULT_EXECUTION_ERROR, SERVER_REPLY_EXECUTION_ERROR); | 437 | (void)ServerReplyMsg(handle, SERVER_RESULT_EXECUTION_ERROR, SERVER_REPLY_EXECUTION_ERROR); |
| 258 | - ServerDestroyCommHandle(handle); | 438 | + // hand the node over to the reference machinery: pending in-flight users, if any, |
| 439 | + // release it to zero and the last ServerReleaseCommNode destroys the handle | ||
| 440 | + LOCK_WARN_LOG(&g_serverMgr[type].stateLock); | ||
| 441 | + if (g_serverMgr[type].current == node) { | ||
| 442 | + g_serverMgr[type].current = NULL; | ||
| 443 | + } | ||
| 444 | + g_serverMgr[type].linkedNum--; // rollback the reserved slot | ||
| 445 | + node->dying = true; | ||
| 446 | + bool destroyNow = (node->refs == 0); | ||
| 447 | + if (destroyNow) { | ||
| 448 | + // no monitor and no in-flight user: node is still the list head (mgr->lock is | ||
| 449 | + // held, no other publish can precede) | ||
| 450 | + g_serverMgr[type].commList = node->next; | ||
| 451 | + } | ||
| 452 | + UNLOCK_WARN_LOG(&g_serverMgr[type].stateLock); | ||
| 453 | + if (destroyNow) { | ||
| 454 | + ServerDestroyCommHandle(handle); | ||
| 455 | + XFREE(node); | ||
| 456 | + } | ||
| 259 | UNLOCK_WARN_LOG(&g_serverMgr[type].lock); | 457 | UNLOCK_WARN_LOG(&g_serverMgr[type].lock); |
| 260 | return LOG_FAILURE; | 458 | return LOG_FAILURE; |
| 261 | } | 459 | } |
| @@ -263,6 +461,8 @@ STATIC int32_t ServerProcess(const CommHandle* handle, const void* msg, uint32_t | |||
| 263 | if ((g_serverMgr[type].linkType == SERVER_LONG_LINK) || (g_serverMgr[type].linkType == SERVER_LONG_LINK_STOP)) { | 461 | if ((g_serverMgr[type].linkType == SERVER_LONG_LINK) || (g_serverMgr[type].linkType == SERVER_LONG_LINK_STOP)) { |
| 264 | ret = ServerReplyMsg(handle, SERVER_RESULT_SUCCESS, SERVER_REPLY_SUCCESS); | 462 | ret = ServerReplyMsg(handle, SERVER_RESULT_SUCCESS, SERVER_REPLY_SUCCESS); |
| 265 | if (ret != IDE_DAEMON_OK) { | 463 | if (ret != IDE_DAEMON_OK) { |
| 464 | + // the monitor thread owns the session node now: it marks the node dying and | ||
| 465 | + // rolls back the slot on exit, no extra rollback is needed here. | ||
| 266 | (void)SessionMonitorRelease(&g_serverMgr[type]); | 466 | (void)SessionMonitorRelease(&g_serverMgr[type]); |
| 267 | UNLOCK_WARN_LOG(&g_serverMgr[type].lock); | 467 | UNLOCK_WARN_LOG(&g_serverMgr[type].lock); |
| 268 | SELF_LOG_ERROR("server process, send ack failed, ret: %d", ret); | 468 | SELF_LOG_ERROR("server process, send ack failed, ret: %d", ret); |
| @@ -270,7 +470,6 @@ STATIC int32_t ServerProcess(const CommHandle* handle, const void* msg, uint32_t | |||
| 270 | } | 470 | } |
| 271 | } | 471 | } |
| 272 | g_serverMgr[type].processFlag = true; | 472 | g_serverMgr[type].processFlag = true; |
| 273 | - g_serverMgr[type].linkedNum++; | ||
| 274 | UNLOCK_WARN_LOG(&g_serverMgr[type].lock); | 473 | UNLOCK_WARN_LOG(&g_serverMgr[type].lock); |
| 275 | 474 | ||
| 276 | ret = g_serverMgr[type].start(&g_serverMgr[type]); | 475 | ret = g_serverMgr[type].start(&g_serverMgr[type]); |
| @@ -364,39 +563,40 @@ int32_t ServerCreateEx( | |||
| 364 | return AdxRegisterService(HDC_SERVICE_TYPE_IDE_FILE_TRANS, type, init, process, uninit); | 563 | return AdxRegisterService(HDC_SERVICE_TYPE_IDE_FILE_TRANS, type, init, process, uninit); |
| 365 | } | 564 | } |
| 366 | 565 | ||
| 367 | -static int32_t ServerCheckHandle(ServerHandle handle) | ||
| 368 | -{ | ||
| 369 | - ONE_ACT_ERR_LOG(handle == NULL, return LOG_FAILURE, "check handle failed, handle is null"); | ||
| 370 | - ONE_ACT_ERR_LOG(handle->handle == NULL, return LOG_FAILURE, "check handle failed, comm handle is null"); | ||
| 371 | - ComponentType type = ServerGetComponentType(handle->handle); | ||
| 372 | - ONE_ACT_ERR_LOG(type >= NR_COMPONENTS, return LOG_FAILURE, "check handle failed, invalid type %d", (int32_t)type); | ||
| 373 | - if (!g_serverMgr[type].monitorRunFlag) { | ||
| 374 | - SELF_LOG_ERROR("check handle failed, process is stopped"); | ||
| 375 | - return LOG_FAILURE; | ||
| 376 | - } | ||
| 377 | - return LOG_SUCCESS; | ||
| 378 | -} | ||
| 379 | - | ||
| 380 | int32_t ServerSyncFile(ServerHandle handle, const char* srcFileName, const char* dstFileName) | 566 | int32_t ServerSyncFile(ServerHandle handle, const char* srcFileName, const char* dstFileName) |
| 381 | { | 567 | { |
| 382 | - ONE_ACT_ERR_LOG(ServerCheckHandle(handle) != LOG_SUCCESS, return LOG_FAILURE, "sync file check handle failed"); | 568 | + ONE_ACT_ERR_LOG(handle == NULL, return LOG_FAILURE, "sync file check handle failed, handle is null"); |
| 383 | - ONE_ACT_ERR_LOG( | 569 | + ServerCommNode* node = ServerAcquireCommNode(handle, true); |
| 384 | - SessionMonitorIsHandleValid(handle->handle) != LOG_SUCCESS, return LOG_FAILURE, | 570 | + ONE_ACT_ERR_LOG(node == NULL, return LOG_FAILURE, "sync file check handle failed, comm handle is invalid"); |
| 385 | - "sync file session invalid, skip send"); | 571 | + AdxCommConHandle comm = node->comm; |
| 386 | - return AdxSendFileByHandle(handle->handle, IDE_FILE_REPORT_REQ, srcFileName, dstFileName, SEND_FILE_TYPE_TMP_FILE); | 572 | + int32_t ret = SessionMonitorIsHandleValid(comm); |
| 573 | + if (ret != LOG_SUCCESS) { | ||
| 574 | + SELF_LOG_ERROR("sync file session invalid, skip send"); | ||
| 575 | + } else { | ||
| 576 | + ret = AdxSendFileByHandle(comm, IDE_FILE_REPORT_REQ, srcFileName, dstFileName, SEND_FILE_TYPE_TMP_FILE); | ||
| 577 | + } | ||
| 578 | + ServerReleaseCommNode(handle, node); | ||
| 579 | + return ret; | ||
| 387 | } | 580 | } |
| 388 | 581 | ||
| 389 | int32_t ServerSendMsg(ServerHandle handle, const char* msg, uint32_t msgLen) | 582 | int32_t ServerSendMsg(ServerHandle handle, const char* msg, uint32_t msgLen) |
| 390 | { | 583 | { |
| 391 | - ONE_ACT_ERR_LOG(ServerCheckHandle(handle) != LOG_SUCCESS, return LOG_FAILURE, "send message check handle failed"); | 584 | + ONE_ACT_ERR_LOG(handle == NULL, return LOG_FAILURE, "send message check handle failed, handle is null"); |
| 392 | - return AdxSendMsg(handle->handle, msg, msgLen); | 585 | + ServerCommNode* node = ServerAcquireCommNode(handle, true); |
| 586 | + ONE_ACT_ERR_LOG(node == NULL, return LOG_FAILURE, "send message check handle failed, comm handle is invalid"); | ||
| 587 | + int32_t ret = AdxSendMsg(node->comm, msg, msgLen); | ||
| 588 | + ServerReleaseCommNode(handle, node); | ||
| 589 | + return ret; | ||
| 393 | } | 590 | } |
| 394 | 591 | ||
| 395 | int32_t ServerRecvMsg(ServerHandle handle, char** msg, uint32_t* msgLen, uint32_t timeout) | 592 | int32_t ServerRecvMsg(ServerHandle handle, char** msg, uint32_t* msgLen, uint32_t timeout) |
| 396 | { | 593 | { |
| 397 | - ONE_ACT_ERR_LOG( | 594 | + ONE_ACT_ERR_LOG(handle == NULL, return LOG_FAILURE, "receive message check handle failed, handle is null"); |
| 398 | - ServerCheckHandle(handle) != LOG_SUCCESS, return LOG_FAILURE, "receive message check handle failed"); | 595 | + ServerCommNode* node = ServerAcquireCommNode(handle, true); |
| 399 | - return AdxRecvMsg((AdxCommHandle)handle->handle, msg, msgLen, timeout); | 596 | + ONE_ACT_ERR_LOG(node == NULL, return LOG_FAILURE, "receive message check handle failed, comm handle is invalid"); |
| 597 | + int32_t ret = AdxRecvMsg((AdxCommHandle)node->comm, msg, msgLen, timeout); | ||
| 598 | + ServerReleaseCommNode(handle, node); | ||
| 599 | + return ret; | ||
| 400 | } | 600 | } |
| 401 | 601 | ||
| 402 | int32_t ServersStart(void) | 602 | int32_t ServersStart(void) |
| @@ -39,12 +39,31 @@ typedef void (*ServerStop)(void); | |||
| 39 | typedef int32_t (*ServerSend)(ServerHandle, const char*, uint32_t); | 39 | typedef int32_t (*ServerSend)(ServerHandle, const char*, uint32_t); |
| 40 | typedef int32_t (*ServerRecv)(ServerHandle, char**, uint32_t*, uint32_t); | 40 | typedef int32_t (*ServerRecv)(ServerHandle, char**, uint32_t*, uint32_t); |
| 41 | 41 | ||
| 42 | +// per-session comm reference node: created when ServerProcess publishes a session handle, | ||
| 43 | +// destroyed together with the handle. refs counts every holder of the comm handle (the | ||
| 44 | +// session monitor's lifetime reference plus in-flight requests); dying marks a handle | ||
| 45 | +// detached by its monitor or rolled back by ServerProcess. Nodes are kept in commList and | ||
| 46 | +// referenced by pointer: each generation is counted on its own node, references of | ||
| 47 | +// different sessions never mix, so a generation is destroyed exactly once by its own | ||
| 48 | +// last releaser. | ||
| 49 | +typedef struct ServerCommNode { | ||
| 50 | + AdxCommConHandle comm; | ||
| 51 | + uint32_t refs; | ||
| 52 | + bool dying; | ||
| 53 | + struct ServerCommNode* next; | ||
| 54 | +} ServerCommNode; | ||
| 55 | + | ||
| 42 | typedef struct ServerMgr { | 56 | typedef struct ServerMgr { |
| 43 | bool init; // init is used only once; true: be used; false: free | 57 | bool init; // init is used only once; true: be used; false: free |
| 44 | bool processFlag; // true: running process exists; false: no running process | 58 | bool processFlag; // true: running process exists; false: no running process |
| 45 | bool monitorRunFlag; // true: running status; false: stopped status | 59 | bool monitorRunFlag; // true: running status; false: stopped status |
| 46 | ToolThread monitorTid; // tid of session monitor | 60 | ToolThread monitorTid; // tid of session monitor |
| 47 | - AdxCommConHandle handle; | 61 | + // commList and current are guarded by stateLock (lock order: lock -> stateLock). |
| 62 | + // The session monitor tail and the acquire/release paths take stateLock ONLY: a caller | ||
| 63 | + // joining the monitor under lock (SessionMonitorCreate) must never share a lock with the | ||
| 64 | + // joined thread, otherwise concurrent session turnover can deadlock. | ||
| 65 | + ServerCommNode* commList; // all session generations, a dying node leaves the list when its refs drops to 0 | ||
| 66 | + ServerCommNode* current; // the published session node, the only source of new references | ||
| 48 | uint32_t maxNum; | 67 | uint32_t maxNum; |
| 49 | uint32_t linkedNum; | 68 | uint32_t linkedNum; |
| 50 | uint32_t linkType; | 69 | uint32_t linkType; |
| @@ -54,6 +73,7 @@ typedef struct ServerMgr { | |||
| 54 | ServerSend send; | 73 | ServerSend send; |
| 55 | ServerRecv recv; | 74 | ServerRecv recv; |
| 56 | ToolMutex lock; | 75 | ToolMutex lock; |
| 76 | + ToolMutex stateLock; | ||
| 57 | } ServerMgr; | 77 | } ServerMgr; |
| 58 | 78 | ||
| 59 | // total 256 byte | 79 | // total 256 byte |
| @@ -85,6 +85,8 @@ set(logDaemonSrc | |||
| 85 | ${CMAKE_CURRENT_SOURCE_DIR}/testcase/event_process/event_process_func_utest.cc | 85 | ${CMAKE_CURRENT_SOURCE_DIR}/testcase/event_process/event_process_func_utest.cc |
| 86 | # file monitor | 86 | # file monitor |
| 87 | ${CMAKE_CURRENT_SOURCE_DIR}/testcase/file_monitor/file_monitor_func_utest.cc | 87 | ${CMAKE_CURRENT_SOURCE_DIR}/testcase/file_monitor/file_monitor_func_utest.cc |
| 88 | + # server mgr ownership model (kept last: dedicated TU, real monitor threads) | ||
| 89 | + ${CMAKE_CURRENT_SOURCE_DIR}/testcase/server_mgr_func_utest.cc | ||
| 88 | ${TOP_DIR}/tests/ut/slog/ut/slog/testcase/main.cc | 90 | ${TOP_DIR}/tests/ut/slog/ut/slog/testcase/main.cc |
| 89 | ) | 91 | ) |
| 90 | 92 | ||
| @@ -158,7 +158,6 @@ TEST_F(CPU_DETECT_EXCP_UTEST, CpuDetectServerStartInvalidMagic) | |||
| 158 | { | 158 | { |
| 159 | ServerMgr tmp = {}; | 159 | ServerMgr tmp = {}; |
| 160 | CommHandle comm = {COMM_HDC, 0x12345, COMPONENT_GETD_FILE}; | 160 | CommHandle comm = {COMM_HDC, 0x12345, COMPONENT_GETD_FILE}; |
| 161 | - tmp.handle = &comm; | ||
| 162 | tmp.send = ServerSendMsgStub; | 161 | tmp.send = ServerSendMsgStub; |
| 163 | tmp.recv = ServerRecvMsgStub; | 162 | tmp.recv = ServerRecvMsgStub; |
| 164 | ServerHandle handle = &tmp; | 163 | ServerHandle handle = &tmp; |
| @@ -172,7 +171,6 @@ TEST_F(CPU_DETECT_EXCP_UTEST, CpuDetectServerStart) | |||
| 172 | 171 | ||
| 173 | ServerMgr tmp = {}; | 172 | ServerMgr tmp = {}; |
| 174 | CommHandle comm = {COMM_HDC, 0x12345, COMPONENT_GETD_FILE}; | 173 | CommHandle comm = {COMM_HDC, 0x12345, COMPONENT_GETD_FILE}; |
| 175 | - tmp.handle = &comm; | ||
| 176 | tmp.send = ServerSendMsgStub; | 174 | tmp.send = ServerSendMsgStub; |
| 177 | tmp.recv = ServerRecvMsgStub; | 175 | tmp.recv = ServerRecvMsgStub; |
| 178 | ServerHandle handle = &tmp; | 176 | ServerHandle handle = &tmp; |
| @@ -189,7 +187,6 @@ TEST_F(CPU_DETECT_EXCP_UTEST, CpuDetectServerStartCpuDetectStartNull) | |||
| 189 | 187 | ||
| 190 | ServerMgr tmp = {}; | 188 | ServerMgr tmp = {}; |
| 191 | CommHandle comm = {COMM_HDC, 0x12345, COMPONENT_GETD_FILE}; | 189 | CommHandle comm = {COMM_HDC, 0x12345, COMPONENT_GETD_FILE}; |
| 192 | - tmp.handle = &comm; | ||
| 193 | tmp.send = ServerSendMsgStub; | 190 | tmp.send = ServerSendMsgStub; |
| 194 | tmp.recv = ServerRecvMsgStub; | 191 | tmp.recv = ServerRecvMsgStub; |
| 195 | ServerHandle handle = &tmp; | 192 | ServerHandle handle = &tmp; |
| @@ -205,7 +202,6 @@ TEST_F(CPU_DETECT_EXCP_UTEST, CpuDetectTestcaseFailed) | |||
| 205 | MOCKER(CpuDetectStart).stubs().will(returnValue(100)); | 202 | MOCKER(CpuDetectStart).stubs().will(returnValue(100)); |
| 206 | ServerMgr tmp = {}; | 203 | ServerMgr tmp = {}; |
| 207 | CommHandle comm = {COMM_HDC, 0x12345, COMPONENT_GETD_FILE}; | 204 | CommHandle comm = {COMM_HDC, 0x12345, COMPONENT_GETD_FILE}; |
| 208 | - tmp.handle = &comm; | ||
| 209 | tmp.send = ServerSendMsgStub; | 205 | tmp.send = ServerSendMsgStub; |
| 210 | tmp.recv = ServerRecvMsgStub; | 206 | tmp.recv = ServerRecvMsgStub; |
| 211 | ServerHandle handle = &tmp; | 207 | ServerHandle handle = &tmp; |
| @@ -221,7 +217,6 @@ TEST_F(CPU_DETECT_EXCP_UTEST, CpuDetectTestcaseTimeout) | |||
| 221 | MOCKER(CpuDetectStart).stubs().will(returnValue(1000)); | 217 | MOCKER(CpuDetectStart).stubs().will(returnValue(1000)); |
| 222 | ServerMgr tmp = {}; | 218 | ServerMgr tmp = {}; |
| 223 | CommHandle comm = {COMM_HDC, 0x12345, COMPONENT_GETD_FILE}; | 219 | CommHandle comm = {COMM_HDC, 0x12345, COMPONENT_GETD_FILE}; |
| 224 | - tmp.handle = &comm; | ||
| 225 | tmp.send = ServerSendMsgStub; | 220 | tmp.send = ServerSendMsgStub; |
| 226 | tmp.recv = ServerRecvMsgStub; | 221 | tmp.recv = ServerRecvMsgStub; |
| 227 | ServerHandle handle = &tmp; | 222 | ServerHandle handle = &tmp; |
| @@ -237,7 +232,6 @@ TEST_F(CPU_DETECT_EXCP_UTEST, CpuDetectServerStartRecvFailed) | |||
| 237 | 232 | ||
| 238 | ServerMgr tmp = {}; | 233 | ServerMgr tmp = {}; |
| 239 | CommHandle comm = {COMM_HDC, 0x12345, COMPONENT_GETD_FILE}; | 234 | CommHandle comm = {COMM_HDC, 0x12345, COMPONENT_GETD_FILE}; |
| 240 | - tmp.handle = &comm; | ||
| 241 | tmp.send = ServerSendMsgStub; | 235 | tmp.send = ServerSendMsgStub; |
| 242 | tmp.recv = ServerRecvMsgFailedStub; | 236 | tmp.recv = ServerRecvMsgFailedStub; |
| 243 | ServerHandle handle = &tmp; | 237 | ServerHandle handle = &tmp; |
| @@ -253,7 +247,6 @@ TEST_F(CPU_DETECT_EXCP_UTEST, CpuDetectServerLifecycleMultiRequest) | |||
| 253 | 247 | ||
| 254 | ServerMgr tmp = {}; | 248 | ServerMgr tmp = {}; |
| 255 | CommHandle comm = {COMM_HDC, 0x12345, COMPONENT_GETD_FILE}; | 249 | CommHandle comm = {COMM_HDC, 0x12345, COMPONENT_GETD_FILE}; |
| 256 | - tmp.handle = &comm; | ||
| 257 | tmp.send = ServerSendMsgStub; | 250 | tmp.send = ServerSendMsgStub; |
| 258 | tmp.recv = ServerRecvMsgStub; | 251 | tmp.recv = ServerRecvMsgStub; |
| 259 | ServerHandle handle = &tmp; | 252 | ServerHandle handle = &tmp; |
| @@ -366,7 +366,10 @@ TEST_F(EP_LOG_DAEMON_FUNC_UTEST, LogDaemonMainInvalidRunEnv) | |||
| 366 | TEST_F(EP_LOG_DAEMON_FUNC_UTEST, LogDaemonMainServerProcessFailed) | 366 | TEST_F(EP_LOG_DAEMON_FUNC_UTEST, LogDaemonMainServerProcessFailed) |
| 367 | { | 367 | { |
| 368 | ServerAttr attr = {0}; | 368 | ServerAttr attr = {0}; |
| 369 | - attr.num = 1; | 369 | + // num is 2: the ack-failed session reserves one link slot until the session monitor |
| 370 | + // releases it; pthread_create is stubbed so no monitor thread runs and the slot stays | ||
| 371 | + // reserved, one more slot keeps the success path below reachable. | ||
| 372 | + attr.num = 2; | ||
🔵 Low Priority 变更行(log_daemon_func_utest.cc:369-372)把 问题在于覆盖方向:
建议:调整 num 可以保留,但需补充对 linkedNum/handle 残留状态的断言,并新增覆盖「commRefCnt > 1 时 monitor 退出 → dyingHandle 延迟销毁 → 最后释放者销毁、linkedNum 归零」的用例。 ![]() ![]() Incorrect? | |||
| 370 | attr.linkType = 0; | 373 | attr.linkType = 0; |
| 371 | attr.runEnv = 1; | 374 | attr.runEnv = 1; |
| 372 | MOCKER(ToolJoinTask).stubs().will(returnValue(0)); | 375 | MOCKER(ToolJoinTask).stubs().will(returnValue(0)); |
| @@ -0,0 +1,475 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +// server_mgr ownership-model cases kept in a dedicated translation unit: the reference-count | ||
| 12 | +// scenarios need real session monitor threads, and adding them to the shared testcase file | ||
| 13 | +// shifts the linked layout, which breaks the mockcpp ApiHook patches of the other cases. | ||
| 14 | +#include <thread> | ||
| 15 | +#include <unistd.h> | ||
| 16 | + | ||
| 17 | +#include <chrono> | ||
| 18 | +#include <future> | ||
| 19 | + | ||
| 20 | +#include "gtest/gtest.h" | ||
| 21 | +#include "mockcpp/mockcpp.hpp" | ||
| 22 | + | ||
| 23 | +#include "adcore_api.h" | ||
| 24 | +#include "ascend_hal.h" | ||
| 25 | +#include "log_common.h" | ||
| 26 | +#include "server_mgr.h" | ||
| 27 | + | ||
| 28 | +extern ServerMgr g_serverMgr[NR_COMPONENTS]; | ||
| 29 | +extern "C" { | ||
| 30 | +// ServerProcess is internal to log_daemon (STATIC, expanded empty in UT builds) | ||
| 31 | +extern int32_t ServerProcess(const CommHandle* handle, const void* msg, uint32_t len); | ||
| 32 | +} | ||
| 33 | + | ||
| 34 | +namespace { | ||
| 35 | +bool g_utSessionValid = true; | ||
| 36 | +int32_t g_utDestroyCount = 0; | ||
| 37 | +bool g_utRecvEntered = false; | ||
| 38 | +bool g_utStopEntered = false; | ||
| 39 | +OptHandle g_utInvalidSession = 0; | ||
| 40 | + | ||
| 41 | +int32_t UtSessionAttrStateful(AdxCommConHandle handle, int32_t attr, int32_t* value) | ||
| 42 | +{ | ||
| 43 | + (void)handle; | ||
| 44 | + if (attr == (int32_t)HDC_SESSION_ATTR_RUN_ENV) { | ||
| 45 | + *value = RUN_ENV_PHYSICAL; | ||
| 46 | + return 0; | ||
| 47 | + } | ||
| 48 | + *value = g_utSessionValid ? 1 : (int32_t)HDC_SESSION_STATUS_CLOSE; // 1: connect status | ||
| 49 | + return 0; | ||
| 50 | +} | ||
| 51 | + | ||
| 52 | +// validity keyed by the session pointer: only the marked session breaks, concurrent | ||
| 53 | +// sessions of a multi-slot component stay alive | ||
| 54 | +int32_t UtSessionAttrPerSession(AdxCommConHandle handle, int32_t attr, int32_t* value) | ||
| 55 | +{ | ||
| 56 | + if (attr == (int32_t)HDC_SESSION_ATTR_RUN_ENV) { | ||
| 57 | + *value = RUN_ENV_PHYSICAL; | ||
| 58 | + return 0; | ||
| 59 | + } | ||
| 60 | + *value = (handle->session == g_utInvalidSession) ? (int32_t)HDC_SESSION_STATUS_CLOSE : 1; | ||
| 61 | + return 0; | ||
| 62 | +} | ||
| 63 | + | ||
| 64 | +// parks the FIRST LogFree call after arming: that is the session monitor's thread-entry | ||
| 65 | +// ctx release, which sits right before the node acquire. Parking there simulates a thread | ||
| 66 | +// that has not run its first instruction yet while a new session gets published | ||
| 67 | +int32_t g_utLogFreeParkArmed = 0; | ||
| 68 | +bool g_utLogFreeRelease = false; | ||
| 69 | + | ||
| 70 | +void UtParkingLogFree(void* buffer) | ||
| 71 | +{ | ||
| 72 | + if (g_utLogFreeParkArmed != 0) { | ||
| 73 | + g_utLogFreeParkArmed = 0; // park exactly once, the monitor entry ctx | ||
| 74 | + for (int32_t i = 0; (i < 10000) && !g_utLogFreeRelease; i++) { | ||
| 75 | + usleep(1000); // 10s cap, the test releases the park well before | ||
| 76 | + } | ||
| 77 | + } | ||
| 78 | + free(buffer); | ||
| 79 | +} | ||
| 80 | + | ||
| 81 | +void UtCountDestroy(AdxCommHandle handle) | ||
| 82 | +{ | ||
| 83 | + (void)handle; | ||
| 84 | + g_utDestroyCount++; | ||
| 85 | +} | ||
| 86 | + | ||
| 87 | +int32_t UtBlockingRecv(AdxCommHandle handle, char** data, uint32_t* len, uint32_t timeout) | ||
| 88 | +{ | ||
| 89 | + (void)handle; | ||
| 90 | + (void)data; | ||
| 91 | + (void)len; | ||
| 92 | + (void)timeout; | ||
| 93 | + g_utRecvEntered = true; | ||
| 94 | + usleep(300 * 1000); // hold the reference while the session monitor exits | ||
| 95 | + return 0; | ||
| 96 | +} | ||
| 97 | + | ||
| 98 | +int32_t TestServerStartNoop(ServerHandle handle) | ||
| 99 | +{ | ||
| 100 | + (void)handle; | ||
| 101 | + return 0; | ||
| 102 | +} | ||
| 103 | + | ||
| 104 | +void TestServerStopNoop(void) {} | ||
| 105 | + | ||
| 106 | +// parks the session monitor inside its exit tail (stop runs after the stateLock section), | ||
| 107 | +// so the next ServerProcess joins a thread that is alive but already detached | ||
| 108 | +void UtParkedStop(void) | ||
| 109 | +{ | ||
| 110 | + g_utStopEntered = true; | ||
| 111 | + usleep(500 * 1000); | ||
| 112 | +} | ||
| 113 | +} // namespace | ||
| 114 | + | ||
| 115 | +// covers the lib_load upgrade pattern: ServerRelease runs while an in-flight request still | ||
| 116 | +// holds a reference (release first, drain afterwards). The dying node must survive the | ||
| 117 | +// ServerMgrNodeReset inside ServerRelease, so the last releaser destroys the comm handle. | ||
| 118 | +TEST(ServerMgrDyingListUtest, DyingHandleSurvivesReleaseAndIsDestroyedByLastReleaser) | ||
| 119 | +{ | ||
| 120 | + g_utSessionValid = true; | ||
| 121 | + g_utDestroyCount = 0; | ||
| 122 | + g_utRecvEntered = false; | ||
| 123 | + ServerAttr attr = {0}; | ||
| 124 | + attr.num = 1; | ||
| 125 | + attr.linkType = SERVER_LONG_LINK; | ||
| 126 | + attr.runEnv = ENV_ALL; | ||
| 127 | + EXPECT_EQ(0, ServerMgrInit()); | ||
| 128 | + EXPECT_EQ(0, ServerCreate(ComponentType::COMPONENT_GETD_FILE, TestServerStartNoop, TestServerStopNoop, &attr)); | ||
| 129 | + | ||
| 130 | + // register before any mock is active (mockcpp ApiHooks are layout sensitive), the | ||
| 131 | + // AdxGetAttrByCommHandle mock is installed afterwards for the monitor session checks | ||
| 132 | + MOCKER(AdxGetAttrByCommHandle).stubs().will(invoke(UtSessionAttrStateful)); | ||
| 133 | + MOCKER(AdxRecvMsg).stubs().will(invoke(UtBlockingRecv)); | ||
| 134 | + MOCKER(AdxDestroyCommHandle).stubs().will(invoke(UtCountDestroy)); | ||
| 135 | + | ||
| 136 | + CommHandle handle; | ||
| 137 | + handle.type = (OptType)COMM_HDC; | ||
| 138 | + handle.session = (OptHandle)0x12345; | ||
| 139 | + handle.timeout = 0; | ||
| 140 | + handle.client = nullptr; | ||
| 141 | + handle.comp = COMPONENT_GETD_FILE; | ||
| 142 | + EXPECT_EQ(0, ServerProcess(&handle, nullptr, 0)); // real session monitor thread runs | ||
| 143 | + | ||
| 144 | + std::thread recvThread([] { | ||
| 145 | + char* msg = nullptr; | ||
| 146 | + uint32_t len = 0; | ||
| 147 | + (void)ServerRecvMsg(&g_serverMgr[ComponentType::COMPONENT_GETD_FILE], &msg, &len, 1000); | ||
| 148 | + }); | ||
| 149 | + bool recvEntered = false; | ||
| 150 | + for (int32_t i = 0; (i < 3000) && !recvEntered; i++) { | ||
| 151 | + recvEntered = g_utRecvEntered; | ||
| 152 | + if (!recvEntered) { | ||
| 153 | + usleep(1000); // wait until the in-flight reference is really held (3s cap) | ||
| 154 | + } | ||
| 155 | + } | ||
| 156 | + g_utSessionValid = false; // the monitor exits on its next poll | ||
| 157 | + | ||
| 158 | + ServerRelease(ComponentType::COMPONENT_GETD_FILE); // release first (lib_load upgrade order) | ||
| 159 | + EXPECT_TRUE(recvEntered); | ||
| 160 | + // the pending node survives the reset with exactly the in-flight reference: the recv | ||
| 161 | + // reference is counted on its own node and waits for its own last releaser | ||
| 162 | + EXPECT_EQ(nullptr, g_serverMgr[ComponentType::COMPONENT_GETD_FILE].current); | ||
| 163 | + ASSERT_NE(nullptr, g_serverMgr[ComponentType::COMPONENT_GETD_FILE].commList); | ||
| 164 | + EXPECT_EQ(1U, g_serverMgr[ComponentType::COMPONENT_GETD_FILE].commList->refs); | ||
| 165 | + | ||
| 166 | + recvThread.join(); // drain: the last releaser destroys the detached handle | ||
| 167 | + EXPECT_EQ(1, g_utDestroyCount); | ||
| 168 | + EXPECT_EQ(nullptr, g_serverMgr[ComponentType::COMPONENT_GETD_FILE].commList); | ||
| 169 | + | ||
| 170 | + GlobalMockObject::reset(); | ||
| 171 | +} | ||
| 172 | + | ||
| 173 | +// after a full release the manager must be reusable: no link slot residue, no dangling | ||
| 174 | +// handle, no pending dying node, and a new session of the same component is accepted again | ||
| 175 | +TEST(ServerMgrDyingListUtest, ReleaseLeavesNoSlotOrHandleResidue) | ||
| 176 | +{ | ||
| 177 | + ServerAttr attr = {0}; | ||
| 178 | + attr.num = 1; | ||
| 179 | + attr.linkType = SERVER_LONG_LINK; | ||
| 180 | + attr.runEnv = ENV_ALL; | ||
| 181 | + EXPECT_EQ(0, ServerMgrInit()); | ||
| 182 | + EXPECT_EQ(0, ServerCreate(ComponentType::COMPONENT_GETD_FILE, TestServerStartNoop, TestServerStopNoop, &attr)); | ||
| 183 | + | ||
| 184 | + MOCKER(AdxGetAttrByCommHandle).stubs().will(invoke(UtSessionAttrStateful)); | ||
| 185 | + g_utSessionValid = true; | ||
| 186 | + | ||
| 187 | + CommHandle handle; | ||
| 188 | + handle.type = (OptType)COMM_HDC; | ||
| 189 | + handle.session = (OptHandle)0x12345; | ||
| 190 | + handle.timeout = 0; | ||
| 191 | + handle.client = nullptr; | ||
| 192 | + handle.comp = COMPONENT_GETD_FILE; | ||
| 193 | + EXPECT_EQ(0, ServerProcess(&handle, nullptr, 0)); // real monitor thread runs | ||
| 194 | + | ||
| 195 | + ServerRelease(ComponentType::COMPONENT_GETD_FILE); | ||
| 196 | + EXPECT_EQ(0U, g_serverMgr[ComponentType::COMPONENT_GETD_FILE].linkedNum); | ||
| 197 | + EXPECT_EQ(nullptr, g_serverMgr[ComponentType::COMPONENT_GETD_FILE].current); | ||
| 198 | + EXPECT_EQ(nullptr, g_serverMgr[ComponentType::COMPONENT_GETD_FILE].commList); | ||
| 199 | + | ||
| 200 | + EXPECT_EQ(0, ServerCreate(ComponentType::COMPONENT_GETD_FILE, TestServerStartNoop, TestServerStopNoop, &attr)); | ||
| 201 | + EXPECT_EQ(0, ServerProcess(&handle, nullptr, 0)); // slot accounting intact, session accepted | ||
| 202 | + ServerRelease(ComponentType::COMPONENT_GETD_FILE); | ||
| 203 | + | ||
| 204 | + GlobalMockObject::reset(); | ||
| 205 | +} | ||
| 206 | + | ||
| 207 | +// concurrent session turnover (the scenario the review flagged for the join-under-lock | ||
| 208 | +// deadlock): the old monitor is parked alive in its exit tail while a new connection runs | ||
| 209 | +// ServerProcess, whose SessionMonitorCreate joins that old thread. With the stateLock split | ||
| 210 | +// the new connection must complete within the timeout and the old thread must exit cleanly. | ||
| 211 | +TEST(ServerMgrDyingListUtest, NewSessionCompletesWhileOldMonitorIsInExitTail) | ||
| 212 | +{ | ||
| 213 | + g_utSessionValid = true; | ||
| 214 | + g_utStopEntered = false; | ||
| 215 | + ServerAttr attr = {0}; | ||
| 216 | + attr.num = 2; // multiple slots: the turnover path stays reachable while the old slot exists | ||
| 217 | + attr.linkType = SERVER_LONG_LINK; | ||
| 218 | + attr.runEnv = ENV_ALL; | ||
| 219 | + EXPECT_EQ(0, ServerMgrInit()); | ||
| 220 | + EXPECT_EQ(0, ServerCreate(ComponentType::COMPONENT_GETD_FILE, TestServerStartNoop, UtParkedStop, &attr)); | ||
| 221 | + | ||
| 222 | + MOCKER(AdxGetAttrByCommHandle).stubs().will(invoke(UtSessionAttrStateful)); | ||
| 223 | + | ||
| 224 | + CommHandle handleA; | ||
| 225 | + handleA.type = (OptType)COMM_HDC; | ||
| 226 | + handleA.session = (OptHandle)0x11111; | ||
| 227 | + handleA.timeout = 0; | ||
| 228 | + handleA.client = nullptr; | ||
| 229 | + handleA.comp = COMPONENT_GETD_FILE; | ||
| 230 | + EXPECT_EQ(0, ServerProcess(&handleA, nullptr, 0)); // first session, real monitor thread | ||
| 231 | + | ||
| 232 | + g_utSessionValid = false; // the old monitor exits on its next poll and parks in stop() | ||
| 233 | + bool stopEntered = false; | ||
| 234 | + for (int32_t i = 0; (i < 3000) && !stopEntered; i++) { | ||
| 235 | + stopEntered = g_utStopEntered; | ||
| 236 | + if (!stopEntered) { | ||
| 237 | + usleep(1000); // 3s cap | ||
| 238 | + } | ||
| 239 | + } | ||
| 240 | + ASSERT_TRUE(stopEntered); | ||
| 241 | + | ||
| 242 | + // the old monitor is alive in its exit tail: this call joins it inside SessionMonitorCreate. | ||
| 243 | + // std::thread + completion flag on purpose: a std::async future destructor would block | ||
| 244 | + // on the unfinished task after an ASSERT failure, turning a deadlock regression into a | ||
| 245 | + // full test-binary hang instead of a clean failure | ||
| 246 | + CommHandle handleB; | ||
| 247 | + handleB.type = (OptType)COMM_HDC; | ||
| 248 | + handleB.session = (OptHandle)0x22222; | ||
| 249 | + handleB.timeout = 0; | ||
| 250 | + handleB.client = nullptr; | ||
| 251 | + handleB.comp = COMPONENT_GETD_FILE; | ||
| 252 | + int32_t turnRet = -1; | ||
| 253 | + bool turnDone = false; | ||
| 254 | + std::thread turnThread([&] { | ||
| 255 | + turnRet = ServerProcess(&handleB, nullptr, 0); | ||
| 256 | + turnDone = true; | ||
| 257 | + }); | ||
| 258 | + bool finished = false; | ||
| 259 | + for (int32_t i = 0; (i < 5000) && !finished; i++) { | ||
| 260 | + finished = turnDone; | ||
级别:提示
问题:并发用例用普通 bool(turnDone、g_utStopEntered、g_utRecvEntered)跨线程读写,没有使用 std::atomic、互斥量或内存序建立同步,构成 C++ 数据竞争。
影响:数据竞争属于未定义行为;优化构建下主线程可能缓存或提升对完成标志的读取,看不到工作线程写入,5 秒后误报 ADD_FAILURE 并 detach 未完成任务,导致专门检测换会话死锁回归的用例抖动或误报,掩盖真实回归。
修复建议:将 turnDone、g_utStopEntered、g_utRecvEntered 等所有跨线程访问的完成标志改为 std::atomic ![]() ![]() | |||
| 261 | + if (!finished) { | ||
| 262 | + usleep(1000); // 5s deadline for the turnover to complete | ||
| 263 | + } | ||
| 264 | + } | ||
| 265 | + if (!finished) { | ||
| 266 | + ADD_FAILURE() << "ServerProcess did not complete while the old monitor was in its exit tail"; | ||
| 267 | + turnThread.detach(); // never join a deadlocked task: keep the failure observable | ||
| 268 | + return; | ||
| 269 | + } | ||
| 270 | + turnThread.join(); | ||
| 271 | + EXPECT_EQ(0, turnRet); | ||
| 272 | + | ||
| 273 | + ServerRelease(ComponentType::COMPONENT_GETD_FILE); // joins the new monitor, no residue | ||
| 274 | + EXPECT_EQ(0U, g_serverMgr[ComponentType::COMPONENT_GETD_FILE].linkedNum); | ||
| 275 | + EXPECT_EQ(nullptr, g_serverMgr[ComponentType::COMPONENT_GETD_FILE].current); | ||
| 276 | + EXPECT_EQ(nullptr, g_serverMgr[ComponentType::COMPONENT_GETD_FILE].commList); | ||
| 277 | + | ||
| 278 | + GlobalMockObject::reset(); | ||
| 279 | +} | ||
| 280 | + | ||
| 281 | +// cross-generation reference isolation (the scenario the review flagged for the shared | ||
| 282 | +// counter): session B is published (its ServerProcess parks inside the join of monitor A) | ||
| 283 | +// and an in-flight request acquires B while monitor A is still running. A's exit must not | ||
| 284 | +// count B's reference on A's node: A is destroyed by its own monitor, B's node keeps only | ||
| 285 | +// its own holders. With a shared counter A's node would absorb B's reference, never reach | ||
| 286 | +// zero and leak, and later generations would under-count into a premature destroy. | ||
| 287 | +TEST(ServerMgrDyingListUtest, CrossGenerationReferencesAreCountedPerSession) | ||
| 288 | +{ | ||
| 289 | + g_utInvalidSession = 0; | ||
| 290 | + g_utRecvEntered = false; | ||
| 291 | + ServerAttr attr = {0}; | ||
| 292 | + attr.num = 2; // two slots: B is published while A still occupies its slot | ||
| 293 | + attr.linkType = SERVER_LONG_LINK; | ||
| 294 | + attr.runEnv = ENV_ALL; | ||
| 295 | + EXPECT_EQ(0, ServerMgrInit()); | ||
| 296 | + EXPECT_EQ(0, ServerCreate(ComponentType::COMPONENT_GETD_FILE, TestServerStartNoop, TestServerStopNoop, &attr)); | ||
| 297 | + | ||
| 298 | + MOCKER(AdxGetAttrByCommHandle).stubs().will(invoke(UtSessionAttrPerSession)); | ||
| 299 | + MOCKER(AdxRecvMsg).stubs().will(invoke(UtBlockingRecv)); | ||
| 300 | + | ||
| 301 | + CommHandle handleA; | ||
| 302 | + handleA.type = (OptType)COMM_HDC; | ||
| 303 | + handleA.session = (OptHandle)0x11111; | ||
| 304 | + handleA.timeout = 0; | ||
| 305 | + handleA.client = nullptr; | ||
| 306 | + handleA.comp = COMPONENT_GETD_FILE; | ||
| 307 | + EXPECT_EQ(0, ServerProcess(&handleA, nullptr, 0)); // session A, monitor A runs | ||
| 308 | + | ||
| 309 | + // session B is published first, its ServerProcess then parks inside the join of A | ||
| 310 | + CommHandle handleB; | ||
| 311 | + handleB.type = (OptType)COMM_HDC; | ||
| 312 | + handleB.session = (OptHandle)0x22222; | ||
| 313 | + handleB.timeout = 0; | ||
| 314 | + handleB.client = nullptr; | ||
| 315 | + handleB.comp = COMPONENT_GETD_FILE; | ||
| 316 | + int32_t turnRet = -1; | ||
| 317 | + bool turnDone = false; | ||
| 318 | + std::thread turnThread([&] { | ||
| 319 | + turnRet = ServerProcess(&handleB, nullptr, 0); | ||
| 320 | + turnDone = true; | ||
| 321 | + }); | ||
| 322 | + bool published = false; | ||
| 323 | + for (int32_t i = 0; (i < 3000) && !published; i++) { | ||
| 324 | + published = (g_serverMgr[ComponentType::COMPONENT_GETD_FILE].current != nullptr) && | ||
| 325 | + (g_serverMgr[ComponentType::COMPONENT_GETD_FILE].current->comm->session == (OptHandle)0x22222); | ||
| 326 | + if (!published) { | ||
| 327 | + usleep(1000); // 3s cap until B is published | ||
| 328 | + } | ||
| 329 | + } | ||
| 330 | + ASSERT_TRUE(published); | ||
| 331 | + | ||
| 332 | + // an in-flight request acquires the newly published B while monitor A is still running | ||
| 333 | + std::thread recvThread([] { | ||
| 334 | + char* msg = nullptr; | ||
| 335 | + uint32_t len = 0; | ||
| 336 | + (void)ServerRecvMsg(&g_serverMgr[ComponentType::COMPONENT_GETD_FILE], &msg, &len, 1000); | ||
| 337 | + }); | ||
| 338 | + bool recvEntered = false; | ||
| 339 | + for (int32_t i = 0; (i < 3000) && !recvEntered; i++) { | ||
| 340 | + recvEntered = g_utRecvEntered; | ||
| 341 | + if (!recvEntered) { | ||
| 342 | + usleep(1000); | ||
| 343 | + } | ||
| 344 | + } | ||
| 345 | + ASSERT_TRUE(recvEntered); | ||
| 346 | + | ||
| 347 | + g_utInvalidSession = (OptHandle)0x11111; // session A breaks, monitor A exits and detaches | ||
| 348 | + | ||
| 349 | + bool finished = false; | ||
| 350 | + for (int32_t i = 0; (i < 5000) && !finished; i++) { | ||
| 351 | + finished = turnDone; | ||
| 352 | + if (!finished) { | ||
| 353 | + usleep(1000); // 5s deadline: join of A + creation of monitor B | ||
| 354 | + } | ||
| 355 | + } | ||
| 356 | + if (!finished) { | ||
| 357 | + ADD_FAILURE() << "ServerProcess of session B did not complete after session A broke"; | ||
| 358 | + turnThread.detach(); // never join a deadlocked task: keep the failure observable | ||
| 359 | + recvThread.join(); | ||
| 360 | + return; | ||
| 361 | + } | ||
| 362 | + turnThread.join(); | ||
| 363 | + EXPECT_EQ(0, turnRet); | ||
| 364 | + recvThread.join(); // the in-flight request on B releases its own reference | ||
| 365 | + | ||
| 366 | + // A's node is fully destroyed by its own monitor (it never counted B's reference); | ||
| 367 | + // B's node keeps exactly its own holders: the monitor B lifetime reference | ||
| 368 | + const ServerMgr* mgr = &g_serverMgr[ComponentType::COMPONENT_GETD_FILE]; | ||
| 369 | + ASSERT_NE(nullptr, mgr->commList); | ||
| 370 | + EXPECT_EQ(nullptr, mgr->commList->next); // exactly one generation left: B | ||
| 371 | + EXPECT_EQ((OptHandle)0x22222, mgr->commList->comm->session); | ||
| 372 | + EXPECT_EQ(1U, mgr->commList->refs); // monitor B lifetime only, the recv reference was returned | ||
| 373 | + EXPECT_FALSE(mgr->commList->dying); | ||
| 374 | + EXPECT_EQ(mgr->commList, mgr->current); | ||
| 375 | + | ||
| 376 | + ServerRelease(ComponentType::COMPONENT_GETD_FILE); | ||
| 377 | + EXPECT_EQ(0U, mgr->linkedNum); | ||
| 378 | + EXPECT_EQ(nullptr, mgr->current); | ||
| 379 | + EXPECT_EQ(nullptr, mgr->commList); | ||
| 380 | + | ||
| 381 | + GlobalMockObject::reset(); | ||
| 382 | +} | ||
| 383 | + | ||
| 384 | +// scheduling race regression (the fatal review round): the session monitor must bind to | ||
| 385 | +// the node of its own generation even when its thread starts late. Monitor A is parked | ||
| 386 | +// at its thread entry (ctx release, right before the acquire), session B is published | ||
| 387 | +// while A never ran, then A is released: A must operate on (and destroy) node A, while | ||
| 388 | +// B keeps its own monitor and node. Acquiring from mgr->current instead would make A | ||
| 389 | +// hold B's node and orphan A's. | ||
| 390 | +TEST(ServerMgrDyingListUtest, MonitorBindsToItsOwnGenerationNodeUnderDelayedStart) | ||
| 391 | +{ | ||
| 392 | + g_utInvalidSession = 0; | ||
| 393 | + g_utDestroyCount = 0; | ||
| 394 | + g_utLogFreeParkArmed = 1; | ||
| 395 | + g_utLogFreeRelease = false; | ||
| 396 | + ServerAttr attr = {0}; | ||
| 397 | + attr.num = 2; | ||
| 398 | + attr.linkType = SERVER_LONG_LINK; | ||
| 399 | + attr.runEnv = ENV_ALL; | ||
| 400 | + EXPECT_EQ(0, ServerMgrInit()); | ||
| 401 | + EXPECT_EQ(0, ServerCreate(ComponentType::COMPONENT_GETD_FILE, TestServerStartNoop, TestServerStopNoop, &attr)); | ||
| 402 | + | ||
| 403 | + MOCKER(AdxGetAttrByCommHandle).stubs().will(invoke(UtSessionAttrPerSession)); | ||
| 404 | + MOCKER(AdxDestroyCommHandle).stubs().will(invoke(UtCountDestroy)); | ||
| 405 | + MOCKER(LogFree).stubs().will(invoke(UtParkingLogFree)); | ||
| 406 | + | ||
| 407 | + CommHandle handleA; | ||
| 408 | + handleA.type = (OptType)COMM_HDC; | ||
| 409 | + handleA.session = (OptHandle)0x11111; | ||
| 410 | + handleA.timeout = 0; | ||
| 411 | + handleA.client = nullptr; | ||
| 412 | + handleA.comp = COMPONENT_GETD_FILE; | ||
| 413 | + EXPECT_EQ(0, ServerProcess(&handleA, nullptr, 0)); // returns, its monitor parks at thread entry | ||
| 414 | + | ||
| 415 | + // session B is published while monitor A has not executed its first instruction: | ||
| 416 | + // B's ServerProcess parks inside the join of monitor A right after publishing | ||
| 417 | + CommHandle handleB; | ||
| 418 | + handleB.type = (OptType)COMM_HDC; | ||
| 419 | + handleB.session = (OptHandle)0x22222; | ||
| 420 | + handleB.timeout = 0; | ||
| 421 | + handleB.client = nullptr; | ||
| 422 | + handleB.comp = COMPONENT_GETD_FILE; | ||
| 423 | + int32_t turnRet = -1; | ||
| 424 | + bool turnDone = false; | ||
| 425 | + std::thread turnThread([&] { | ||
| 426 | + turnRet = ServerProcess(&handleB, nullptr, 0); | ||
| 427 | + turnDone = true; | ||
| 428 | + }); | ||
| 429 | + bool published = false; | ||
| 430 | + for (int32_t i = 0; (i < 3000) && !published; i++) { | ||
| 431 | + published = (g_serverMgr[ComponentType::COMPONENT_GETD_FILE].current != nullptr) && | ||
| 432 | + (g_serverMgr[ComponentType::COMPONENT_GETD_FILE].current->comm->session == (OptHandle)0x22222); | ||
| 433 | + if (!published) { | ||
| 434 | + usleep(1000); | ||
| 435 | + } | ||
| 436 | + } | ||
| 437 | + ASSERT_TRUE(published); | ||
| 438 | + | ||
| 439 | + g_utInvalidSession = (OptHandle)0x11111; // session A is broken | ||
| 440 | + g_utLogFreeRelease = true; // monitor A finally starts: it must take node A | ||
| 441 | + | ||
| 442 | + bool finished = false; | ||
| 443 | + for (int32_t i = 0; (i < 5000) && !finished; i++) { | ||
| 444 | + finished = turnDone; | ||
| 445 | + if (!finished) { | ||
| 446 | + usleep(1000); // 5s deadline: join of A + creation and ack of B | ||
| 447 | + } | ||
| 448 | + } | ||
| 449 | + if (!finished) { | ||
| 450 | + ADD_FAILURE() << "ServerProcess of session B did not complete after monitor A started"; | ||
| 451 | + turnThread.detach(); // never join a deadlocked task: keep the failure observable | ||
| 452 | + return; | ||
| 453 | + } | ||
| 454 | + turnThread.join(); | ||
| 455 | + EXPECT_EQ(0, turnRet); | ||
| 456 | + | ||
| 457 | + // monitor A operated on node A: A's handle destroyed exactly once, A's node is gone; | ||
| 458 | + // monitor B holds node B with only its own lifetime reference | ||
| 459 | + const ServerMgr* mgr = &g_serverMgr[ComponentType::COMPONENT_GETD_FILE]; | ||
| 460 | + EXPECT_EQ(1, g_utDestroyCount); | ||
| 461 | + ASSERT_NE(nullptr, mgr->commList); | ||
| 462 | + EXPECT_EQ(nullptr, mgr->commList->next); // only B's generation left | ||
| 463 | + EXPECT_EQ((OptHandle)0x22222, mgr->commList->comm->session); | ||
| 464 | + EXPECT_EQ(1U, mgr->commList->refs); | ||
| 465 | + EXPECT_FALSE(mgr->commList->dying); | ||
| 466 | + EXPECT_EQ(mgr->commList, mgr->current); | ||
| 467 | + | ||
| 468 | + ServerRelease(ComponentType::COMPONENT_GETD_FILE); // B's monitor exits and destroys node B | ||
| 469 | + EXPECT_EQ(2, g_utDestroyCount); | ||
| 470 | + EXPECT_EQ(0U, mgr->linkedNum); | ||
| 471 | + EXPECT_EQ(nullptr, mgr->current); | ||
| 472 | + EXPECT_EQ(nullptr, mgr->commList); | ||
| 473 | + | ||
| 474 | + GlobalMockObject::reset(); | ||
| 475 | +} | ||


🟡 Medium Priority
变更行(server_mgr.c:137-141)新增:
SessionMonitorProcess进入线程后先ServerAcquireCommHandle(mgr, false),失败即return NULL。证据链:
ServerProcess在创建 monitor 前已执行g_serverMgr[type].linkedNum++(line 333),该槽位的释放责任被交给 monitor 线程(line 162mgr->linkedNum--),line 348-349 的注释也明确「the monitor thread owns the comm handle now ... decrements linkedNum on exit」。mgr->linkedNum--,也没有调用mgr->stop()/ServerWaitStop,也没有清理mgr->handle。失败模式:一旦该路径被触发(monitor 线程启动时
mgr->handle已被清空,例如前一任 monitor 的摘除与本次handle=赋值交错、或ServerMgrNodeReset在未 join 场景下清空 handle),则:①linkedNum永久 +1,而linkedNum >= maxNum是接入的第一道闸门(file_monitor 的MAX_CONCURRENT_NUM为 1),后续所有连接都会被回 OVERLOAD 拒绝;②g_serverMgr[type].handle指向的 comm 句柄无人销毁,HDC 会话句柄泄漏;③组件注册的stop()不再执行,服务状态与句柄生命周期脱钩。PR 描述称该路径「在现有调用序列下不可达」,但代码本身并未建立该不变量,属于把正确性寄托在调用时序上的隐患。建议:在 acquire 失败分支内持锁回滚本线程负责的槽位与 monitor 状态(或在 ServerProcess 侧不再把槽位所有权交给线程),避免 linkedNum 永久泄漏、handle 悬挂以及 stop() 不执行。