filesystem icon indicating copy to clipboard operation
filesystem copied to clipboard

Reading a file throws error or hangs

Open Zizaco opened this issue 10 years ago • 10 comments

With the following code:

<?php
require "vendor/autoload.php";

$loop = \React\EventLoop\Factory::create();
$filesystem = \React\Filesystem\Filesystem::create($loop);
$loaded = [];

// I have "big_file_5.txt" and "big_file_7.txt".
// Both with about 6 MB.
foreach (['5', '7'] as $num) {
    $filename = __DIR__.'/resources/big_file_'.$num.'.txt';

    $file = $filesystem->file($filename);
    $file->getContents()->then(
        function ($content) use ($filename, &$loaded) {
            echo "Got contents of $filename\n";
            $loaded[] = $content;
        },
        function ($error) use ($filename, &$loaded) {
            echo "Error when reading $filename\n";
            echo $error;
        }
    );
}

$loop->addPeriodicTimer(1, function () use (&$loaded) {
    echo count($loaded)." loaded files\n";
    echo "tick-tack\n";
});

echo "Running. Press CTRL+C to exit\n";
$loop->run();

The output should be something along the lines of:

$ php main.php
Running. Press CTRL+C to exit
Got contents of <project_path>/resources/big_file_7.txt
Got contents of <project_path>/resources/big_file_5.txt
2 loaded files
tick-tack
2 loaded files
tick-tack
2 loaded files
tick-tack
2 loaded files
tick-tack
^C

But sometimes the Eio throws an unknown error

$ php main.php 
Running. Press CTRL+C to exit
Error when reading <project_path>/resources/big_file_7.txt
exception 'React\Filesystem\Eio\RuntimeException' with message 'Unknown error calling "eio_open"' in <project_path>/vendor/react/filesystem/src/EioAdapter.php:280
Stack trace:
#0 <project_path>/vendor/react/filesystem/src/EioAdapter.php(254): React\Filesystem\EioAdapter->executeDelayedCall('eio_open', Array, -1, Object(React\Promise\Deferred))
#1 [internal function]: React\Filesystem\EioAdapter->React\Filesystem\{closure}(Object(React\EventLoop\LibEventLoop))
#2 <project_path>/vendor/react/event-loop/Tick/FutureTickQueue.php(46): call_user_func(Object(Closure), Object(React\EventLoop\LibEventLoop))
#3 <project_path>/vendor/react/event-loop/LibEventLoop.php(202): React\EventLoop\Tick\FutureTickQueue->tick-tack()
#4 <project_path>/main.php(32): React\EventLoop\LibEventLoop->run()
#5 {main}Got contents of <project_path>/resources/big_file_5.txt
1 loaded files
tick-tack
1 loaded files
tick-tack
^C

Or one or both reads hangs forever

Running. Press CTRL+C to exit
Got contents of <project_path>/resources/big_file_7.txt
1 loaded files
tick-tack
1 loaded files
tick-tack
1 loaded files
tick-tack
1 loaded files
tick-tack
1 loaded files
tick-tack
1 loaded files
tick-tack
1 loaded files
tick-tack
1 loaded files
tick-tack
1 loaded files
tick-tack
1 loaded files
tick-tack
1 loaded files
tick-tack
... (forever)

Every time I run the script I got one of the results randomly(?). I'm running Ubuntu 14.04.1 LTS.

@WyriHaximus, do you have an idea of what may be causing the problem?

Zizaco avatar Apr 23 '15 12:04 Zizaco

Hmm interesting, I'll take a look at his, I've seen odd behavior with reading multiple files at the same time before but never tried to nail it down before because it occurred randomly. I'll give this code a shot locally and see what happens :+1: .

WyriHaximus avatar Apr 23 '15 20:04 WyriHaximus

Hello @WyriHaximus ! I have same issue with file reading, maybe you need to add other listener to the 'end' event on stream in FileInterface::getContents function. Like this solution:

    public function getContents()
    {
        return $this->open('r')->then(function ($stream) {
            /** @var ReadableStreamInterface $stream */
            $stream->once('end', function () use ($stream) { $stream->close(); });
            return Stream\buffer($stream)->always(function () {
                $this->close();
            });
        });
    }

digitv avatar Apr 26 '18 14:04 digitv

IIRC I fixed this locally but was hunting down some other errors popping up. I'll try to work on this during the weekend.

WyriHaximus avatar Apr 26 '18 14:04 WyriHaximus

This can be also an issue from React\Promise\Stream::buffer function, there is no handling of 'end' event and that is strange ^_^

digitv avatar Apr 26 '18 14:04 digitv

FYI my WIP changes: https://github.com/reactphp/filesystem/compare/master...WyriHaximus-labs:fix-test-chown-uid

WyriHaximus avatar Apr 26 '18 14:04 WyriHaximus

Thanks a lot, will use as temporary solution up to next update :smiley:

digitv avatar Apr 26 '18 14:04 digitv

Got a bit further, everything runs without issues on PHP 5 and 7 locally. So I'm currently debugging permission issues on travis

WyriHaximus avatar Apr 29 '18 21:04 WyriHaximus

Nearly fixed all issues: https://travis-ci.org/WyriHaximus-labs/filesystem/builds/378239247

Once done I'll file a bunch of PR's for all fixes :tada:

WyriHaximus avatar May 13 '18 00:05 WyriHaximus

Gave up and marked 5.4 as allowed failure. Will start creating PR's for all fixes tomorrow

WyriHaximus avatar May 27 '18 20:05 WyriHaximus

Right so PR #23, #24, #25, #26, #27, #28, #29, #30, and #31 filed :tada:

WyriHaximus avatar May 28 '18 18:05 WyriHaximus