Search code examples
phpamazon-web-servicestailtime-limiting

Impose time limit to popen/fgets in PHP


I want impose a time limit to a process reading using fgets opened by popen in PHP.

I have the next code:

$handle = popen("tail -F -n 30 /tmp/pushlog.txt 2>&1", "r");
while(!feof($handle)) {
    $buffer = fgets($handle);
    echo "data: ".$buffer."\n";
    @ob_flush();
    flush();
}
pclose($handle);

I tried without success:

set_time_limit(60);
ignore_user_abort(false);

The process is as follow:

  1. The browser send a GET request waiting for a Answer in HTML5 Server side events format.
  2. The request is received by AWS Load Balancer and is forwarded to EC2 instances.
  3. The answer is the last 30 lines of the file
  4. The browser receive it in 30 messages and the connection is persisted.
  5. If tail command sends a new line it is returned else fgets wait undefined time until new line is returned from tail command.
  6. AWS Load Balancer after 60 seconds of network inactivity (No new lines in 60 seconds) closes the connection to the browser. The connection to EC2 instance is not closed.
  7. The browser detect that the connection is closed and it opens a new connection, the process go back to step 1.

AS this steps describe, the connection between AWS Load Balancer and EC2 instance is never closed, after a few hours/days there is hundreds and hundreds of tail and httpd process running and the server start not answering.

Of course it appear to be a AWS Load Balancer bug, but I don't want start a process to gain the attention from Amazon and wait for a fix.

My temporary solution is do a sudo kill tail to kill the process before the server becomes unstable.

I think PHP doesn't stop the script because PHP is "blocked" waiting for fgets to finish.

I know that the time limit of AWS Load Balancer is editable, but I want keep in the default value, even a higher limit is not going to fix the problem.

I don't know if I need change the question to How to execute a process in linux with a time limit / timeout?.

PHP 5.5.22 / Apache 2.4 / Linux Kernel 3.14.35-28.38.amzn1.x86_64


Solution

  • Tested with PHP 5.5.20:

    //Change configuration.
    set_time_limit(0);
    ignore_user_abort(true);
    
    //Open pipe & set non-blocking mode.
    $descriptors  = array(0 => array('file', '/dev/null', 'r'),
                          1 => array('pipe', 'w'),
                          2 => array('file', '/dev/null', 'w'));
    $process      = proc_open('exec tail -F -n 30 /tmp/pushlog.txt 2>&1',
                                    $descriptors, $pipes, NULL, NULL) or exit;
    $stream       = $pipes[1];
    stream_set_blocking($stream, 0);
    
    //Call stream_select with a 10 second timeout.
    $read = array($stream); $write = NULL; $except = NULL;
    while (!feof($stream) && !connection_aborted()
            && stream_select($read, $write, $except, 10)) {
    
        //Print out all the lines we can.
        while (($buffer = fgets($stream)) !== FALSE) {
            echo 'data: ' . $buffer . "\n";
            @ob_flush();
            flush();
        }
    
    }
    
    //Clean up.
    fclose($stream);
    $status = proc_get_status($process);
    if ($status !== FALSE && $status['running'] === TRUE)
        proc_terminate($process);
    proc_close($process);