fastcgi_finish_request creates hung connection when open session exists

Viewed 1471

I have a client side that sends a request that need long processing time, the client send the request in ajax. Once the request is accepted on the Server the client redirects to another page, this is accomplished by fastcgi_finish_request (I am running php-fpm)

LongWork.php:

<?php
    fastcgi_finish_request(); 
    sleep(1000); //Simulate long computation time
?>

client.js:

$.ajax({
    url: "...",
    data: {},
    success: function() {
        top.location.href="next_page.php" 
    }
});

The ajax gets sent and success callback causes redirection to next_page.php as expected.

But then the page halts and I do not get any service until the sleep is finished. It looks like my connection is waiting for the same php-fpm process to finish

I am running nginx with php-fpm, any Idea why this happens?

EDIT:

After more investigation I found the cause to this behavior is that I have a active session (from facebook SDK), When I destroy the session on LongWork.php:

<?php
    session_destroy(); // Session was halting the client from accessing another page
    fastcgi_finish_request(); 
    sleep(1000); //Simulate long computation time
?>

Can you please reflect on this solution?

Should I do something different from session_destroy()

EDIT:

following Lachlan Pease comment, I have switched session_destroy with session_write_close

2 Answers
Related