Non blocking Ajax request to PHP Non blocking Ajax request to PHP curl curl

Non blocking Ajax request to PHP


This is most likely due to the session file being locked. This is a very common oversight on many php-based web-apps. Essentially, when you call session_start() to access the $_SESSION array, it opens the session file in the tmp directory in read/write mode and locks this file to avoid potential concurrency issues. If you call another script from a different ajax request (or any HTTP request, such as from a new browser window), if that second request also calls session_start, it will wait until the session file is unlocked before moving forward.

The fix is to release the session file once you know you are no longer going to be writing to it. Since your use-case is a huge file download, it is unlikely that during the data output you will need to push anything into the $_SESSION array. You release it from write-mode by calling session_write_close()

I had no idea this was the case until I found out a popular web-app I frequently use was guilty of this. A great blog post on this common bottleneck is:

http://konrness.com/php5/how-to-prevent-blocking-php-requests/