php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72517 file_exists(), is_file() and stat() report wrong result infrequently (~1%)
Submitted: 2016-06-29 20:08 UTC Modified: 2017-07-12 14:32 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: stgrein at gmail dot com Assigned:
Status: Wont fix Package: Filesystem function related
PHP Version: 7.0.8 OS: Windows
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: stgrein at gmail dot com
New email:
PHP Version: OS:

 

 [2016-06-29 20:08 UTC] stgrein at gmail dot com
Description:
------------
Hi,

functions which check if a file exists do report wrong results sometimes.
I did not find a special pattern, but maybe higher load is triggering it more.
The chance on my machine is around ~1% to hit the bug per function call.
I am using PHP v7.0.8 now but also had this bug on PHP v7.0.7. I did not test with earlier Versions.

Test script:
---------------
<?php

//check an existing file
$a = file_exists("/this/file/exists");

if($a === true) {
  echo "File exists!"; //expected
} else {
  echo "File does NOT exists!"; //bug hit
}

//expected output:
//File exists!

//chance to get this output is around 1% on my machine under load
//File does NOT exists!



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-06-29 20:10 UTC] stgrein at gmail dot com
-Operating System: +Operating System: Windows
 [2016-06-29 20:10 UTC] stgrein at gmail dot com
Additional information:
I used the VC14 x64 Thread Safe build from windows.php.net
 [2016-06-30 07:50 UTC] requinix@php.net
-Summary: file_exists(), is_file() and stat() report wrong result +Summary: file_exists(), is_file() and stat() report wrong result infrequently (~1%) -Status: Open +Status: Feedback
 [2016-06-30 07:50 UTC] requinix@php.net
Here's a cmd command to repeat the test 10k times:

(for /l %i in (1,1,10000) do php -r "var_dump(file_exists('/this/file/exists'));") | find /c "true"

Naturally it needs to be run from where you have PHP installed (or give the path to php.exe) and you need to change the /this/file/exists path.
If you run that, does it show 10000 or something less? What if the system and/or disk is under load?

Also, if you're getting this behavior with regular PHP code, are you potentially creating that file first? Because PHP will cache stat information during execution; if you are creating the file then use clearstatcache() before re-testing for the presence of the file.
http://php.net/manual/en/function.clearstatcache.php

Also, testing with PHP 5.5 or 5.6 would be appreciated.
 [2016-07-10 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 [2016-07-12 14:01 UTC] stgrein at gmail dot com
-Status: No Feedback +Status: Closed
 [2016-07-12 14:01 UTC] stgrein at gmail dot com
Sorry for the late reply.
In the CLI the problem does not occur. There I get the 10000 as response for your test case.

It occurs only with the php7apache2_4.dll (Apache x64 2.4.20, PHP 7.08 x64 Windows)

I cannto re-open the bug:
ERROR:
You aren't allowed to change a bug to that state
 [2016-07-12 15:20 UTC] requinix@php.net
-Status: Closed +Status: Feedback
 [2016-07-12 15:20 UTC] requinix@php.net
Try a similar test with Apache:

/test.php:
<?php
var_dump(file_exists('/this/file/exists'));

/loop.php:
<?php
$c = 0;
for ($i=1; $i<=10000; $i++) {
  if (strpos(file_get_contents("http://localhost/test.php"), "true")) $c++;
}
echo $c, "/10000";
 [2016-07-14 10:37 UTC] ab@php.net
A path of the form \path\to\file is relative to the current drive. The CWD is stored per process in the CRT. Now, if something changes CWD to another drive in one thread, a drive relative path could likely become invalid in another thread. That's the only scenario i can imagine for this bug to reproduce, especially on high load that produces enough concurrency.

I barely see a way this bug to be reproduced with the pure core, because chdir() is not used in the thread safe builds. However some non core extension or library could cause this. @stgrein, is this the pure core that reproduces it? It might be quite tricky to trace what's causing the CWD change, maybe procmon or drmemory could give some chance.

Thanks.
 [2016-07-14 11:39 UTC] stgrein at gmail dot com
-Status: Feedback +Status: Open
 [2016-07-14 11:39 UTC] stgrein at gmail dot com
Thank you for the test case.

I had to add a set_time_limit(0); to the loop.php
Then I got: 10000/10000


I modified test.php a little bit to use a relative path and I had to start it in 5 different Browser Tabs in Parallel:

/test.php:
<?php
var_dump(file_exists('relative\path\to\an\existing\file.php'));

Now I got:
Tab1: 8248/10000
Tab2: 7762/10000
Tab3: 7685/10000
Tab4: 7726/10000
Tab5: 7783/10000

This was reproduceable if i refreshed (F5) my actual website in a sixth tab.

Conclusion: While using absolute paths everything is working, but if using relative paths there is a chance to fail.

These are my loaded modules:

[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
dom
exif
filter
gd
gettext
hash
iconv
imagick
intl
json
ldap
libxml
mbstring
mcrypt
mysqli
mysqlnd
OAuth
openssl
pcre
PDO
pdo_mysql
pdo_sqlsrv
Phar
Reflection
session
SimpleXML
soap
sockets
SPL
sqlsrv
standard
tokenizer
wddx
xdebug
xml
xmlreader
xmlrpc
xmlwriter
xsl
Zend OPcache
zip
zlib

[Zend Modules]
Xdebug
Zend OPcache

I suspect the imagick extension (I tested with different versions but also with the very newest one) to cause this behavior.
 [2016-07-14 15:31 UTC] stgrein at gmail dot com
For your info: It also occurs with imagick not loaded.
 [2017-07-12 14:32 UTC] ab@php.net
-Status: Open +Status: Wont fix
 [2017-07-12 14:32 UTC] ab@php.net
As discussed, this is the nature of the thread safe SAPI.

Thanks.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Fri Sep 20 23:01:26 2019 UTC