Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug: onWorkerStop cannot be triggered. #959

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
}
$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 @@
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 @@
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 stop(): void
{
if ($this->stopping === true) {
return;
}
// Try to emit onWorkerStop callback.
if ($this->onWorkerStop) {
try {
Expand All @@ -2481,6 +2486,7 @@
}
// Clear callback.
$this->onMessage = $this->onClose = $this->onError = $this->onBufferDrain = $this->onBufferFull = null;
$this->stopping = true;
}

/**
Expand Down
Loading