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

From: Date: Tue, 21 Nov 2017 09:07:00 +0000
Subject: Bug #75538 [Fbk->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-212652@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:             Feedback
+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,
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)


Previous Comments:
------------------------------------------------------------------------
[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 (#212652) next »