No php script can be run on server while there is another long-lasting php script running
I have a long-lasting PHP script running on server. It streams video to video tag from server (don't ask me why I do it :-) ). It can run for tens of minutes.
The problem is that at the same time I want to send the request to server using AJAX and it doesn't happen before the long script stops.
I've investigated the issue a bit more and I found out strange things.
- While the long-lasting PHP script is running I can not call any PHP scripts (either AJAX or not) from the same browser.
- Meanwhile from other browser server is accessible without problem.
- Non-php assets (images, videos) can be accessed without problems.
- In case video is played directly from .mp4 file the problem doesn't exist. AJAX requests to php can be processed while video is played.
I'm curious what could cause this issue. From what I see it could be combination of server and browser configuration.
My question is why it happens and how can it be solved? I want to be able do the AJAX requests to the server while other PHP script is running in the same browser.
Web server is Apache 2. It was reproduced in IE and Chrome.
Ajax request is sent using just normal jQuery's $.ajax() call.
Here is the request and response information for the video request that blocks AJAX request.
Request GET /portal/portlet/index/id/671/call/proxyVideo?entityId=1 HTTP/1.1 Accept */* If-Unmodified-Since Fri, 24 Apr 2015 08:18:29 GMT If-Match "f91b54d-514740a87ff40" Range bytes=360448-261207372 User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko Referer http://mm.mac/content GetContentFeatures.DLNA.ORG 1 Pragma getIfoFileURI.dlna.org Accept-Language en-US Accept-Encoding gzip, deflate Host mm.mac Connection Keep-Alive Cache-Control no-cache Cookie PHPSESSID=41821op72k2s24mfrsbn5gad64 Response HTTP/1.1 206 Partial Content Date Tue, 01 Dec 2015 13:39:31 GMT Server Apache/2.4.16 (Unix) PHP/5.6.13 X-Powered-By PHP/5.6.13 Expires Thu, 19 Nov 1981 08:52:00 GMT Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma no-cache Last-Modified Fri, 24 Apr 2015 08:18:29 GMT ETag "f91b54d-514740a87ff40" Accept-Ranges bytes Content-Length 260846925 Access-Control-Allow-Methods GET Content-Range bytes 360448-261207372/261207373 Keep-Alive timeout=5, max=100 Connection Keep-Alive Content-Type video/mp4
This issue could be caused by a session lock. When the long lasting php script uses sessions with session_start(), the process locks the session file on the server till it is finished. Blocking all other PHP processes that are trying to open the same session file.
This is why you see this behaviour in the same browser, but not on another machine or different browser (since the session is different).
The solution to your problem is to use
Whenever you do not need to write to the session. You can still read from the session variables when you called this function but for another write to a session variable you need to open the session again using session_start().
You can read a lot of this problem here.
An example: (from the linked page)
// start the session session_start(); // I can read/write to session $_SESSION['latestRequestTime'] = time(); // close the session for writing session_write_close(); // now do my long-running code. // still able to read from session, but not write $twitterId = $_SESSION['twitterId']; //when you want to write again do session_start() before and close after.
Only 1 PHP script can access the same user session at a time , all other scripts will wait until the first script complete.
This is becouse session file write lock.
- do no use session if you do not need one (in your long script) or
- Call session_write_close () as soon as possible to unlock session file http://php.net/manual/en/function.session-write-close.php