An import script that processes a few thousand rows starts with the traditional incantation:
set_time_limit(0);
ini_set('memory_limit', '512M');
It runs fine locally. On the server it dies partway through — no PHP fatal error, no entry in the PHP error log, sometimes a 502 or 504 in the browser, sometimes a truncated response. Raising the limit changes nothing, because the limit you raised is not the one doing the killing.
There are at least three independent clocks on a request, owned by three different processes.
Clock 1: PHP's own execution limit
max_execution_time, which set_time_limit() sets at runtime.
The manual is precise about what it measures, and it is not what most people assume:
The
set_time_limit()function and the configuration directivemax_execution_timeonly affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls usingsystem(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running. This is not true on Windows where the measured time is real.
So on Linux this clock does not tick while you are waiting on MySQL, reading a file, or calling an API. An import that spends 95% of its wall-clock time in database round-trips can run for ten minutes and register barely any "execution time" at all.
That is why the symptom is confusing in both directions: scripts that should hit the limit do not, and scripts that are nowhere near it die anyway.
Clock 2: PHP-FPM's request timeout
request_terminate_timeout, set per pool in the FPM config. From the manual:
The timeout for serving a single request after which the worker process will be killed. This option should be used when the 'max_execution_time' ini option does not stop script execution for some reason. A value of '0' means 'Off'.
Read that middle sentence again. It exists precisely to override the case where max_execution_time will not stop a script — which includes the case where the script asked for no limit at all.
Two things follow:
-
It is wall-clock, and it is enforced by the FPM master, not by your process. Your script has no say.
set_time_limit(0)is a request you make of the PHP engine;request_terminate_timeoutis a decision made about you by the process manager. -
The kill is a
SIGTERMto the worker. No PHP fatal error, no shutdown functions in the normal case, nothing in the PHP error log. What you get is in the FPM log, along the lines ofexecution timed out (31.05 sec), terminating.
The default is 0 — off. So this is not something PHP does to you out of the box; it is something your host does, and shared hosts and managed platforms very often do it. If you do not control the FPM pool config, you cannot raise it.
Since PHP 7.3 there is also request_terminate_timeout_track_finished, which extends the same watchdog over work done after fastcgi_finish_request() — worth knowing if your plan was to close the connection and keep working.
Clock 3: the web server in front
nginx has fastcgi_read_timeout (default 60s). Apache with mod_proxy_fcgi has ProxyTimeout; with mod_fcgid, FcgidIOTimeout. When this one fires first, nginx gives up on the response and returns 504 Gateway Time-out — while your PHP script is still happily running, because nothing told it to stop. You then get a half-finished import and a browser error, which is the worst of both.
Cloudflare adds a fourth on the public path (524 after 100 seconds on the free plan), which is why "it works from the office VPN but not from outside" is a real report.
The diagnosis, in order
-
Is there a PHP fatal error in the PHP error log?
Maximum execution time of N seconds exceededmeans clock 1 — this one you can raise. -
Nothing in the PHP log, but the worker died? Check the FPM log for
execution timed out ... terminating. That is clock 2. - 502/504 from the web server while the work appears to keep going? Clock 3.
-
Enable
request_slowlog_timeoutin the pool and readslowlog. It dumps a PHP backtrace of what the request was doing when it went past the threshold, which is the single most useful thing available here and almost nobody turns it on.
The actual fix
Stop trying to make one HTTP request last longer. You are arguing with three different owners, one of whom you do not control, and even if you win the request is still hostage to the visitor's browser and any proxy in between.
Batch it instead. The shape:
- Process a fixed number of items per request — small enough that the slowest plausible batch finishes well inside the tightest timeout. We use 200 rows as a default for import work, which comfortably fits a 30-second budget.
- Record progress durably: a cursor, a "last processed id", a status row.
- Return, and have the client come back for the next batch. A button the admin presses again, a
<meta refresh>, a fetch loop in the admin page, or a cron job that takes one batch per run. - Make the operation idempotent and resumable, because it will be interrupted eventually — a deploy, a restart, a closed laptop.
The pleasant side effect is that the merchant gets a progress indicator instead of a spinner, and an interruption costs one batch rather than the whole job.
If the work genuinely must be one long-running unit — a migration, a full reindex — take it off the web entirely and run it from CLI. The CLI SAPI has max_execution_time = 0 by default and there is no FPM and no web server in the path. That is not a workaround; it is what CLI is for.
The one-line version
set_time_limit(0) disables PHP's limit, which on Linux was not counting your database time anyway, and has no authority over the process manager that is actually going to kill you. If you cannot change the FPM pool config, the only real answer is to stop needing a long request.
From maintaining around sixty PrestaShop modules at MEG Venture, where "the import stops halfway on my host" is a recurring ticket with the same three causes.
Top comments (0)