sebastianbergmann/phpunit · error · PHPUnit\Event\RuntimeException
getrusage() failed.
Error message
getrusage() failed.
What it means
SystemCpuTimeMeter reads CPU time via PHP's native getrusage() inside its private cpuTime() helper (reached from userCpuTime()/systemCpuTime()). If the call returns false, a RuntimeException is thrown. Stock PHP builds essentially never return false here — the code is even marked @codeCoverageIgnore — so encountering it signals a non-standard environment (patched or shadowed PHP function) rather than an application bug.
Source
Thrown at src/Event/Value/Telemetry/SystemCpuTimeMeter.php:52
*/
public function systemCpuTime(): CpuTime
{
return $this->cpuTime('ru_stime.tv_sec', 'ru_stime.tv_usec');
}
/**
* @param 'ru_stime.tv_sec'|'ru_utime.tv_sec' $secondsKey
* @param 'ru_stime.tv_usec'|'ru_utime.tv_usec' $microsecondsKey
*
* @throws RuntimeException
*/
private function cpuTime(string $secondsKey, string $microsecondsKey): CpuTime
{
$usage = getrusage();
if ($usage === false) {
// @codeCoverageIgnoreStart
throw new RuntimeException('getrusage() failed.');
// @codeCoverageIgnoreEnd
}
if (!isset($usage[$secondsKey]) || !isset($usage[$microsecondsKey])) {
// @codeCoverageIgnoreStart
throw new RuntimeException(
sprintf(
'getrusage() did not return the expected keys "%s" and "%s".',
$secondsKey,
$microsecondsKey,
),
);
// @codeCoverageIgnoreEnd
}
$seconds = $usage[$secondsKey];
$microseconds = $usage[$microsecondsKey];
View on GitHub (pinned to f123cdb2a2)
Solutions
- Reproduce outside the sandbox (bare 'php -r "var_dump(getrusage());"') to confirm the environment is the cause; then loosen the seccomp/container profile or use an unmodified PHP build.
- Remove uopz/runkit overrides or namespace-shadowed getrusage() stubs from the test bootstrap.
- If you drive the meter in your own runner, wrap userCpuTime()/systemCpuTime() in a try/catch and degrade gracefully to stopwatch-only telemetry.
- If it reproduces on a stock PHP build, report it to PHPUnit with 'php -i' output and the sandbox details.
Example fix
// before
$cpu = $telemetry->cpuTimeMeter()->userCpuTime(); // may throw RuntimeException
// after
try {
$cpu = $telemetry->cpuTimeMeter()->userCpuTime();
} catch (PHPUnit\Event\RuntimeException $e) {
$cpu = null; // CPU telemetry unavailable in this environment; stopwatch data still usable
} Defensive patterns
Strategy: fallback
Validate before calling
if (!function_exists('getrusage')) {
// CPU-time telemetry impossible in this environment; use stopwatch-only reporting
} Try / catch
try {
$cpu = $meter->userCpuTime();
} catch (PHPUnit\Event\RuntimeException $e) {
$cpu = null; // fall back to wall-clock-only telemetry and continue
} Prevention
- Run PHPUnit under an unmodified PHP build in CI; document exotic sandboxes as unsupported for CPU telemetry.
- Avoid uopz/runkit overrides and namespace-shadowed getrusage() stubs in bootstraps.
- Smoke-test getrusage() availability when building custom runners on constrained platforms.
When it happens
Trigger: Running under a PHP build where getrusage() is disabled or stubbed; test or tooling environments where uopz/runkit7 intercept native functions; runtimes or seccomp sandboxes that block the underlying getrusage(2) syscall so PHP returns false.
Common situations: Exotic container images or hardened CI sandboxes; heavily patched PHP distributions; unit tests that mock global functions via namespace shadowing (use function ...\getrusage) accidentally affecting the telemetry path.
Related errors
- getrusage() did not return the expected keys "%s" and "%s".
- getrusage() returned non-integer values for "%s" and/or "%s"
- Value for %s must not be negative.
- Value for nanoseconds must not be greater than 999999999.
- Value for %s must not be negative.
AI-assisted analysis of sebastianbergmann/phpunit@f123cdb2a2 (2026-08-23).
Data as JSON: /api/errors/c71fe7428b86dd79.
Report an issue: GitHub.