Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLI] Stop closing standard streams (v8.1) #8570

Closed
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ PHP NEWS
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? ??? ????, PHP 8.1.4

- CLI:
. Fixed bug GH-8575 (CLI closes standard streams too early). (Levi Morrison)

- Core:
. Fixed Haiku ZTS build. (David Carlier)
. Fixed bug GH-8059 arginfo not regenerated for extension. (Remi)
Expand Down
1 change: 1 addition & 0 deletions ext/zend_test/php_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ ZEND_BEGIN_MODULE_GLOBALS(zend_test)
HashTable global_weakmap;
int replace_zend_execute_ex;
int register_passes;
bool print_stderr_mshutdown;
zend_test_fiber *active_fiber;
ZEND_END_MODULE_GLOBALS(zend_test)

Expand Down
5 changes: 5 additions & 0 deletions ext/zend_test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ static ZEND_METHOD(ZendTestNS2_ZendSubNS_Foo, method)
PHP_INI_BEGIN()
STD_PHP_INI_BOOLEAN("zend_test.replace_zend_execute_ex", "0", PHP_INI_SYSTEM, OnUpdateBool, replace_zend_execute_ex, zend_zend_test_globals, zend_test_globals)
STD_PHP_INI_BOOLEAN("zend_test.register_passes", "0", PHP_INI_SYSTEM, OnUpdateBool, register_passes, zend_zend_test_globals, zend_test_globals)
STD_PHP_INI_BOOLEAN("zend_test.print_stderr_mshutdown", "0", PHP_INI_SYSTEM, OnUpdateBool, print_stderr_mshutdown, zend_zend_test_globals, zend_test_globals)
PHP_INI_END()

void (*old_zend_execute_ex)(zend_execute_data *execute_data);
Expand Down Expand Up @@ -463,6 +464,10 @@ PHP_MSHUTDOWN_FUNCTION(zend_test)

zend_test_observer_shutdown(SHUTDOWN_FUNC_ARGS_PASSTHRU);

if (ZT_G(print_stderr_mshutdown)) {
fprintf(stderr, "[zend_test] MSHUTDOWN\n");
}

return SUCCESS;
}

Expand Down
13 changes: 13 additions & 0 deletions ext/zend_test/tests/gh8575.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--TEST--
CLI: stderr is available in mshutdown
--SKIPIF--
<?php if (php_sapi_name() != "cli")) die('skip: cli test only'); ?>
morrisonlevi marked this conversation as resolved.
Show resolved Hide resolved
--EXTENSIONS--
zend_test
--INI--
zend_test.print_stderr_mshutdown=1
--FILE--
==DONE==
--EXPECTF--
==DONE==
[zend_test] MSHUTDOWN
22 changes: 13 additions & 9 deletions sapi/cli/php_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -538,19 +538,23 @@ static void cli_register_file_handles(bool no_close) /* {{{ */
s_out = php_stream_open_wrapper_ex("php://stdout", "wb", 0, NULL, sc_out);
s_err = php_stream_open_wrapper_ex("php://stderr", "wb", 0, NULL, sc_err);

/* Release stream resources, but don't free the underlying handles. Othewrise,
* extensions which write to stderr or company during mshutdown/gshutdown
* won't have the expected functionality.
*/
if (no_close) {
if (s_in) s_in->flags |= PHP_STREAM_FLAG_NO_CLOSE;
if (s_out) s_out->flags |= PHP_STREAM_FLAG_NO_CLOSE;
if (s_err) s_err->flags |= PHP_STREAM_FLAG_NO_CLOSE;
}

if (s_in==NULL || s_out==NULL || s_err==NULL) {
if (s_in) php_stream_close(s_in);
if (s_out) php_stream_close(s_out);
if (s_err) php_stream_close(s_err);
return;
}

if (no_close) {
s_in->flags |= PHP_STREAM_FLAG_NO_CLOSE;
s_out->flags |= PHP_STREAM_FLAG_NO_CLOSE;
s_err->flags |= PHP_STREAM_FLAG_NO_CLOSE;
}

s_in_process = s_in;

php_stream_to_zval(s_in, &ic.value);
Expand Down Expand Up @@ -956,7 +960,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
switch (behavior) {
case PHP_MODE_STANDARD:
if (script_file) {
cli_register_file_handles(/* no_close */ PHP_DEBUG || num_repeats > 1);
cli_register_file_handles(/* no_close */ true);
}

if (interactive) {
Expand Down Expand Up @@ -991,7 +995,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
}
break;
case PHP_MODE_CLI_DIRECT:
cli_register_file_handles(/* no_close */ PHP_DEBUG || num_repeats > 1);
cli_register_file_handles(/* no_close */ true);
zend_eval_string_ex(exec_direct, NULL, "Command line code", 1);
break;

Expand All @@ -1006,7 +1010,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
file_handle.filename = NULL;
}

cli_register_file_handles(/* no_close */ PHP_DEBUG || num_repeats > 1);
cli_register_file_handles(/* no_close */ true);

if (exec_begin) {
zend_eval_string_ex(exec_begin, NULL, "Command line begin code", 1);
Expand Down