|  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
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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
42 - 8 = ?
Subscribe to this entry?

 [2015-01-09 01:32 UTC] rikurr at gmail dot com
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.


Test script:

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
// as does this
writestamp('2', $file);
// but this one does NOT - calling flush() after fastcgi_finish_request() exits PHP without errors or warnings
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


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-01-29 14:11 UTC]
-Status: Open +Status: Not a bug
 [2015-01-29 14:11 UTC]
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:

PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jun 17 04:01:31 2024 UTC