Parallel request on the same PHP script causes a second long delay
<p><strong>Background</strong></p>

<p>This is something driving me crazy for weeks. I use MrClay's PHP Minify script to minify and concatenate my JS and CSS. It works great, all my files are merged into common.css and common.js, which are virtual files, translated to link to to the script like this:</p>

<pre><code>RewriteRule ^common.js$ /tynamic/min/?g=js [L,QSA]
RewriteRule ^common.css$ /tynamic/min/?g=css [L,QSA]
</code></pre>

<p>A query string is also appended to denote a version of these files, and they have a 3 year long caching, so a person that visited my site will likely never have to download any CSS or JS on any future visit to that site (if they don't change, obviously). So far everything works.</p>

<p><strong>The problem</strong></p>

<p>Often (sadly not always), when my browser asks for these 2 files (which is done at the same time), <strong>one of the files takes a second to get returned</strong>. Its always the one whose request arrives later to the server, so its <strong>usually</strong> the one which is later in the HTML, but its not a rule.</p>

<p>See these screenshots:</p>

<p><a href="https://www.dropbox.com/s/x69xcgjp1dxih64/delaygate_ff.png?dl=0">taken from my Firefox</a></p>

<p><a href="https://www.dropbox.com/s/c49xjoxtxbkv5nv/delaygate_pingdom.png?dl=0">test report by Pingdom</a></p>

<p>I'd be okay with the server putting the other file to a queue and proccesing it after the first one, but that doesn't take a whole second.
Few more things: <strong>no action like concatetion or gzipping is being performed</strong> in this case.. the script is only doing a fpassthru() of an existing pre-gzipped file. It doesn't happen all the time however.. there it gets a little bit odd, if I do a large number of consecutive pageloads, like 30 or more, it goes back to "normal" when both files are processed in a trivial time. Then when I check after some time, it is back on the second hang. <strong>The time is always something little over a second</strong>.</p>

<p><strong>What I already tried</strong></p>

<ol>
<li>Putting <code>if($_GET["g"]=="js") exit;</code> right at the start of the script.</li>
</ol>

<p>Thats right, that was of no help. The file was still delayed, outputting nothing. Just <code>exit;</code> (for both files) however works... :)</p>

<ol start="2">
<li>Timing the scripts</li>
</ol>

<p>Both runs report minimal times (units or tens of milliseconds) of their runs, so there is no function that would delay it.</p>

<ol start="3">
<li>Different server/hosting</li>
</ol>

<p>No help, 3 different servers and hosting providers. It is not hosting related.</p>

<ol start="4">
<li>Making a full copy of the script</li>
</ol>

<p>So I made a copy of the full script directory to ensure both runs are made by different set of files - no help.</p>

<ol start="5">
<li>Disabling file locking nad other tweaks to script config or the script itself.</li>
</ol>

<p>So far I didn't came up with anything :(</p>

<ol start="6">
<li>Different script - doing something else.</li>
</ol>

<p>This was interesting, modifying the files to do something else, e.g. do a scandir and pick a file <strong>didn't help either</strong>. Another analysis showed, that the PHP scripts are being assigned to free CPU threads <strong>every second</strong>. So if there are e.g. 5 threads, and 6 scripts need to be run at once, the first 5 are done in like 10 msecs, but the 6th has to wait a whole second to even start being proccessed... Why is this happening?</p>

<p><strong>Thanks a lot in advance for any effort put in to helping me</strong></p>