Skip to content

Commit

Permalink
Fixed bug: onWorkerStop cannot be triggered.
Browse files Browse the repository at this point in the history
  • Loading branch information
twomiao committed Oct 4, 2023
1 parent c9e4100 commit 9fcf04b
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use function stream_socket_accept;
use function stream_socket_recvfrom;
use function substr;
use function array_walk;

/**
* Worker class
Expand Down Expand Up @@ -1165,7 +1166,7 @@ protected static function reinstallSignal(): void
}
$signals = [SIGINT, SIGTERM, SIGHUP, SIGTSTP, SIGQUIT, SIGUSR1, SIGUSR2, SIGIOT, SIGIO];
foreach ($signals as $signal) {
pcntl_signal($signal, SIG_IGN, false);
// Rewrite master process signal.
static::$globalEvent->onSignal($signal, [static::class, 'signalHandler']);
}
}
Expand Down Expand Up @@ -1801,7 +1802,7 @@ protected static function reload(): void
if (static::$masterPid === posix_getpid()) {
$sig = static::getGracefulStop() ? SIGUSR2 : SIGUSR1;
// Set reloading state.
if (static::$status !== static::STATUS_RELOADING && static::$status !== static::STATUS_SHUTDOWN) {
if (static::$status === static::STATUS_RUNNING) {
static::log("Workerman[" . basename(static::$startFile) . "] reloading");
static::$status = static::STATUS_RELOADING;

Expand Down Expand Up @@ -1911,17 +1912,18 @@ public static function stopAll(int $code = 0, mixed $log = ''): void
else {
// Execute exit.
$workers = array_reverse(static::$workers);
foreach ($workers as $worker) {
if (!$worker->stopping) {
$worker->stop();
$worker->stopping = true;
}
}
array_walk($workers, static fn (Worker $worker) => $worker->stop());

if (!static::getGracefulStop() || ConnectionInterface::$statistics['connection_count'] <= 0) {
static::$workers = [];
static::$globalEvent?->stop();

exit($code);
try {
// Ignore Swoole ExitException: Swoole exit.
exit($code);
} catch(\Exception $e) {

Check failure on line 1924 in src/Worker.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 - prefer-lowest - ubuntu-latest

Dead catch - Exception is never thrown in the try block.

Check failure on line 1924 in src/Worker.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 - prefer-stable - ubuntu-latest

Dead catch - Exception is never thrown in the try block.

Check failure on line 1924 in src/Worker.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 - prefer-lowest - ubuntu-latest

Dead catch - Exception is never thrown in the try block.

Check failure on line 1924 in src/Worker.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 - prefer-stable - ubuntu-latest

Dead catch - Exception is never thrown in the try block.

Check failure on line 1924 in src/Worker.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 - prefer-lowest - ubuntu-latest

Dead catch - Exception is never thrown in the try block.

Check failure on line 1924 in src/Worker.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 - prefer-stable - ubuntu-latest

Dead catch - Exception is never thrown in the try block.

Check failure on line 1924 in src/Worker.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 - prefer-lowest - macos-latest

Dead catch - Exception is never thrown in the try block.

Check failure on line 1924 in src/Worker.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 - prefer-stable - macos-latest

Dead catch - Exception is never thrown in the try block.

Check failure on line 1924 in src/Worker.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 - prefer-lowest - macos-latest

Dead catch - Exception is never thrown in the try block.

Check failure on line 1924 in src/Worker.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 - prefer-stable - macos-latest

Dead catch - Exception is never thrown in the try block.

Check failure on line 1924 in src/Worker.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 - prefer-lowest - macos-latest

Dead catch - Exception is never thrown in the try block.

Check failure on line 1924 in src/Worker.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 - prefer-stable - macos-latest

Dead catch - Exception is never thrown in the try block.

Check failure on line 1924 in src/Worker.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 - prefer-lowest - windows-latest

Dead catch - Exception is never thrown in the try block.

Check failure on line 1924 in src/Worker.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 - prefer-stable - windows-latest

Dead catch - Exception is never thrown in the try block.

Check failure on line 1924 in src/Worker.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 - prefer-lowest - windows-latest

Dead catch - Exception is never thrown in the try block.

Check failure on line 1924 in src/Worker.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 - prefer-stable - windows-latest

Dead catch - Exception is never thrown in the try block.

Check failure on line 1924 in src/Worker.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 - prefer-lowest - windows-latest

Dead catch - Exception is never thrown in the try block.

Check failure on line 1924 in src/Worker.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 - prefer-stable - windows-latest

Dead catch - Exception is never thrown in the try block.

}
}
}
}
Expand Down Expand Up @@ -2457,6 +2459,9 @@ public function run(): void
*/
public function stop(): void
{
if ($this->stopping === true) {
return;
}
// Try to emit onWorkerStop callback.
if ($this->onWorkerStop) {
try {
Expand All @@ -2481,6 +2486,7 @@ public function stop(): void
}
// Clear callback.
$this->onMessage = $this->onClose = $this->onError = $this->onBufferDrain = $this->onBufferFull = null;
$this->stopping = true;
}

/**
Expand Down

0 comments on commit 9fcf04b

Please sign in to comment.