sebastianbergmann/phpunit · error · PHPUnit\Util\PHP\PhpProcessException

Unable to create temporary file for redirected output

Error message

Unable to create temporary file for redirected output

What it means

When a job is configured to redirect errors (merged stdout/stderr into one stream), JobRunner::startProcess() allocates that stream with tmpfile(). If tmpfile() fails it throws PhpProcessException('Unable to create temporary file for redirected output'). Like the other JobRunner failures this indicates the system temp directory cannot be used.

Source

Thrown at src/Util/PHP/JobRunner.php:196

                if (is_array($value)) {
                    continue;
                }

                $environmentVariables[$key] = $value;
            }

            $environmentVariables = array_merge($environmentVariables, $job->environmentVariables());
        }

        $mergedOutputStream = null;

        if ($job->redirectErrors()) {
            $mergedOutputStream = tmpfile();

            if ($mergedOutputStream === false) {
                // @codeCoverageIgnoreStart
                throw new PhpProcessException('Unable to create temporary file for redirected output');
                // @codeCoverageIgnoreEnd
            }

            $pipeSpec = [
                0 => ['pipe', 'r'],
                1 => $mergedOutputStream,
                2 => $mergedOutputStream,
            ];
        } else {
            $pipeSpec = [
                0 => ['pipe', 'r'],
                1 => ['pipe', 'w'],
                2 => ['pipe', 'w'],
            ];
        }

        $process = proc_open(
            $this->buildCommand($job, $temporaryFile),

View on GitHub (pinned to f123cdb2a2)

Solutions

  1. Ensure /tmp (or TMPDIR) exists and is writable by the test user: `mkdir -p /tmp && chmod 1777 /tmp`
  2. Mount a writable tmpfs at /tmp in the container definition
  3. Free disk space on the volume hosting the temp directory
  4. Check open_basedir includes the temp directory if it is configured

Example fix

# before
$ docker run --read-only my-ci vendor/bin/phpunit
# PhpProcessException: Unable to create temporary file for redirected output

# after
$ docker run --read-only --tmpfs /tmp:rw,size=64m my-ci vendor/bin/phpunit
Defensive patterns

Strategy: try-catch

Validate before calling

if (!is_writable(sys_get_temp_dir()) || disk_free_space(sys_get_temp_dir()) === false) {
    throw new RuntimeException('Temp directory unusable for redirected output');
}

Try / catch

use PHPUnit\Util\PHP\PhpProcessException;

try {
    $runner->start($job); // job configured with redirectErrors()
} catch (PhpProcessException $e) {
    // disable error redirection for this run or fix the temp dir
    error_log('Could not redirect child output: ' . $e->getMessage());
}

Prevention

When it happens

Trigger: Running process-isolated tests that redirect errors while the temp directory is unwritable, full, or excluded by open_basedir, so tmpfile() cannot create its anonymous temporary file.

Common situations: Docker images with read-only root filesystems and no /tmp tmpfs; CI runners with full disks; minimal VMs where /tmp is missing entirely or mode 000.

Related errors


AI-assisted analysis of sebastianbergmann/phpunit@f123cdb2a2 (2026-08-23). Data as JSON: /api/errors/2defa12c7f19825b. Report an issue: GitHub.