reactphp / reactphp

Event-driven, non-blocking I/O with PHP.

Home Page:https://reactphp.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

http server return "Error: socket hang up"

Chrisdowson opened this issue · comments

<?php
require_once('./vendor/autoload.php');
$server = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
    $client = new React\Http\Browser();
    $count = 3;
    $start = time();
    $promises = [];
    while($count--) {
        $promises[] =
            $client->get('http://bing.com')->then(function (Psr\Http\Message\ResponseInterface $response) use ($count) {
            echo $count;
            echo "\n";
            return $count;
        });
    }
    $data = \Clue\React\Block\awaitAll($promises);
    $resp =  React\Http\Message\Response::plaintext(
        "Hello World!\n"
    );
    return $resp;
});
$socket = new React\Socket\SocketServer('127.0.0.1:8080');
$server->listen($socket);

when I call $data = \Clue\React\Block\awaitAll($promises);, error message:Error: socket hang up
image

@Chrisdowson Interesting one! It looks like you've been bitten by clue/reactphp-block#44 / clue/reactphp-block#66.

As a rule of thumb, mixing blocking and non-blocking code should be avoided. This means these functions should only be used in a blocking application. If you're using a non-blocking application, you should use non-blocking primitives provided by promises and event emitters.

The good news is we're currently working on our upcoming Async package that leverages PHP 8.1 fibers. The new await() function provided by this package can be used just fine by wrapping your HTTP server callback in an async() call, see also https://github.com/reactphp/async#await

I hope this helps! 👍