wish for implementation of fastcgi_finish_request(), or any other function that has the same effect (close the connection with the client and do extra work background)
Thanks for reporting this. We're working on fastcgi support and this would likely come after that. register_shutdown_function() will get you cleanup behavior after a request is done but I don't believe we currently have an equivalent in PHP land to disconnect the user and then do something else.
This is a legitimate request but we don't have any plans to implement this anytime soon -- still keeping this open since it's relatively recent and fastcgi is new. We'd be happy to review a PR.
If you do:
register_shutdown_function(function() {
// Do lots of stuff
});
die();
Then the request will finish for the user, and your shutdown handler will still be called. We do this a lot at Facebook to commit transactions that don't need user input.
This is part of php-fpm (an extension), not Zend. We have no current plans to implement this function inside Zend (it would belong in php-fpm when that is implemented) and @ptarjan describes what I believe is a workaround above.
@ptarjan is this a hhvm only workarround or does this also work in zend?
@staabm also works in zend
@ptarjan
I tried the following snippet, but it didn't work for me..
<?php
echo "yes";
register_shutdown_function(function() {
sleep("2");
});
die();
the website takes 2 seconds to load, no matter if I use die or exit after the shutdown func with zend php 5.3.2
I'm going to change the type of this to missing extension and change the title since this is really asking for php-fpm to be implemented. We wouldn't package it with the core even if we or someone else did.
[I'm at least not sure what semantics PHP has about client disconnection / page loading when die() / exit() is called.]
fastcgi_finish_request would be nice so i do not need to re-write lots of my app to handle this single function call.
@staabm, @scannell
look at this: php connection handling
We don't want to break compatibility here though by closing connections when PHP doesn't. As I said, the best way to deal with this would be for someone to implement php-fpm and, as a side effect, fastcgi_finish_request.
For anyone finding this in the future (and also for @staabm ), after a chat with Paul on the HHVM IRC, it seems that there is an HHVM specific command register_postsend_function.
So the code below works fine:
<?php
echo "and ...";
register_postsend_function(function() {
echo "... you should not be seeing this";
sleep("10");
});
die();
I'm pretty new to HHVM and was looking for replacement to fastcgi_finish_request.
register_postsend_function first didn't work for me. The request didn't close and was waiting for the function to finish. But if I put a "sleep(1)" in the beginning of my postsend-function before my long running task it work perfectly.
Anyone else noticed this as well?
Running HipHop VM 3.3.0-dev+2014.08.11 (rel)
@onnerby read my post above.
@sinaa I did. But what I'm saying is that the register_postsend_function seems to be a bit shaky.
This did not close the request before "long_running_task":
register_postsend_function(function(){
long_running_task();
});
But this did, and "long_running_task" is done in the background when request is close:
register_postsend_function(function(){
sleep(1);
long_running_task();
});
Very strange, but I do not want to report this as a bug unless anyone else has experienced this.
Ah, sorry I misread that you were running fastcgi_finish_request().
The issue is rather odd. There is a specific test case for this, which I believe is passing: https://github.com/facebook/hhvm/blob/5fbdeba60985da2521c9a278f18a00959666aff4/hphp/test/slow/request_events/oom_psp.php
How long does the long task run for?
@sinaa The long task is running for about a minute in my case and is running a shell command. This looks like a classic "racing condition" problem in C++ :smile:
look at this: php connection handling
@kandy thanks for the link. this example did not work on php 5.3.x on my Ubuntu LTS box. will try it on php 5.4...
As a note about "register_postsend_function" which may make it APPEAR to no instantly close the browser session even thought it actually is.
HHVM has a limited number of worker threads to process requests. Even though the browser connection is closed, the worker thread is still actively processing the request. On my particular HHVM test instance, there are a total of 4 worker threads.
Doing the example above with "Sleep(10)" (taking 10 seconds of execution time)
First through fourth request returns instantly.
Request #5 then stalls waiting for there to be an active worker thread. This means that it is stalling BEFORE execution even begins at the first line of PHP/HACK code, NOT stalling at "register_postsend_function"
Any updates on this? :walking:
I believe they are still not implemented, but there are other functions in HHVM (as mentioned further up) that provide similar functionality.
Might be helpful to others, had to run session_write_close for it to work:
register_postsend_function(function () use ($postSendFunc) {
session_write_close();
$postSendFunc();
});