Edit report at https://bugs.php.net/bug.php?id=77366&edit=1
ID: 77366
Updated by: nikic@php.net
Reported by: lagodichyuriy at gmail dot com
Summary: invalid fclose behavior
-Status: Open
+Status: Not a bug
Type: Bug
Package: Streams related
Operating System: Archlinux
PHP Version: 7.2.13
Block user comment: N
Private report: N
New Comment:
Streams are closed when they go out of scope, so you do indeed need to make them global if you want
them to stay open outside the method.
Previous Comments:
------------------------------------------------------------------------
[2018-12-28 23:35:18] lagodichyuriy at gmail dot com
Extra info: it's possible to avoid this behavior by using the "global $stdin, $stdout,
$stderr;" right after the "fclose" section, however it looks like a dirty hotfix to
avoid unexpected "fclose" behavior which closes main streams and interrupts the execution.
------------------------------------------------------------------------
[2018-12-28 23:27:54] lagodichyuriy at gmail dot com
Description:
------------
I was writing PHP daemon when faced with a very weird fclose bug.
According to Linux daemons, they should not use STDIN/STDOUT/STDERR streams, so I must close them.
The only one way I see is to use fclose(STDIN) + fclose(STDOUT) + fclose(STDERR) and reopen them
later in the same order (when you close the standard input, output and error file descriptors, the
first three new descriptors will become the NEW standard input, output and error file descriptors).
In the same time, I got segfaults each time when tried to use "echo" inside my code. I
installed strace and checked the calls (see "Actual result" section): in the end of class
method all my fopens became closed automatically, so any usage of STDIN/STDOUT/STDERR triggers fatal
error.
This behavior of fclose should fixed or documentation have to be updated.
Test script:
---------------
<?php
class sample
{
public function method()
{
fclose(STDIN);
fclose(STDOUT);
fclose(STDERR);
$stdin = fopen('/dev/null', 'r');
$stdout = fopen('/dev/null', 'w');
$stderr = fopen('php://stdout', 'w');
}
}
$sample = new sample();
$sample->method();
echo 'segfault!';
Expected result:
----------------
close(0) = 0
close(1) = 0
close(2) = 0
lstat("/dev/null", {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0
lstat("/dev", {st_mode=S_IFDIR|0755, st_size=4000, ...}) = 0
open("/dev/null", O_RDONLY) = 0
fstat(0, {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0
lseek(0, 0, SEEK_CUR) = 0
open("/dev/null", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 1
fstat(1, {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0
lseek(1, 0, SEEK_CUR) = 0
dup(1) = 2
fstat(2, {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0
fstat(2, {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0
lseek(2, 0, SEEK_CUR) = 0
write(1, "segfault!", 9) = 9
close(2) = 0
close(1) = 0
close(0) = 0
Actual result:
--------------
close(0) = 0
close(1) = 0
close(2) = 0
lstat("/dev/null", {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0
lstat("/dev", {st_mode=S_IFDIR|0755, st_size=4000, ...}) = 0
open("/dev/null", O_RDONLY) = 0
fstat(0, {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0
lseek(0, 0, SEEK_CUR) = 0
open("/dev/null", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 1
fstat(1, {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0
lseek(1, 0, SEEK_CUR) = 0
dup(1) = 2
fstat(2, {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0
fstat(2, {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0
lseek(2, 0, SEEK_CUR) = 0
close(0) = 0
close(1) = 0
close(2) = 0
write(1, "segfault!", 9) = -1 EBADF (Bad file descriptor)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=77366&edit=1