Two simultaneous AJAX requests won't run in parallel

Solution 1:

Sounds like a session blocking issue

By default PHP writes its session data to a file. When you initiate a session with session_start() it opens the file for writing and locks it to prevent concurrent edits. That means that for each request going through a PHP script using a session has to wait for the first session to be done with the file.

The way to fix this is to change PHP sessions to not use files or to close your session write like so:

<?php
    session_start(); // starting the session

    $_SESSION['foo'] = 'bar'; // Write data to the session if you want to

    session_write_close(); // close the session file and release the lock

    echo $_SESSION['foo']; // You can still read from the session.

Solution 2:

After a bit of hair-pulling, I found one other way that these non-parallel AJAX requests can happen, totally independent of PHP session-handling... So I'm posting it here just for anyone getting here through Google with the same problem.

XDebug can cause this, and I wouldn't be surprised if Zend Debugger could too.

In my case, I had:

  • XDebug installed on my local LAMP stack
  • xdebug.remote_autostart enabled
  • My IDE accepting inbound debugger-connections, even though no breakpoints were active

This caused all my AJAX tests to run sequentially, no matter what. In retrospect it makes a lot of sense (from the standpoint of debugging things) to force sequential processing, but I simply hadn't noticed that my IDE was still interacting behind-the-scenes.

After telling the IDE to stop listening entirely, parallel runs resumed and I was able to reproduce the race-condition I had been looking for.