Long polling locking up other AJAX calls

I’m looking to do long polling to “push” some data down to the client and I’m also making other unrelated AJAX calls to the server in parallel with the long polling. It appears that my other AJAX calls won’t complete until the long poll has received a response (either from a response or timeout). When I step through the Javascript, it appears that the 2nd AJAX request is being sent at the proper time, but the response isn’t being received until the long poll request gets a response. Any idea what is going on?

Here is the code for the long polling portion:

Server side:

    function getPlaylistTracksIfChanged($playlist_id, $numClientTracks) {
  $reportChange = false;
  for($i = 0; $i < 10; $i++) {
   $numServerTracks = $this->PlaylistTrack->find('count', array(
     'conditions' => array('playlist_id' => $playlist_id)

   if($numClientTracks != $numServerTracks) {
    $reportChange = true;




  if($reportChange) {
   $playlist_tracks = $this->PlaylistTrack->find('all', array(
    'conditions' => array('playlist_id' => $playlist_id),
    'order' => array('PlaylistTrack.position') 

   $this->set('playlist_tracks', $playlist_tracks);
   $this->layout = false;

  } else {
   $this->autoRender = false;
   return 'false';


Client side:

function checkForChangesOnServer() {
 $.post('/getResultsIfChanged/' + playlist_id + '/' + $('#sortable_tracks').children().size(), function(results) {

  if(results == 'false') {
   //alert('no change');
  } else {



And a sample of another AJAX call:

Server side:

    function getLibraryTracksStartingWithLetter($user_id, $letter) {
  $results = $this->Track->find(
    'conditions' => array(
     'user_id' => $user_id,
     'OR' => array(
      'Track.artist LIKE' => $letter . '%',
      'Track.name LIKE' => $letter . '%'
    'order' => array('case when Track.artist = "" then 1 else 0 end', 'Track.artist', 'Track.name')

  $this->set('results', $results);
  $this->layout = false;

Client side:

    function loadLibraryResultsForLetter(letter) {

 $.post('/getLibraryTracksStartingWithLetter/' + user_id + '/' + letter, function(results) {

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Seems like you experienced the session file lock.

Perform session_write_close() (or corresponding function in cakephp) to close the session in the begin of the ajax endpoint.

Solution 2

This happens because of session file locks. In CakePHP you can choose other options for session management. You can save sessions in database, in cache etc. So you don’t wait for file lock issues.

The built-in configurations are:

php – Saves sessions with the standard settings in your php.ini file.

cake – Saves sessions as files inside app/tmp/sessions. This is a good
option when on hosts that don’t allow you to write outside your own
home dir.

database – Use the built-in database sessions.

cache – Use the built-in cache sessions.


Of course you can make session_write_close(), but you should be sure that no changes needed in session between two page loads.

Similar question: Simultaneous Requests to PHP Script

Solution 3

it works for symfony 1.4. user session_write_close(); instead of $this->getUser()->shutdown();

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply