php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68772 calling flush() after fastcgi_finish_request() exits PHP without errors or warn
Submitted: 2015-01-09 01:32 UTC Modified: 2015-01-29 14:11 UTC
Votes:3
Avg. Score:4.0 ± 0.8
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:2 (100.0%)
From: rikurr at gmail dot com Assigned:
Status: Not a bug Package: Output Control
PHP Version: 5.4.36 OS: Linux
Private report: No CVE-ID: None
 [2015-01-09 01:32 UTC] rikurr at gmail dot com
Description:
------------
At least explicit flushing after calling fastcgi_finish_request() will exit PHP without any errors or warnings.

I found this behavior while writing a code that:
 1) calls fastcgi_finish_request() and based on parameters either
  1.a) continues processing in the background (asynchronous)
  1.b) streams the processsing logs to the screen (synchronous)
 2) has a generic logging method that both echoes+flushes(streaming) and writes the logs to a file.

As a workaround I will have have to check the parameter condition during logging to not to flush if fastcgi_finish_request() was called.

Related: http://stackoverflow.com/questions/14191947/php-fpm-fastcgi-finish-request-reliable/27847878#27847878

Test script:
---------------
<?php

function writestamp($case, $file){
  file_put_contents($file, "". $case . ": " . time() . "" . PHP_EOL, FILE_APPEND);
}

$file = tempnam(sys_get_temp_dir(), 'flushbug_');
echo 'Writing 4 timestamps to: '.$file;

// this one gets called
writestamp('1', $file);
// and this
register_shutdown_function('writestamp', '4', $file);
// finish the request
fastcgi_finish_request();
// as does this
writestamp('2', $file);
// but this one does NOT - calling flush() after fastcgi_finish_request() exits PHP without errors or warnings
flush();
writestamp('3', $file);

Expected result:
----------------
PHP should continue processing the script if I flush() output to the client after calling fastcgi_finish_request() and output 4 lines to the temporary file:

1: 1420766961
2: 1420766961
3: 1420766961
4: 1420766961


Actual result:
--------------
PHP exits the process without, at least to my knowledge, errors or warnings and only outputs 3 lines to the temporary file:

root@afa63474201a:/# cat /tmp/flushbug_XCsRsu 
1: 1420766961
2: 1420766961
4: 1420766961


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-01-29 14:11 UTC] mike@php.net
-Status: Open +Status: Not a bug
 [2015-01-29 14:11 UTC] mike@php.net
flush() on the closed fcgi output stream triggers an aborted_connection, so you've got to set ignore_user_abort to continue script execution.
 [2020-03-17 10:11 UTC] Pikamander2 at gmail dot com
A simple fix is to call ignore_user_abort(true) right before or after the `fastcgi_finish_request` call:

ignore_user_abort(true);
fastcgi_finish_request();
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 15:01:28 2024 UTC