已开启
bbox fix #1172
wenxingqi创建于 9 天前
bbox fix #1172
已开启
共 1 个文件变更+28-14
| @@ -429,6 +429,26 @@ static const char* GetSigName(const siginfo_t* info) | |||
| 429 | } | 429 | } |
| 430 | } | 430 | } |
| 431 | 431 | ||
| 432 | +static bool WaitChildExit(pid_t childPid) | ||
| 433 | +{ | ||
| 434 | + struct timespec deadline; | ||
| 435 | + clock_gettime(CLOCK_MONOTONIC, &deadline); | ||
| 436 | + deadline.tv_sec += WAIT_TIMEOUT_SECONDS; | ||
| 437 | + | ||
| 438 | + pid_t wpid { 0 }; | ||
| 439 | + while ((wpid = waitpid(childPid, nullptr, WNOHANG)) == 0) { | ||
| 440 | + struct timespec now; | ||
| 441 | + clock_gettime(CLOCK_MONOTONIC, &now); | ||
| 442 | + if ((now.tv_sec == deadline.tv_sec && now.tv_nsec >= deadline.tv_nsec) || (now.tv_sec > deadline.tv_sec)) { | ||
| 443 | + break; | ||
| 444 | + } | ||
| 445 | + struct timespec sleepTime = {0, WAIT_PID_SLEEP_MS * 1000000}; | ||
| 446 | + nanosleep(&sleepTime, nullptr); | ||
| 447 | + } | ||
| 448 | + | ||
| 449 | + return wpid != 0; | ||
| 450 | +} | ||
| 451 | + | ||
| 432 | static void HandleChildProcess() | 452 | static void HandleChildProcess() |
| 433 | { | 453 | { |
| 434 | BBoxDeInit(); | 454 | BBoxDeInit(); |
| @@ -445,22 +465,16 @@ static void HandleChildProcess() | |||
| 445 | 465 | ||
| 446 | _exit(0); | 466 | _exit(0); |
| 447 | } else if (childPid > 0) { | 467 | } else if (childPid > 0) { |
| 448 | - pid_t wpid; | 468 | + if (WaitChildExit(childPid)) { |
| 449 | - struct timespec deadline; | 469 | + return; |
| 450 | - clock_gettime(CLOCK_MONOTONIC, &deadline); | ||
| 451 | - deadline.tv_sec += WAIT_TIMEOUT_SECONDS; | ||
| 452 | - while ((wpid = waitpid(childPid, nullptr, WNOHANG)) == 0) { | ||
| 453 | - struct timespec now; | ||
| 454 | - clock_gettime(CLOCK_MONOTONIC, &now); | ||
| 455 | - if ((now.tv_sec == deadline.tv_sec && now.tv_nsec >= deadline.tv_nsec) || (now.tv_sec > deadline.tv_sec)) { | ||
| 456 | - break; | ||
| 457 | - } | ||
| 458 | - struct timespec sleepTime = {0, WAIT_PID_SLEEP_MS * 1000000}; | ||
| 459 | - nanosleep(&sleepTime, nullptr); | ||
| 460 | } | 470 | } |
| 461 | - if (wpid == 0) { | 471 | + |
| 462 | - (void)kill(childPid, SIGKILL); | 472 | + int ret = kill(childPid, SIGKILL); |
| 473 | + if (ret != 0 && errno != ESRCH) { // ESRCH 表示进程已退出,可忽略 | ||
| 474 | + return; | ||
| 463 | } | 475 | } |
| 476 | + | ||
| 477 | + (void)WaitChildExit(childPid); | ||
| 464 | } | 478 | } |
| 465 | } | 479 | } |
| 466 | 480 | ||