Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading a file throws error or hangs #5

Open
Zizaco opened this issue Apr 23, 2015 · 10 comments
Open

Reading a file throws error or hangs #5

Zizaco opened this issue Apr 23, 2015 · 10 comments
Assignees

Comments

@Zizaco
Copy link

Zizaco commented Apr 23, 2015

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 Zizaco changed the title Sometimes the reading of a file simply hangs Reading a file throws error or hangs Apr 23, 2015
@WyriHaximus WyriHaximus self-assigned this Apr 23, 2015
@WyriHaximus
Copy link
Member

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 👍 .

@digitv
Copy link

digitv commented Apr 26, 2018

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();
            });
        });
    }

@WyriHaximus
Copy link
Member

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

@digitv
Copy link

digitv commented Apr 26, 2018

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

@WyriHaximus
Copy link
Member

FYI my WIP changes: master...WyriHaximus-labs:fix-test-chown-uid

@digitv
Copy link

digitv commented Apr 26, 2018

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

@WyriHaximus
Copy link
Member

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

@WyriHaximus
Copy link
Member

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 🎉

@WyriHaximus
Copy link
Member

WyriHaximus commented May 27, 2018

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

@WyriHaximus
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants