File: php_cli_server_pdeathsig.phpt

package info (click to toggle)
php8.4 8.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,108 kB
  • sloc: ansic: 1,060,628; php: 35,345; sh: 11,866; cpp: 7,201; pascal: 4,913; javascript: 3,091; asm: 2,810; yacc: 2,411; makefile: 689; xml: 446; python: 301; awk: 148
file content (56 lines) | stat: -rw-r--r-- 1,471 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
--TEST--
Killing server should terminate all worker processes
--ENV--
PHP_CLI_SERVER_WORKERS=2
--SKIPIF--
<?php
include "skipif.inc";
if (!(str_contains(PHP_OS, 'Linux') || str_contains(PHP_OS, 'FreeBSD'))) {
    die('skip PDEATHSIG is only supported on Linux and FreeBSD');
}
if (@file_exists('/.dockerenv')) die("skip Broken in Docker");
?>
--FILE--
<?php

function split_words(?string $lines): array {
    return preg_split('(\s)', trim($lines ?? ''), flags: PREG_SPLIT_NO_EMPTY);
}

function find_workers_by_ppid(string $ppid) {
    return split_words(shell_exec('pgrep -P ' . $ppid));
}

function find_workers_by_pids(array $pids) {
    return split_words(shell_exec('ps -o pid= -p ' . join(',', $pids)));
}

include "php_cli_server.inc";
$cliServerInfo = php_cli_server_start('');

$master = proc_get_status($cliServerInfo->processHandle)['pid'];
$workersBefore = find_workers_by_ppid($master);
if (count($workersBefore) === 0) {
    throw new \Exception('Could not find worker pids');
}

proc_terminate($cliServerInfo->processHandle, 9); // SIGKILL

$try = 1;
$max_tries = 20;
while (true) {
    $workersAfter = find_workers_by_pids($workersBefore);
    if (count($workersAfter) === 0) {
        break;
    }
    if ($try >= $max_tries) {
        throw new \Exception('Workers were not properly terminated. Before: ' . join(', ', $workersBefore) . ', after: ' . join(', ', $workersAfter));
    }
    $try++;
    usleep(100_000);
}

echo 'Done';
?>
--EXPECT--
Done