Magento 2 dev returns random 500s from file session locking + mod_fcgid timeout
Symptoms
Section titled “Symptoms”- The dev/staging site sometimes shows Apache’s “Internal Server Error” page (769 bytes) when opening admin or reloading a frontend page. Random, hard to reproduce.
- In the browser: the main page returns 200, then a few AJAX calls (
customer/section/load, recommender AJAX, menu…) return 500. - Magento logs nothing: no exception, no
var/report, cleansystem.log/exception.log. - Apache
error_logshows:
[fcgid:warn] mod_fcgid: read data timeout in 31 secondsEnd of script output before headers: index.phpEnvironment
Section titled “Environment”- Magento 2.4.9 CE + Hyvä, developer mode on dev and staging (production in production mode).
- Apache + mod_fcgid +
php-cgi8.3 (shared Virtualmin server),FcgidIOTimeout= 31 s. - Dev/staging sessions:
save = files(var/session/). Production: Redis withdisable_locking = 1.
Investigation
Section titled “Investigation”| Hypothesis | Checked with | Result |
|---|---|---|
| PHP/Magento error (exception, fatal) | var/log/*, var/report/, trace added in the cache adapter |
Ruled out: PHP always finishes with 200, only Apache returns 500 |
| AJAX “Failed to write tag file” error (empty tag in the Symfony cache adapter) | Trace in Symfony::save() / FilesystemTagAdapter::onSave() |
Real, but a different bug (old dev code), gone after deploying new code; the 500s remained |
| Full swap | free -m on this server and other dev servers |
Ruled out: every dev server sits at ~100% swap, it is normal |
| Empty opcache after fcgid kills idle processes | Log opcache_get_status()['opcache_statistics']['num_cached_scripts'] at request start |
Ruled out: only ~0.4 s extra per request, far from the 31 s limit |
DI compile / generated/ |
Simulated deploy: delete generated/ + caches + kill processes |
Causes real 500s, but the natural incident happened with generated/ intact → not the root cause |
| Slow database | Slow query log | Ruled out for the failing requests (only the night crons have 20–36 s queries) |
| Slow AJAX holds the file session lock, later requests queue | Timing marks in pub/index.php + App\Http::launch(), logging pid and ms per phase |
Confirmed: the slow AJAX runs 25–30 s; requests with the same cookie stall between area_di_loaded → frontcontroller_ready (session start), each one starting within a few ms of the previous one finishing |
| Can a slow AJAX block the main page too? | Run the slow AJAX, 1 s later request / with and without the same cookie |
Yes: / from FPC (normally ~10 ms) waited 7.1 s and resumed in the same ms the AJAX finished; without the cookie it did not wait |
Reproduction (run on the server, $A = htpasswd user:pass if any):
J=$(mktemp); H=https://dev.client-a.com; T=$(date +%s%N)curl -sk -u "$A" -c $J -o /dev/null "$H/" # get a session cookiereq(){ curl -sk -u "$A" $2 -o /dev/null -w "%{http_code} %{time_total}s $3\n" --max-time 90 "$H$1"; }req "/<slow-ajax-url>?_t=$T" "-b $J" slow_ajax &sleep 1req "/" "-b $J" main_same_cookie & # has to wait for the AJAXreq "/?_t=x$T" "" main_no_cookie & # does not waitwait; rm -f $JRoot cause
Section titled “Root cause”Three conditions at once:
- Sessions are locked. The PHP
filessession handler takes an exclusive lock (flock) onsess_<id>for the whole request. Every request with the same cookie from one browser (AJAX calls and the next page load) runs sequentially. - One request is very slow. The recommender AJAX loads 100 products with a 5 s block cache, so it rebuilds almost every time: 10–30 s.
- Short timeout. mod_fcgid waits only 31 s. A queued request that has not sent a byte yet is cut off and Apache returns its own 500. PHP does not know it was abandoned, keeps running and ends with 200 → Magento logs nothing.
Reloading or navigating away does not help: the browser cancels the AJAX calls, but PHP on the server keeps running and keeps the lock, so the new page queues behind them and can 500 too.
Production is not affected because its sessions are in Redis with disable_locking = 1. Ordinary sites with file sessions are fine too: with AJAX calls under 1 s, queuing only adds a little latency.
- Root fix: speed up the slow AJAX (longer block cache, fewer products).
- Dev/staging: use Redis sessions without locking, like production:
bin/magento setup:config:set --session-save=redis \ --session-save-redis-host=127.0.0.1 --session-save-redis-port=<PORT> \ --session-save-redis-db=<DB> --session-save-redis-disable-locking=1- Mitigation: a sequential warm-up (curl admin, homepage, the AJAX endpoints) at the end of
deploy.sh, before anyone visits.
Prevention & how to recognise it next time
Section titled “Prevention & how to recognise it next time”- 500 + no Magento log +
read data timeout in N secondsin the Apache log → think web-server timeout first, not a PHP error. - Failing requests start back-to-back (a few ms apart) → session lock.
- Compare with and without the cookie: fast without the cookie → session lock.
- Staging should mirror production (sessions, mode, merge/minify) to avoid “staging-only” bugs.
- Admin uses its own cookie (
admin), so frontend AJAX cannot block it; an admin 500 is a different case (first request on a cold cache, ~21 s, close to the limit).