Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would I be seeing execution timeouts when setting $_SESSION values?

I'm seeing the following errors in my PHP error logs:

PHP Fatal error: Maximum execution time of 60 seconds exceeded in D:\sites\s105504\www\index.php on line 3
PHP Fatal error: Maximum execution time of 60 seconds exceeded in D:\sites\s105504\www\search.php on line 4

The lines in question are:

index.php:

01 <?php
02 session_start();
03 ob_start();
04 error_reporting(E_All);
05 $_SESSION['nav'] = "range";  // <-- Error generated here

search.php

01 <?php
02 
03 session_start();
04 $_SESSION['nav'] = "range";
05 $_SESSION['navselected'] = 21; // <-- Error generated here

Would it really take as long as 60+ seconds to assign a $_SESSION[] value?

The platform is:

  • Windows 2003 SP2 + IIS6
  • FastCGI for Windows 2003 (original RTM build)
  • PHP 5.2.6 Non thread-safe build

There aren't any issues with session data files being cleared up on the server as sessions expire. The oldest sess_XXXXXXXXXXXXXX file I'm seeing is around 2 hours old.

There are no disk timeouts evident in the event logs or other such disk health issues that might suggest difficulty creating session data files.

The site is also on a server that isn't under heavy load. The site is busy but not being hammered and is very responsive. It's just that we get these errors, three or four in a row, every three or four hours.

I should also add that I'm not the original developer of this code and that it belongs to a customer who's developer has long since departed.

like image 476
Kev Avatar asked Jun 08 '10 17:06

Kev


1 Answers

Sessions are blocking. session_start() will block execution until it can get an exclusive lock on that particular session's file. This is to prevent concurrency issues from cropping up. You can solve it by issuing a session_write_close() when you're done with the session in each script. The reason you're seeing it on the next line, is session_start() blocks for > than the max execution time. So when it returns, the next line is executed after the time limit, so that's where the error is raised.

See: session_write_close() for more information

Oh, and based on the errors you posted, the error is generated at the ob_start(); and the $_SESSION['nav'] = "range"; lines respectively, not the line 5 that you indicated in the code section...

like image 195
ircmaxell Avatar answered Oct 19 '22 23:10

ircmaxell