← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

max_execution_time & set_time_limit()

PHP PHP 4.0+ Beginner
debt(d3/e3/b3/t7)
d3 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'default linter catches the common case' (d3). The term's detection_hints list phpcs and phpstan as tools that can catch `set_time_limit(0)` patterns. These are commonly configured PHP tools that would flag the obvious misuse, though they won't catch the subtler misconception about CPU vs wall-clock time.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix indicates moving operations to CLI/queue workers and calling set_time_limit(60) per iteration in loops. This is a pattern-based refactor within the same component—not a one-liner, but also not cross-cutting architectural work.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The term applies to web, cli, and queue-worker contexts, but the fix pattern (using queue workers for long operations) is a localized architectural choice. It doesn't impose a persistent productivity tax across the codebase—once you establish the pattern of using background jobs, individual features follow it.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap' (t7). The misconception explicitly states that developers wrongly assume max_execution_time is wall-clock time when it only counts CPU time. This directly contradicts how similar timeout concepts work in other systems (e.g., HTTP timeouts, database timeouts are all wall-clock). A script waiting for I/O can run indefinitely, which violates reasonable developer expectations.

About DEBT scoring →

TL;DR

max_execution_time limits script CPU time (not wall-clock time) — use set_time_limit(0) sparingly for long-running CLI tasks, never for web requests.

Explanation

max_execution_time (default 30s) measures CPU time consumed by the PHP process, not wall-clock time. Blocking I/O (sleep, DB queries, HTTP requests) does NOT count. This means a script making many slow DB calls can run for minutes without hitting the limit. set_time_limit(N) resets the timer from the current point. CLI scripts have max_execution_time=0 (unlimited) by default. The fatal error it produces is uncatchable. For long operations: use background jobs/queues, call set_time_limit() in loops, or use CLI with time limits managed by the OS.

Common Misconception

max_execution_time is wall-clock time — it only counts CPU time. A script sleeping or waiting for I/O can run indefinitely regardless of the setting.

Why It Matters

Runaway scripts consuming CPU without hitting time limits can starve servers. Conversely, legitimate long-running imports may need explicit set_time_limit() calls.

Common Mistakes

  • Using set_time_limit(0) in web requests — allows infinite execution.
  • Assuming max_execution_time protects against slow DB queries.
  • Not using background jobs for operations known to exceed 30s.

Code Examples

✗ Vulnerable
// In a web controller:
set_time_limit(0); // Disable limit for 'safety'
foreach ($millionRows as $row) { processRow($row); }
✓ Fixed
// In a CLI command or queue worker:
set_time_limit(3600); // 1 hour for this operation

// Reset per chunk to avoid single-chunk timeout:
foreach (array_chunk($rows, 500) as $chunk) {
    set_time_limit(60); // 60s per chunk
    processChunk($chunk);
}

Added 22 Mar 2026
Views 48
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 1 ping W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 2 pings W 1 ping T 1 ping F 0 pings S 0 pings S 3 pings M 0 pings T 2 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings T 0 pings F 1 ping S 0 pings S 1 ping M 0 pings T 0 pings W
No pings yet today
No pings yesterday
Perplexity 7 Amazonbot 6 Scrapy 6 ChatGPT 4 Google 4 Unknown AI 3 Ahrefs 3 SEMrush 3 Claude 2 Bing 1 Meta AI 1 Sogou 1 PetalBot 1
crawler 35 crawler_json 5 pre-tracking 2
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Medium
⚡ Quick Fix
Move long operations to CLI/queue workers. In loops, call set_time_limit(60) per iteration. Never use set_time_limit(0) in web request handlers.
📦 Applies To
PHP 4.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
set_time_limit(0)
Auto-detectable: ✓ Yes phpcs phpstan
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Low ✗ Manual fix Fix: Medium Context: Function
CWE-400


✓ schema.org compliant