Is ini_set('max_execution_time', 0) a bad idea?

Is ini_set('max_execution_time', 0) a bad idea?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Is ini_set('max_execution_time', 0) a Bad Idea?

Setting ini_set('max_execution_time', 0) disables the standard PHP timeout, allowing scripts to run indefinitely. While this solves immediate issues with slow complex processing, it fundamentally alters how your server handles resource exhaustion. The decision isn't binary; it depends on whether your infrastructure has other layers of defense and what specific application behaviors you are trying to accommodate.

The primary purpose of max_execution_time is to prevent poorly written scripts from tying up the server. By default, this value is often 30 seconds. If a script runs longer than that, PHP sends a "504 Gateway Timeout" error immediately, killing the process. When you set this to 0, you are removing this specific check within the PHP runtime itself.

However, disabling the PHP timeout does not mean your request will run forever. As the documentation notes, web servers have their own timeouts. Apache has a Timeout directive, and IIS has a CGI timeout function. On many shared hosting environments or default configurations, these server-level timeouts often default to 300 seconds (5 minutes). Even if you set PHP's internal timer to infinity, the web server will eventually abort the connection if data isn't flowing back within its window.

There is a subtle but critical distinction between "running indefinitely" and "having no limit." If your application performs a massive calculation that takes 10 minutes, ini_set('max_execution_time', 0) allows it to finish. However, if your Apache Timeout directive remains at the default 300 seconds, that same script will still crash after 5 minutes with an error like "Client timed out." To truly ensure a long-running process completes, you must coordinate the PHP setting with the web server configuration.

The Risk of Infinite Loops

The most significant danger of disabling max_execution_time globally is not just slow scripts, but infinite loops or unhandled exceptions that prevent output generation.

Consider this scenario: A developer fixes a bug where a script hangs during a database transaction by setting the timeout to 0. They do not fix the underlying logic error causing the hang. Now, any request to that endpoint consumes an entire PHP worker process indefinitely because there is no internal timer to interrupt it. If the web server's timeout hasn't been adjusted accordingly, the user gets a generic "504 Gateway Timeout" after the server-level limit hits, but the resources remain tied up until the process is forcefully killed by the operating system or a restart cycle.

In high-traffic environments, this leads to resource starvation. One hung script can exhaust available worker slots in Apache or Nginx, causing legitimate requests to fail with "502 Bad Gateway" simply because there are no free processes left to handle them.

Where to Apply the Change

Using ini_set('max_execution_time', 0) directly in your application code is generally discouraged for global enforcement. This function only affects the current scope and can be overwritten by server configurations or subsequent settings. If you need to change this limit, it should ideally be done at the configuration level (e.g., in php.ini, .user.ini, or via an Apache php_value directive) rather than inside every script.

If you must do it programmatically, restrict the scope:

// Only in specific controllers or jobs that handle long tasks
if ($isLongRunningTask) {
    ini_set('max_execution_time', 0);
}

For frameworks like Laravel, this is often handled via service providers or middleware for specific request types (like CLI commands or scheduled tasks), rather than a global blanket setting.

The Real Solution: Asynchronous Processing

If you are dealing with complex processing that exceeds reasonable timeouts (whether 30 seconds or 300 seconds), the architectural pattern should be async, not just disabling timers. Offloading heavy lifting to a queue system ensures that long-running tasks don't block web server threads.

You can configure this in your app/Console/Kernels.php or via artisan commands:

// In Laravel, handle long tasks outside the request lifecycle
Artisan::queue('ProcessHeavyData', function ($event) {
    // Your heavy logic here
});

By moving logic to a queue (like Redis, RabbitMQ, or Beanstalkd), you decouple the user's HTTP request from the background processing time. The user gets an immediate response, and the server doesn't risk hanging on a single slow job.

Summary of Trade-offs

Disabling max_execution_time removes a safety net. It shifts the responsibility entirely to your web server configuration and operational monitoring. If you disable it:

  1. Adjust Web Server Timeouts: Ensure Apache's Timeout directive is increased if you expect runs longer than 5 minutes.
  2. Monitor Resource Usage: You need better observability to catch processes that hang indefinitely without generating output.
  3. Scope the Change: Avoid doing this globally for every request unless your infrastructure is specifically tuned for it.

Unless you have a very specific need and have hardened your server's other timeout layers, keeping max_execution_time enabled provides a necessary fail-safe against runaway scripts.

Our network