Nuno
Nuno

Reputation: 3632

PHP hanging when printing minified javascript, only for specific users

Here's a strange one, that I'm still trying to figure out.

In order to fix LCP in Core Web Vitals, I added JavaScript (130Kb) inline in the <head> (hardcoded in the PHP file itself), rather than having the browser load from a separate file (which is what usually should be done, but LCP is unforgiving!).

However, since then, I've been having random logs of "MySQL gone away" (in whatever query happens in the body of the page), and before I had connected the dots, I had a long way of several releases until I figured out this was connected with added the inline JS.

It's because that specific spot was sometimes (randomly) taking over 300 seconds to send, which is the timeout config of MySQL.

When I finally figured out the spot, I put a StopWatch before and after, and to write a log if it takes over 2 seconds.

Aaaannnddd..... I've been getting loads of logs every day, of the printing of that inline JS taking 2 seconds, 42 seconds, 7 seconds, 115 seconds, 15 seconds, ........... totally random.

BUT... surprisingly, this only happens either on IP addresses that are VPN/Proxy, or a bot like Googlebot or Sagou (Chinese search engine). This does not seem to be happening with real users, as far as I can see (and I have many users 24 hours a day).

I disabled output_buffering and zlib.output_compression, and made Apache do DEFLATE instead, in case it was OB/gzip that was causing conflicts with the minified JS...
But no... still happening. :(

I put the inline JS into a separate inline-script.php file, and required it in the original php file. I tried this instead of readfile just so the contents are cached in opcache, instead of hammering the disk :)
But no... still happening.

Alright... save it in memcache (runs on the same host as PHP) and print it...
SOLVED!!!


Does anyone know what's happening here??

And why does this happen mostly/only with bots/crawlers?

Is it their connection that is slow? I wouldn't think PHP would be influenced by the user connection, and would assume that Apache is the one that deals with the user connection??

I can't understand how this can be anything in the contents of the inline JS, conflicting with PHP syntax or so, because otherwise it would happen with all users, and not just some?

It can't be the connection either, because PHP is still effectively sending the exact same bytes out, just now fetching from memcache. Apache and the user are still receiving the exact same output!

I'm glad I seem to have mitigated the issue, finally, but I'm very confused about what the cause is... 🤯

Upvotes: 3

Views: 66

Answers (0)

Related Questions