d.a.vorm d.a.vorm - 1 month ago 11
PHP Question

PHP web app stuck loading

I have a web app in development on localhost written in PHP and JS, that occasionally gets stuck loading.
By stuck loading I mean you still don't see anything on the page.
Once it's stuck, even if I open it in another tab, it's still stuck. Only when I close the window and reopen would it work again.

From that, I suspect that it has something to do with sessions, but I'm not aware how sessions can affect this. Is it the PHP session handling code or the browser?

I mostly use Chrome (right now version 54). The server is XAMPP version 5.6.3 running on Windows 10. The app makes connections to SQL Server 2012, although I don't believe that's relevant in this case.

Additional info:

If I wait long enough, eventually the script will timeout and show:

Fatal error: Maximum execution time of 90 seconds exceeded in project_functions.php on line 22


function startSession(){
if (session_status() == PHP_SESSION_NONE) {
session_start();
} // this is line 22
}


A side question: why is it stuck on line 22, which is just a closing brace?

Answer

The default implementation of session_start will lock the session to prevents race conditions. That's means that if you open a session in a long running script and you don't close it with session_write_close, the next request that try to open that session will wait until the lock is released.

The easy solution: close the session early with a call to session_write_close()