Skip to content

stream_select() leaks a stream-error operation level on its FD_SETSIZE failure path #24506

Closed

Description

@azjezz

Description

Each stream_select() call that hits the PHP_SAFE_MAX_FD check (any fd >= FD_SETSIZE, 1024 on macOS and Linux) emits its "You MUST recompile PHP with a larger value of FD_SETSIZE" warning and returns false. It also leaves one operation level open.

After 1,000 such calls, every later stream operation in the process (fread, fwrite, ..etc) emits:

Warning: fread(): Stream error operation depth exceeded (1000), possible infinite recursion

The operation itself still succeeds. The warnings never stop until the process exits.

Example:

$keep = [];
while (true) { // open sockets until one has fd >= 1024
    $p = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, 0);
    $keep[] = $p;
    if (count($keep) * 2 > 1100) {
      break;
    }
}

[$hi] = end($keep);
[$a, $b] = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, 0);
$calls = (int) ($argv[1] ?? 1001);
for ($i = 0; $i < $calls; $i++) {
    $r = [$hi]; $w = $e = null;
    @stream_select($r, $w, $e, 0);
}

fwrite($b, 'x');
$last = null;
set_error_handler(function ($no, $str) use (&$last) {
  $last = $str;
  return true;
});

$data = fread($a, 1);
restore_error_handler();
printf("after %d over-limit stream_select() calls: fread()=%s warning=%s\n", $calls, var_export($data, true), $last ?? 'none');
ulimit -n 4096
php reproduce.php 2000 # 8.6.0RC3: warning=fread(): Stream error operation depth exceeded (1000)...
php reproduce.php 999  # no warning

On 8.5.4 the same script prints warning=none.

PHP Version

PHP 8.5.4 (cli) (built: Mar 31 2026 17:40:26) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.5.4, Copyright (c) Zend Technologies
    with Zend OPcache v8.5.4, Copyright (c), by Zend Technologies

Operating System

MacOS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions