0

PHP throw errors at random lines

error message:

PHP Fatal error: Maximum execution time of 1 second exceeded 

in code we don't have any setting, which sets execution time to one second. And this error occurs on completely random lines. sometimes on simple if statement, sometimes on db query. I can't find any pattern. Maybe someone had similar problem, and had resolved it ?

iehrlich
  • 3,572
  • 4
  • 34
  • 43
dpa
  • 427
  • 4
  • 16
  • 1
    Check your `php.ini`, it probably has been set in there to 1 second. – Hatted Rooster Jul 11 '17 at 11:17
  • Do you have long-running loops in your programs? Do you call out to external programs that might take time? Do you do *anything* that might take a long time (well obviously you *do* but you need to locate those places)? Perhaps your database access should happen in smaller steps, not fetching thousands or more rows at once? – Some programmer dude Jul 11 '17 at 11:17
  • @SomeProgrammerDude I don't think OP wants to resolve that scripts take longer than 1 second to run, but just wants to remove the time limit. – Hatted Rooster Jul 11 '17 at 11:18
  • yes, we have long-running parts in our code. And at most case they run successful, and take much more than 1 second. Problem, that I can't locate anywhere 1 second limit. And if I testing, script can run 300 seconds (this limit is set in php.ini). But still in logs we get some random errors, and page respond with 500 http code. – dpa Jul 11 '17 at 11:23
  • try ini_set('max_execution_time', 300); //300 seconds = 5 minutes – Syed Daniyal Asif Jul 11 '17 at 11:29
  • and check may be some where it is defined for 1 second – Syed Daniyal Asif Jul 11 '17 at 11:29

1 Answers1

1

And this error occurs on completely random lines

It's normal: PHP starts executing code, then fails after one second. So it can be any line, because it does not execute at the same speed all the time (think about I/O, memory, processor load, etc.)

Maybe someone had similar problem, and had resolved it ?

It's a common problem. If you want to fix it you can either:

  • Find where is the actual php.ini file that is user by your server/cli, find the line about max_execution-time and change the value to something like 30. Then reload/restart your server.
  • Or you can add a first line on the first included file with ini_set('max_execution_time', 30); just after the <?php tag. If there is no included file, then add this at the beginning of all your files.

More information:

rap-2-h
  • 30,204
  • 37
  • 167
  • 263
  • I checked max_execution_time setting, and I am sure that issue is not there. It happens not with every request. – dpa Jul 13 '17 at 14:22