Skip to content

Commit 5e6469e

Browse files
stklcodebackportbot[bot]
authored andcommitted
fix(jobs): resolve portability issues with background job cleanup
The previous implementation executes "ps" with flags -p and -o to check whether a job process was still running. This approach has portability issues regarding e.g. BusyBox ps (Alpine Linux) where `ps -p` fails or BSD/macOS where `ps -o` fails. Replace with `posix_kill($pid, 0)`, the standard POSIX way to probe process existence without delivering a signal. EPERM is treated as "process exists but not owned by current user". This should be available on all supported platforms. Fixes: 60ce92a Suggested-by: Michele Marcionelli Signed-off-by: Stefan Kalscheuer <stefan@stklcode.de>
1 parent 58c4667 commit 5e6469e

1 file changed

Lines changed: 3 additions & 5 deletions

File tree

core/BackgroundJobs/CleanupBackgroundJobsJob.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,9 @@ private function reapCrashedJobs(): void {
4747
if ($job->serverId !== $currentServerId) {
4848
continue;
4949
}
50-
$output = [];
51-
$result = 0;
52-
exec('ps -p ' . escapeshellarg((string)$job->pid) . ' -o cmd', $output, $result);
53-
if (count($output) === 1 && current($output) === 'CMD' && $result === 1) {
54-
// Process doesn't exists anymore
50+
$processExists = posix_kill($job->pid, 0) || posix_get_last_error() === 1 /* EPERM */;
51+
if (!$processExists) {
52+
// Process doesn't exist anymore
5553
$maxDuration = (new DateTimeImmutable())->diff($job->startedAt);
5654
$maxDuration
5755
= ($maxDuration->days * 24 * 60 * 60 * 1000)

0 commit comments

Comments
 (0)