Bug #68772 [Com]: calling flush() after fastcgi_finish_request() exits PHP without errors or warn
| From: | Pikamander2 at gmail dot com | Date: | Tue, 17 Mar 2020 10:11:38 +0000 |
| Subject: | Bug #68772 [Com]: calling flush() after fastcgi_finish_request() exits PHP without errors or warn | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-226131@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=68772&edit=1
ID: 68772
Comment by: Pikamander2 at gmail dot com
Reported by: rikurr at gmail dot com
Summary: calling flush() after fastcgi_finish_request() exits
PHP without errors or warn
Status: Not a bug
Type: Bug
Package: Output Control
Operating System: Linux
PHP Version: 5.4.36
Block user comment: N
Private report: N
New Comment:
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();
Previous Comments:
------------------------------------------------------------------------
[2015-01-29 14:11:41] 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.
------------------------------------------------------------------------
[2015-01-09 01:32:42] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=68772&edit=1