Bug #75538 [Opn]: stream_set_blocking() always returning false for files on windows

From: Date: Wed, 22 Nov 2017 10:04:46 +0000
Subject: Bug #75538 [Opn]: stream_set_blocking() always returning false for files on windows
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-212673@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75538&edit=1

 ID:                 75538
 User updated by:    samuel dot chemla at orange dot com
 Reported by:        samuel dot chemla at orange dot com
 Summary:            stream_set_blocking() always returning false for
                     files on windows
 Status:             Open
 Type:               Bug
 Package:            Streams related
 Operating System:   Windows
 PHP Version:        7.1.11
 Block user comment: N
 Private report:     N

 New Comment:

Hi,
Thanks for your answer.
I didn't realize the implication of this, and I understand it's not a priority.
I'm not C developper, but I would be happy to help on PHP side (testing?)...

Maybe we should at least update the doc to mention that non blocking file acces cannot (yet :-)) be
guaranteed.


Previous Comments:
------------------------------------------------------------------------
[2017-11-21 15:01:33] ab@php.net

Thanks for the further explanation. With reactphp - yep, the topic was discussed at least with Bob
several times. However it regards more to standard file descriptors and pipes. Regular files are a
different matter.

What i was basically asking for was a bare implementation in PHP as a show case to evaluate
crossplatform. A flag being true/false is a bit insufficient :) Regular files are always available.
The I/O performance depends on quite some factors like caching, compression, encryption, etc. Libs
like libeven or libuv use also threading besidses the libc APIs. Just by changing a flag passed to
the C API likely won't guarantee anything. Even if done, the system can still decide to go
synchronously. An option here could be to cache those files in RAM, if possible.

So in first place - it is barely a bug in PHP, disregard the OS. The socket APIs support
non-blocking crossplatform. It might be for sure a significant chunk of work to spread this onto
regular files, standard descriptors and pipes. For what it matters, I'd be not really convinced
ATM it is worth and specially is possible without significant userspace API rework :( Perhaps libuv
and other bindings are a worthy option in this regard. At the current stage, even if the
non-blocking flag can be changed on some platforms, it doesn't guarantee non-blocking with the
regard to possible obstacles.

Thanks.

------------------------------------------------------------------------
[2017-11-21 10:16:54] samuel dot chemla at orange dot com

I made a mistake in my previous comment, you must read
array(9) {
  ...
  'blocked' =>
  bool(false)
  ...
}
(I copied/pasted the result without "n" mode)

------------------------------------------------------------------------
[2017-11-21 09:06:54] samuel dot chemla at orange dot com

Hi,
I'm building a server with https://github.com/reactphp/socket.
This server accepts connection from many clients and stream files to them (or receive files from
them).

As it handles all clients connections in one loop, all the code must be non blocking to achieve
optimal performance.

The most straightforward way to serve files is then to use https://github.com/reactphp/stream with a fopen()
resource, configured in a non blocking mode.

By the way react/stream doc says fopen() may block, but I see a non documented "n" mode
when using fopen (see https://github.com/php/php-src/blob/2283b6ff534e173bc56dc73137fedd19f37fc979/main/streams/plain_wrapper.c#L105)
if (strchr(mode, 'n')) {
		flags |= O_NONBLOCK;
}
It actually works on linux, but not on windows.
$fileRes = fopen("bar.txt", 'wbn');
if ($fileRes === false) {
        throw new Exception();
}
var_dump(stream_get_meta_data($fileRes));
will return :
array(9) {
  ...
  'blocked' =>
  bool(true)
  ...
}
(Maybe I should file another report on php documentation for this?)


https://github.com/reactphp/filesystem offers a
non blocking filesystem acces, but it seems very experimental, complex, and require eio extention or
create processes to read files.
I was looking for a simple solution with pure php (and fopen)

------------------------------------------------------------------------
[2017-11-20 19:36:47] ab@php.net

Thanks for the report. Could you give an extended example in PHP on the topic you've mentioned?

Thanks

------------------------------------------------------------------------
[2017-11-19 16:39:27] samuel dot chemla at orange dot com

Description:
------------
Tested on PHP 5.6, PHP 7.0.24 & PHP 7.1.11.

stream_set_blocking() return always false for files (used with fopen()) on windows.
It seems to work on linux.

This is related to an old bug marked as "not a bug", but I prefered opening this new one.
https://bugs.php.net/bug.php?id=50856
Is this really not a bug? If so, then the doc shoud be updated to clarify why.

IMHO this is a bug because non blocking local file streams is a desired feature, even on windows.
Use case: dealing with multiple files reading and/or writing in parallel

Test script:
---------------
$fileRes = fopen("foo.txt", 'wb');
if ($fileRes === false) {
    throw new Exception();
}
var_dump(stream_set_blocking($fileRes, 0));

Expected result:
----------------
bool(true)

Actual result:
--------------
bool(false)


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=75538&edit=1


Thread (13 messages)

« previous php.bugs (#212673) next »