Skip to content

Commit ffd27bd

Browse files
morrisonleviarnaud-lb
authored andcommittedMay 20, 2022
Stop closing stderr and stdout streams (#8570)
Extensions may (and do) write to stderr in mshutdown and similar. In the best case, with the stderr stream closed, it's just swallowed. However, some libraries will do things like try to detect color, and these will outright fail and cause an error path to be taken.
1 parent c88dc44 commit ffd27bd

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed
 

‎NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.1.7
44

5+
- CLI:
6+
. Fixed bug GH-8575 (CLI closes standard streams too early). (Levi Morrison)
7+
58
- FPM:
69
. Fixed ACL build check on MacOS. (David Carlier)
710
. Fixed bug #72185: php-fpm writes empty fcgi record causing nginx 502.

‎ext/zend_test/php_test.h

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ ZEND_BEGIN_MODULE_GLOBALS(zend_test)
5151
HashTable global_weakmap;
5252
int replace_zend_execute_ex;
5353
int register_passes;
54+
bool print_stderr_mshutdown;
5455
zend_test_fiber *active_fiber;
5556
ZEND_END_MODULE_GLOBALS(zend_test)
5657

‎ext/zend_test/tests/gh8575.phpt

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
--TEST--
22
CLI: stderr is available in mshutdown
33
--SKIPIF--
4-
<?php
5-
if (!extension_loaded('zend-test')) die('skip zend-test extension required');
6-
if (php_sapi_name() != "cli") die('skip cli test only');
7-
?>
4+
<?php if (php_sapi_name() != "cli") die('skip cli test only'); ?>
5+
--EXTENSIONS--
6+
zend_test
87
--INI--
98
zend_test.print_stderr_mshutdown=1
109
--FILE--

‎sapi/cli/php_cli.c

+8-6
Original file line numberDiff line numberDiff line change
@@ -542,9 +542,11 @@ static void cli_register_file_handles(bool no_close) /* {{{ */
542542
* extensions which write to stderr or company during mshutdown/gshutdown
543543
* won't have the expected functionality.
544544
*/
545-
if (s_in) s_in->flags |= PHP_STREAM_FLAG_NO_CLOSE;
546-
if (s_out) s_out->flags |= PHP_STREAM_FLAG_NO_CLOSE;
547-
if (s_err) s_err->flags |= PHP_STREAM_FLAG_NO_CLOSE;
545+
if (no_close) {
546+
if (s_in) s_in->flags |= PHP_STREAM_FLAG_NO_CLOSE;
547+
if (s_out) s_out->flags |= PHP_STREAM_FLAG_NO_CLOSE;
548+
if (s_err) s_err->flags |= PHP_STREAM_FLAG_NO_CLOSE;
549+
}
548550

549551
if (s_in==NULL || s_out==NULL || s_err==NULL) {
550552
if (s_in) php_stream_close(s_in);
@@ -958,7 +960,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
958960
switch (behavior) {
959961
case PHP_MODE_STANDARD:
960962
if (script_file) {
961-
cli_register_file_handles(/* no_close */ PHP_DEBUG || num_repeats > 1);
963+
cli_register_file_handles(/* no_close */ true);
962964
}
963965

964966
if (interactive) {
@@ -993,7 +995,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
993995
}
994996
break;
995997
case PHP_MODE_CLI_DIRECT:
996-
cli_register_file_handles(/* no_close */ PHP_DEBUG || num_repeats > 1);
998+
cli_register_file_handles(/* no_close */ true);
997999
zend_eval_string_ex(exec_direct, NULL, "Command line code", 1);
9981000
break;
9991001

@@ -1008,7 +1010,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
10081010
file_handle.filename = NULL;
10091011
}
10101012

1011-
cli_register_file_handles(/* no_close */ PHP_DEBUG || num_repeats > 1);
1013+
cli_register_file_handles(/* no_close */ true);
10121014

10131015
if (exec_begin) {
10141016
zend_eval_string_ex(exec_begin, NULL, "Command line begin code", 1);

0 commit comments

Comments
 (0)