Skip to content

Commit 1f6f768

Browse files
fix: release but don't free CLI streams when executing cli scripts (#1906)
* Bring upstream commit php/php-src@0a4a55f into cli_register_file_handles to release but not free stdout/in/err. Fixes being unable to log to stdout or error after using frankenphp.ExecutePHPCode * chore: clang-format --------- Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
1 parent a4596b7 commit 1f6f768

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

frankenphp.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,8 +1041,7 @@ static char **cli_argv;
10411041
* <johannes@php.net> Parts based on CGI SAPI Module by Rasmus Lerdorf, Stig
10421042
* Bakken and Zeev Suraski
10431043
*/
1044-
static void cli_register_file_handles(bool no_close) /* {{{ */
1045-
{
1044+
static void cli_register_file_handles(void) {
10461045
php_stream *s_in, *s_out, *s_err;
10471046
php_stream_context *sc_in = NULL, *sc_out = NULL, *sc_err = NULL;
10481047
zend_constant ic, oc, ec;
@@ -1051,6 +1050,17 @@ static void cli_register_file_handles(bool no_close) /* {{{ */
10511050
s_out = php_stream_open_wrapper_ex("php://stdout", "wb", 0, NULL, sc_out);
10521051
s_err = php_stream_open_wrapper_ex("php://stderr", "wb", 0, NULL, sc_err);
10531052

1053+
/* Release stream resources, but don't free the underlying handles. Othewrise,
1054+
* extensions which write to stderr or company during mshutdown/gshutdown
1055+
* won't have the expected functionality.
1056+
*/
1057+
if (s_in)
1058+
s_in->flags |= PHP_STREAM_FLAG_NO_RSCR_DTOR_CLOSE;
1059+
if (s_out)
1060+
s_out->flags |= PHP_STREAM_FLAG_NO_RSCR_DTOR_CLOSE;
1061+
if (s_err)
1062+
s_err->flags |= PHP_STREAM_FLAG_NO_RSCR_DTOR_CLOSE;
1063+
10541064
if (s_in == NULL || s_out == NULL || s_err == NULL) {
10551065
if (s_in)
10561066
php_stream_close(s_in);
@@ -1061,12 +1071,6 @@ static void cli_register_file_handles(bool no_close) /* {{{ */
10611071
return;
10621072
}
10631073

1064-
if (no_close) {
1065-
s_in->flags |= PHP_STREAM_FLAG_NO_CLOSE;
1066-
s_out->flags |= PHP_STREAM_FLAG_NO_CLOSE;
1067-
s_err->flags |= PHP_STREAM_FLAG_NO_CLOSE;
1068-
}
1069-
10701074
/*s_in_process = s_in;*/
10711075

10721076
php_stream_to_zval(s_in, &ic.value);
@@ -1085,7 +1089,6 @@ static void cli_register_file_handles(bool no_close) /* {{{ */
10851089
ec.name = zend_string_init_interned("STDERR", sizeof("STDERR") - 1, 0);
10861090
zend_register_constant(&ec);
10871091
}
1088-
/* }}} */
10891092

10901093
static void sapi_cli_register_variables(zval *track_vars_array) /* {{{ */
10911094
{
@@ -1130,7 +1133,7 @@ static void *execute_script_cli(void *arg) {
11301133

11311134
php_embed_init(cli_argc, cli_argv);
11321135

1133-
cli_register_file_handles(false);
1136+
cli_register_file_handles();
11341137
zend_first_try {
11351138
if (eval) {
11361139
/* evaluate the cli_script as literal PHP code (php-cli -r "...") */

0 commit comments

Comments
 (0)