1--TEST--
2Waiting on SIGCHLD with a pcntl_wait() loop
3--EXTENSIONS--
4pcntl
5--SKIPIF--
6<?php
7if (PHP_OS != 'Linux') {
8    die('skip Linux only');
9}
10?>
11--FILE--
12<?php
13$processes = [];
14
15pcntl_async_signals(true);
16pcntl_signal(SIGCHLD, function($sig, $info) use (&$processes) {
17    while (($pid = pcntl_wait($status, WUNTRACED | WNOHANG)) > 0) {
18        echo "SIGCHLD\n";
19        unset($processes[$pid]);
20    }
21}, false);
22
23for ($i = 0; $i <= 5; $i++) {
24    // Sleeping ensures we get to add the process to the list before the signal is invoked.
25    $process = proc_open('sleep 1', [], $pipes);
26    $pid = proc_get_status($process)['pid'];
27    $processes[$pid] = $process;
28}
29
30$iters = 50;
31while (!empty($processes) && $iters > 0) {
32    usleep(100_000);
33    $iters--;
34}
35
36var_dump(empty($processes));
37?>
38--EXPECT--
39SIGCHLD
40SIGCHLD
41SIGCHLD
42SIGCHLD
43SIGCHLD
44SIGCHLD
45bool(true)
46