Skip to content

Commit 6604190

Browse files
committed
Add a compatibility "cached" key to the array returned by proc_get_status()
Users who previously would've relied on proc_get_status() failing after calling it more than one time can now check the "cached" key to see whether the result was cached. If it is cached, it would've previously returned a failure in the form of a -1 exit code and the status booleans all being false.
1 parent 9de21ba commit 6604190

File tree

5 files changed

+27
-8
lines changed

5 files changed

+27
-8
lines changed

UPGRADING

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ PHP 8.3 UPGRADE NOTES
2727
. Class constants can now be accessed dynamically using the C::{$name} syntax.
2828
RFC: https://wiki.php.net/rfc/dynamic_class_constant_fetch
2929
. Executing proc_get_status() multiple times will now always return the right
30-
value. Previously, only the first call of the function returned the right
31-
value. Executing proc_close() after proc_get_status() will now also return
32-
the right exit code. Previously this would return -1.
30+
value on posix systems. Previously, only the first call of the function
31+
returned the right value. Executing proc_close() after proc_get_status() will
32+
now also return the right exit code. Previously this would return -1.
33+
Internally, this works by caching the result on posix systems. If you want
34+
the old behaviour, you can check the "cached" key in the array returned by
35+
proc_get_status() to check whether the result was cached.
3336

3437
========================================
3538
2. New Features

ext/standard/proc_open.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,10 @@ PHP_FUNCTION(proc_get_status)
404404
running = wstatus == STILL_ACTIVE;
405405
exitcode = running ? -1 : wstatus;
406406

407+
/* The status is always available on Windows and will always read the same,
408+
* even if the child has already exited. This is because the result stays available
409+
* until the child handle is closed. Hence no caching is used on Windows. */
410+
add_assoc_bool(return_value, "cached", false);
407411
#elif HAVE_SYS_WAIT_H
408412
wait_pid = waitpid_cached(proc, &wstatus, WNOHANG|WUNTRACED);
409413

@@ -426,6 +430,8 @@ PHP_FUNCTION(proc_get_status)
426430
* looking for either does not exist or is not a child of this process */
427431
running = 0;
428432
}
433+
434+
add_assoc_bool(return_value, "cached", proc->has_stored_exit_wait_status);
429435
#endif
430436

431437
add_assoc_bool(return_value, "running", running);

ext/standard/tests/general_functions/bug39322.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ echo "Done!\n";
2424

2525
?>
2626
--EXPECTF--
27-
array(8) {
27+
array(9) {
2828
["command"]=>
2929
string(14) "/bin/sleep 120"
3030
["pid"]=>
3131
int(%d)
32+
["cached"]=>
33+
bool(false)
3234
["running"]=>
3335
bool(false)
3436
["signaled"]=>

ext/standard/tests/general_functions/gh10239_2.phpt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ var_dump(proc_get_status($p));
1313
var_dump(proc_get_status($p));
1414
?>
1515
--EXPECTF--
16-
array(8) {
16+
array(9) {
1717
["command"]=>
1818
string(5) "false"
1919
["pid"]=>
2020
int(%d)
21+
["cached"]=>
22+
bool(true)
2123
["running"]=>
2224
bool(false)
2325
["signaled"]=>
@@ -31,11 +33,13 @@ array(8) {
3133
["stopsig"]=>
3234
int(0)
3335
}
34-
array(8) {
36+
array(9) {
3537
["command"]=>
3638
string(5) "false"
3739
["pid"]=>
3840
int(%d)
41+
["cached"]=>
42+
bool(true)
3943
["running"]=>
4044
bool(false)
4145
["signaled"]=>

ext/standard/tests/general_functions/proc_open02.phpt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ echo "Done!\n";
3232
?>
3333
--EXPECTF--
3434
bool(true)
35-
array(8) {
35+
array(9) {
3636
["command"]=>
3737
string(10) "/bin/sleep"
3838
["pid"]=>
3939
int(%d)
40+
["cached"]=>
41+
bool(false)
4042
["running"]=>
4143
bool(true)
4244
["signaled"]=>
@@ -51,11 +53,13 @@ array(8) {
5153
int(0)
5254
}
5355
bool(true)
54-
array(8) {
56+
array(9) {
5557
["command"]=>
5658
string(10) "/bin/sleep"
5759
["pid"]=>
5860
int(%d)
61+
["cached"]=>
62+
bool(false)
5963
["running"]=>
6064
bool(false)
6165
["signaled"]=>

0 commit comments

Comments
 (0)