PHP: Hypertext Preprocessor (original) (raw)

fastcgi_finish_request

(PHP 5 >= 5.3.3, PHP 7, PHP 8)

fastcgi_finish_request — Flushes all response data to the client

Description

fastcgi_finish_request(): bool

Parameters

This function has no parameters.

Return Values

Returns [true](reserved.constants.php#constant.true) on success or [false](reserved.constants.php#constant.false) on failure.

Found A Problem?

tuxrampage

8 years ago

`There are some pitfalls you should be aware of when using this function.

The script will still occupy a FPM process after fastcgi_finish_request(). So using it excessively for long running tasks may occupy all your FPM threads up to pm.max_children. This will lead to gateway errors on the webserver.

Another important thing is session handling. Sessions are locked as long as they're active (see the documentation for session_write_close()). This means subsequent requests will block until the session is closed.

You should therefore call session_write_close() as soon as possible (even before fastcgi_finish_request()) to allow subsequent requests and a good user experience.

This also applies for all other locking techniques as flock or database locks for example. As long as a lock is active subsequent requests might bock.

`

rundiz dot com

3 years ago

`Here are few example of how to using it.

The first is basic example.

' . $file . ' was deleted.
' . PHP_EOL; } echo '

Calling to fastcgi_finish_request().

' . PHP_EOL; echo '

If success, the file ' . $file . ' will be created.

' . PHP_EOL; if ( function_exists('fastcgi_finish_request')) { fastcgi_finish_request(); } else { echo '

This server does not support fastcgi_finish_request() function.

' . PHP_EOL; echo 'Exit now.
' . PHP_EOL; exit(); } echo 'This line will be not echo out.
' . PHP_EOL;file_put_contents($file, date('Y-m-d H:i:s') . PHP_EOL, FILE_APPEND); ?>

The file text.txt will be create if successfully.

==========================

The second is about execution timeout.

' . $file . ' was deleted.
' . PHP_EOL; } echo '

Testing timeout and fastcgi_finish_request() function.

' . PHP_EOL; echo '

Set timeout to ' . ini_get('max_execution_time') . ' seconds.

' . PHP_EOL; echo '

Calling to fastcgi_finish_request().

' . PHP_EOL; echo '

If success, the file ' . $file . ' will be created but error will be shown in the log.

' . PHP_EOL; if ( function_exists('fastcgi_finish_request')) { fastcgi_finish_request(); } else { echo '

This server does not support fastcgi_finish_request() function.

' . PHP_EOL; echo 'Exit now.
' . PHP_EOL; exit(); }$i = 1; while(true){ if ($i <= 10) { file_put_contents($file, date('Y-m-d H:i:s') . PHP_EOL, FILE_APPEND); $i++; } //to infinity and beyond... } ?>

I found that the code will be working as long as it is not reach timeout setting in php.ini or set_time_limit() function.

`

john at jrcii dot com

1 year ago

`mike@php.net decided this https://bugs.php.net/bug.php?id=68772 is "not a bug" however be warned:

If you write to the buffer (using print statements, etc.) after calling fastcgi_finish_request(), your script will exit with no error message or exception. Calling ignore_user_abort(true) after fastcgi_finish_request() can mitigate this issue.

`