6

I'm using PHP to download a (big) file from a remote server and this download is triggered by clicking on a download button on a web page.

So when i click the download button on the web page, then an Ajax request (with angulars $http) is made to a PHP function. That function triggers a download using cURL.

In the mean time I'd like to make other requests to my PHP site with Ajax. But all other Ajax requests show the status Pending as long as the download is in progress.

So basically the download is blocking every other request to PHP. Is there any way I can avoid this blockage?

Vivendi
  • 20,047
  • 25
  • 121
  • 196

1 Answers1

15

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/

Community
  • 1
  • 1
Anthony
  • 36,459
  • 25
  • 97
  • 163
  • It could also be due to the maximum simultaneous connections from a single host allowed by the server. Any connections beyond the maximum will typically be kept alive but not processed by the server until a slot frees up. – Nico Mar 09 '14 at 12:29
  • Thanks a lot! That seems to be the problem. I tested it by removing `session_start()` from my application. Making multiple request while the file is downloading works now. – Vivendi Mar 09 '14 at 12:37
  • Thanks for this answer. It work for me. I close the session, make a long process and restart session after to save information from long process in the session. – Juan Jul 27 '22 at 11:47