php / php/php-src

PHP incorrectly closes file handles when used to replace STDIN/STDOUT/STDERR

Open
#11,399 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Category: Streams Feature SAPI: cli
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

The following code:

<?php
  fclose(STDIN);
  fclose(STDOUT);
  fclose(STDERR);
  fopen('/dev/null', 'r');
  fopen('/dev/null', 'w');
  fopen('/dev/null', 'w');

  exec("dig +noall +answer -t ns -q 'www.google.com'", $out, $ret);
  file_put_contents('ret.txt', $ret . "\n");

Resulted in exec failing with the return code 134 as seen in ret.txt

PHP's garbage collection seems to have not taken into account that the file handles opened are now in use as stdin/stdout/stderr. Obviously this can be worked around by storing the result of fopen into some global vars, but IMO this is a bug.

PHP Version

PHP 8.2.6

Operating System

Debian 11

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the PHP 8.2.6 example on Debian 11, focusing on the replacement of STDIN, STDOUT, and STDERR and the later garbage collection. Trace how those standard streams are handled before exec runs. Done means the command no longer fails with return code 134 when the replacement handles are not retained in global variables.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.