This commit is contained in:
2026-07-08 11:39:27 +08:00
parent 1f443958e8
commit 27ffa05515
6 changed files with 221 additions and 30 deletions
+76 -3
View File
@@ -49,9 +49,7 @@ trait TraitServer
$handling = true;
try {
$pidFile = function_exists('storage') ? storage('.swoole.pid') : '';
$masterPid = is_file($pidFile) ? (int)trim((string)file_get_contents($pidFile)) : 0;
if ($masterPid > 1 && $masterPid !== getmypid()) {
if (!$this->shouldHandleSigint()) {
return;
}
@@ -62,6 +60,81 @@ trait TraitServer
}
}
private function shouldHandleSigint(): bool
{
$currentPid = getmypid();
if ($this->isMasterProcess($currentPid)) {
return true;
}
$masterPid = $this->getVerifiedPidFileMasterPid();
return $masterPid !== null && $masterPid === $currentPid;
}
private function getVerifiedPidFileMasterPid(): ?int
{
$pidFile = function_exists('storage') ? storage('.swoole.pid') : '';
if (!is_file($pidFile)) {
return null;
}
$pid = (int)trim((string)file_get_contents($pidFile));
if ($pid <= 1 || !$this->isProcessAlive($pid) || !$this->isSameAppProcess($pid) || !$this->isMasterProcess($pid)) {
return null;
}
return $pid;
}
private function isMasterProcess(int $pid): bool
{
$command = $this->getProcessCommand($pid);
if ($command === '') {
return false;
}
$prefix = '[' . config('site.id', 'system-service') . '].Master[';
return str_contains($command, $prefix);
}
private function getProcessCommand(int $pid): string
{
$command = @file_get_contents('/proc/' . $pid . '/cmdline');
if (!is_string($command) || $command === '') {
return '';
}
return str_replace("\0", ' ', $command);
}
private function isSameAppProcess(int $pid): bool
{
$cwd = @readlink('/proc/' . $pid . '/cwd');
if ($cwd === false || $cwd === '') {
return false;
}
return $this->normalizePath($cwd) === $this->normalizePath(APP_PATH);
}
private function normalizePath(string $path): string
{
$real = realpath($path) ?: $path;
return rtrim(str_replace('\\', '/', $real), '/');
}
private function isProcessAlive(int $pid): bool
{
return $pid > 1 && @Process::kill($pid, 0);
}
/**
* @param $signal
* @param $callback