{"record":{"id":"c71fe7428b86dd79","repo":"sebastianbergmann/phpunit","slug":"getrusage-failed","errorCode":null,"errorMessage":"getrusage() failed.","messagePattern":"getrusage\\(\\) failed\\.","errorType":"exception","errorClass":"PHPUnit\\Event\\RuntimeException","httpStatus":null,"severity":"error","filePath":"src/Event/Value/Telemetry/SystemCpuTimeMeter.php","lineNumber":52,"sourceCode":"     */\n    public function systemCpuTime(): CpuTime\n    {\n        return $this->cpuTime('ru_stime.tv_sec', 'ru_stime.tv_usec');\n    }\n\n    /**\n     * @param 'ru_stime.tv_sec'|'ru_utime.tv_sec'   $secondsKey\n     * @param 'ru_stime.tv_usec'|'ru_utime.tv_usec' $microsecondsKey\n     *\n     * @throws RuntimeException\n     */\n    private function cpuTime(string $secondsKey, string $microsecondsKey): CpuTime\n    {\n        $usage = getrusage();\n\n        if ($usage === false) {\n            // @codeCoverageIgnoreStart\n            throw new RuntimeException('getrusage() failed.');\n            // @codeCoverageIgnoreEnd\n        }\n\n        if (!isset($usage[$secondsKey]) || !isset($usage[$microsecondsKey])) {\n            // @codeCoverageIgnoreStart\n            throw new RuntimeException(\n                sprintf(\n                    'getrusage() did not return the expected keys \"%s\" and \"%s\".',\n                    $secondsKey,\n                    $microsecondsKey,\n                ),\n            );\n            // @codeCoverageIgnoreEnd\n        }\n\n        $seconds      = $usage[$secondsKey];\n        $microseconds = $usage[$microsecondsKey];\n","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/sebastianbergmann/phpunit/blob/f123cdb2a2d49f15025794166cfed8bda8627dd2/src/Event/Value/Telemetry/SystemCpuTimeMeter.php#L34-L70","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\n$cpu = $telemetry->cpuTimeMeter()->userCpuTime(); // may throw RuntimeException\n\n// after\ntry {\n    $cpu = $telemetry->cpuTimeMeter()->userCpuTime();\n} catch (PHPUnit\\Event\\RuntimeException $e) {\n    $cpu = null; // CPU telemetry unavailable in this environment; stopwatch data still usable\n}","handlingStrategy":"fallback","validationCode":"if (!function_exists('getrusage')) {\n    // CPU-time telemetry impossible in this environment; use stopwatch-only reporting\n}","typeGuard":null,"tryCatchPattern":"try {\n    $cpu = $meter->userCpuTime();\n} catch (PHPUnit\\Event\\RuntimeException $e) {\n    $cpu = null; // fall back to wall-clock-only telemetry and continue\n}","preventionTips":["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."],"tags":["phpunit","telemetry","getrusage","environment","php"],"backgroundTag":"system-call-failed","analyzedSha":"f123cdb2a2d49f15025794166cfed8bda8627dd2","analyzedAt":"2026-08-23T01:20:58.058Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}