php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44217 Output after stdout/stderr closed cause immediate exit with status 0
Submitted: 2008-02-22 17:32 UTC Modified: 2018-06-19 10:13 UTC
Votes:2
Avg. Score:3.0 ± 2.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: exe at travian dot org Assigned: cmb (profile)
Status: Closed Package: CGI/CLI related
PHP Version: 5.*, 6CVS (2009-01-21) OS: *
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: exe at travian dot org
New email:
PHP Version: OS:

 

 [2008-02-22 17:32 UTC] exe at travian dot org
Description:
------------
If STDOUT and/or STDERR are closed, output by the php script cause the interpreter to exit immediately.

According to strace output, php tries to write to the closed STDOUT file handle, causing a "Bad file descriptor" error and exit of the interpreter:

[...]
close(1)                                = 0
[...]
write(1, "foo", 3)                      = -1 EBADF (Bad file descriptor)
close(0)                                = 0
close(2)                                = 0
[...]
exit_group(0)                           = ?
Process 19177 detached

Reproduce code:
---------------
<?php
fclose(STDOUT);
print "foo";
sleep(10);
?>

Expected result:
----------------
No output, php sleeping for 10 seconds.

Actual result:
--------------
php exits immediately, strace shows an "Bad file descriptor" on the write() try to STDOUT:

[...]
read(3, "<?php\nfclose(STDOUT);\n\nprint \"fo"..., 8192) = 51
read(3, "", 4096)                       = 0
read(3, "", 8192)                       = 0
close(3)                                = 0
munmap(0x2b1dce200000, 4096)            = 0
close(1)                                = 0
munmap(0x2b1dce202000, 4096)            = 0
write(1, "foo", 3)                      = -1 EBADF (Bad file descriptor)
close(2)                                = 0
close(0)                                = 0
munmap(0x2b1dce201000, 4096)            = 0
munmap(0x2b1dce1bf000, 266240)          = 0
mmap(NULL, 266240, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2b1dce1bf000
setitimer(ITIMER_PROF, {it_interval={0, 0}, it_value={0, 0}}, NULL) = 0
munmap(0x2b1dce1bf000, 266240)          = 0
brk(0xd36000)                           = 0xd36000
exit_group(0)                           = ?
Process 19196 detached


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-24 00:34 UTC] jani@php.net
That's quite expected since you're still trying to output to STDOUT. Why do you want to close STDOUT anyway?

See also: http://www.php.net/wrappers.php
 [2008-02-24 00:40 UTC] jani@php.net
Another bug #44218 describes some more expected behaviour caused by closing the input/output streams.

NOTE: Correct manual page: http://www.php.net/manual/en/wrappers.php.php
 [2008-02-25 11:54 UTC] exe at travian dot org
I'd expect php to discard every output after STDOUT is closed, instead of doing a silent exit (which is hard to track because no error handler or shutdown function is called). Another option (if output in this situation is considered to be an error) would be to trigger a warning/fatal and/or call the shutdown function. This would, at least, make it possible to track this issue.

I have to close STDOUT and STDERR in a daemonized processes to detach from the controlling terminal.
 [2008-04-27 13:40 UTC] duane at e164 dot org
The solution to stopping this output is rather simple. After you close the stdin/stdout/stderr open 3 new file descriptors to /dev/null and all output goes bye bye. eg.

	fclose(STDIN);
	fclose(STDOUT);
	fclose(STDERR);

	$fp1 = fopen('/dev/null', 'r');
	$fp2 = fopen('/dev/null', 'w');
	$fp3 = fopen('/dev/null', 'w');
 [2008-06-28 19:35 UTC] work at anomalizer dot net
I guess what you are asking for is something like this to happen when you close STDOUT

dup2(open("/dev/null", O_WRONLY), 1);
 [2009-07-23 22:41 UTC] jani@php.net
See bug #48803, using ignore_user_abort=1 "fixes" this for you.
 [2018-06-15 06:52 UTC] robberphex at gmail dot com
When ignore_user_abort is 0,
php should exit with non-zero code: https://github.com/php/php-src/pull/3306
 [2018-06-19 10:13 UTC] cmb@php.net
-Summary: Output after stdout/stderr closed cause immediate exit +Summary: Output after stdout/stderr closed cause immediate exit with status 0 -Status: Not a bug +Status: Re-Opened -Package: Filesystem function related +Package: CGI/CLI related -Assigned To: +Assigned To: cmb
 [2018-06-19 10:13 UTC] cmb@php.net
Re-opening wrt. <https://github.com/php/php-src/pull/3306>.
 [2018-06-19 10:31 UTC] cmb@php.net
Automatic comment on behalf of robberphex@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=ecc1a7c582bf8de1eaa619ac4451399ed58f502a
Log: Fix bug #44217: Output after stdout/stderr closed cause immediate exit with status 0
 [2018-06-19 10:31 UTC] cmb@php.net
-Status: Re-Opened +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 17:01:29 2024 UTC