Skip to content

Await goes into infinite loop #66

Description

@sorenbronsted

I am trying to use reactphp in my application without rewriting the whole thing to promises but to use await instead. I have though some problems with await which I can boil down to this example:

<?php

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Loop;
use React\Http\Browser;
use React\Http\HttpServer;
use React\Http\Message\Response;
use React\Socket\SocketServer;
use function Clue\React\Block\await;

require 'vendor/autoload.php';

$http = new HttpServer(function (ServerRequestInterface $request) {
    echo $request->getMethod() . ' ' . $request->getUri() . PHP_EOL;
    return new Response(
        200,
        array(
            'Content-Type' => 'text/plain'
        ),
        "Hello World!\n"
    );
});

$socket = new SocketServer('127.0.0.1:8080');
$http->listen($socket);

$txn = 1;
$preventInfiniteLoop = false;

$loop = Loop::get();
$loop->addPeriodicTimer(1, function() use(&$txn, &$preventInfiniteLoop) {
    $browser  = new Browser();
    echo "$txn Timer running" . PHP_EOL;

    // Synkron version
    if ($preventInfiniteLoop) {
        return;
    }
    $preventInfiniteLoop = true;
    $response = await($browser->get('http://localhost:8080'));
    echo $response->getBody() . PHP_EOL;


    // Asyncron version
    // $browser->get('http://localhost:8080')->then(function(ResponseInterface $response) {
    //     echo $response->getBody() . PHP_EOL;
    // }, function(Exception $error) {
    //     var_dump($error);
    // });


    $preventInfiniteLoop = false;
    $txn++;
});

echo "Server running at http://127.0.0.1:8080" . PHP_EOL;

Then asyncron works out of the box, but the syncron way has 2 issue:

  1. it goes into infinite loop
  2. i can prevent the infinite loop, but then it stops the server.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions