@@ -536,19 +536,23 @@ static void cli_register_file_handles(bool no_close) /* {{{ */
536536 s_out = php_stream_open_wrapper_ex ("php://stdout" , "wb" , 0 , NULL , sc_out );
537537 s_err = php_stream_open_wrapper_ex ("php://stderr" , "wb" , 0 , NULL , sc_err );
538538
539+ /* Release stream resources, but don't free the underlying handles. Othewrise,
540+ * extensions which write to stderr or company during mshutdown/gshutdown
541+ * won't have the expected functionality.
542+ */
543+ if (no_close ) {
544+ if (s_in ) s_in -> flags |= PHP_STREAM_FLAG_NO_CLOSE ;
545+ if (s_out ) s_out -> flags |= PHP_STREAM_FLAG_NO_CLOSE ;
546+ if (s_err ) s_err -> flags |= PHP_STREAM_FLAG_NO_CLOSE ;
547+ }
548+
539549 if (s_in == NULL || s_out == NULL || s_err == NULL ) {
540550 if (s_in ) php_stream_close (s_in );
541551 if (s_out ) php_stream_close (s_out );
542552 if (s_err ) php_stream_close (s_err );
543553 return ;
544554 }
545555
546- if (no_close ) {
547- s_in -> flags |= PHP_STREAM_FLAG_NO_CLOSE ;
548- s_out -> flags |= PHP_STREAM_FLAG_NO_CLOSE ;
549- s_err -> flags |= PHP_STREAM_FLAG_NO_CLOSE ;
550- }
551-
552556 s_in_process = s_in ;
553557
554558 php_stream_to_zval (s_in , & ic .value );
@@ -954,7 +958,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
954958 switch (behavior ) {
955959 case PHP_MODE_STANDARD :
956960 if (script_file ) {
957- cli_register_file_handles (/* no_close */ PHP_DEBUG || num_repeats > 1 );
961+ cli_register_file_handles (/* no_close */ true );
958962 }
959963
960964 if (interactive ) {
@@ -989,7 +993,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
989993 }
990994 break ;
991995 case PHP_MODE_CLI_DIRECT :
992- cli_register_file_handles (/* no_close */ PHP_DEBUG || num_repeats > 1 );
996+ cli_register_file_handles (/* no_close */ true );
993997 zend_eval_string_ex (exec_direct , NULL , "Command line code" , 1 );
994998 break ;
995999
@@ -1004,7 +1008,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
10041008 file_handle .filename = NULL ;
10051009 }
10061010
1007- cli_register_file_handles (/* no_close */ PHP_DEBUG || num_repeats > 1 );
1011+ cli_register_file_handles (/* no_close */ true );
10081012
10091013 if (exec_begin ) {
10101014 zend_eval_string_ex (exec_begin , NULL , "Command line begin code" , 1 );
0 commit comments