Search code examples
phprestkeep-alivereactphpfastroute

ReactPHP memory problem with keep-alive connections


I have a API Rest developed with ReactPHP and FastRoute. If I call it from a web interfaz with header "Connection: keep-alive" the memory starts to increase until memory crash. I tried to change the header to "Connection: close" and there are not memory leaks.

Do you know why is this happening? Can I disable keep-alive in my ReactPHP server?

Thanks

This is the code of my server

$server = new Server($loop, new ErrorHandler(), new JsonRequestDecoder(), new CorsMiddleware([
    'allow_origin' => [
        '*',
        'http://localhost:3000'
    ],
    'allow_methods'     => ['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS'],
    'allow_headers'     => ['DNT','X-Custom-Header','Keep-Alive','User-Agent','X-Requested-With','If-Modified-Since','Cache-Control','Content-Type','Content-Range','Range'],
    'expose_headers'    => ['DNT','X-Custom-Header','Keep-Alive','User-Agent','X-Requested-With','If-Modified-Since','Cache-Control','Content-Type','Content-Range','Range']
 
]), new Router($routes));

Solution

  • ReactPHP maintainer here.

    This got also reported in https://github.com/reactphp/http/issues/514 in our ReactPHP HTTP component and we fixed this just a few days ago through https://github.com/reactphp/http/pull/515. Like you said above, this only occurs when using persistent connections (keep-alive), the memory grew because the connection close handler was not being cleaned up properly for each request.

    We're currently focusing on a new reactphp/http v1.10.0 which is planned to be released around next week and will contain the fix for this. Once released you can install the new version and shouldn't encounter this issue anymore.