php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77366 invalid fclose behavior
Submitted: 2018-12-28 23:27 UTC Modified: 2019-09-06 12:22 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: lagodichyuriy at gmail dot com Assigned:
Status: Not a bug Package: Streams related
PHP Version: 7.2.13 OS: Archlinux
Private report: No CVE-ID: None
 [2018-12-28 23:27 UTC] 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)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-12-28 23:35 UTC] 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.
 [2019-09-06 12:22 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2019-09-06 12:22 UTC] nikic@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 14:01:32 2024 UTC