Skip to content
Merged
Show file tree
Hide file tree
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
18 changes: 14 additions & 4 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,20 @@ public static function cpuCount(): int
return 1;
}

return \strtolower(\PHP_OS) === 'darwin'
? (int) shell_exec('sysctl -n machdep.cpu.core_count')
: (int) shell_exec('nproc')
;
$command = \strtolower(\PHP_OS) === 'darwin'
? 'sysctl -n machdep.cpu.core_count'
: 'nproc';

$result = shell_exec($command);

// shell_exec returns null (no output) or false (command failed)
if (!\is_string($result)) {
return 1;
}

$count = (int) \trim($result);

return $count > 0 ? $count : 1;
}

public static function isWindows(): bool
Expand Down
9 changes: 9 additions & 0 deletions tests/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ public function testCpuCountReturnsPositiveInteger(): void
$this->assertGreaterThanOrEqual(1, $cpuCount);
}

public function testCpuCountNeverReturnsZero(): void
{
// Regression test for #150: Utils::cpuCount() must NEVER return 0,
// even when shell_exec('nproc') returns null (command not available)
// or produces empty/unexpected output. Returning 0 would cause
// downstream issues: zero workers spawned in ServerWorker.
$this->assertNotSame(0, Utils::cpuCount());
}

/**
* @requires OS Windows
*/
Expand Down