Why does PHP occasionally hang on session_start()

Why does PHP occasionally hang on session_start()

August 7, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Why does PHP occasionally hang on session_start()

Why PHP Occasionally Hangs on session_start(): A Comprehensive Analysis

Introduction

Occasional hangs when calling session_start() in PHP can be perplexing, especially when the default session handler is used with files stored on an ext4 filesystem. This issue seems to occur once per month on Ubuntu 12.04 systems running Magento and PHP-FPM (5.4).

The Hang Incident

According to the fpm-slow.log, all PHP processes occasionally hang at session_start(). Here is a breakdown of one such incident:

[24-Sep-2014 11:03:04]  [pool www] pid 24259
script_filename = /data/web/public/index.php
[0x00007f00b4ec6480] session_start() /data/web/public/includes/src/__default.php:7687

Process Analysis

Lsof output reveals that multiple PHP processes are open on the same session file with write locks:

COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF    NODE NAME
php5-fpm 24259  app   10uW  REG  202,1    82492 1220594 /data/web/public/var/session/sess_gr2clur9icgd7s2j9linag7ue6
php5-fpm 24262  app   10u   REG  202,1    82492 1220594 /data/web/public/var/session/sess_gr2clur9icgd7s2j9linag7ue6

Strace shows that these processes are waiting for flock (LOCK_EX), even the one with a write lock already acquired. This suggests a potential deadlock or race condition.

Possible Causes and Debugging Steps

Race Condition with AJAX

The requests triggering this issue are consistently AJAX calls, which could potentially create a race condition if not handled properly. While the article Race condition with Ajax and PHP sessions suggests that the default session handling avoids this problem, it’s worth exploring further.

Session File Locking

PHP uses a locking mechanism to ensure session data integrity. When multiple processes access the same session file simultaneously, they acquire locks:

session_start();

However, if the lock is not released properly or conflicts occur with other concurrent processes trying to access the same file, it can lead to hangs.

Ext4 File System and Session Files

Ext4 filesystems use a journaling system that could potentially cause delays in file locking. While this is less likely to be the primary culprit given the occasional nature of the hang, it’s worth considering as a contributing factor or an area for further investigation.

Conclusion

The hanging issue described here appears to involve a complex interplay between multiple PHP processes, session locking mechanisms, and possibly AJAX requests creating race conditions. While the default session handling should avoid these issues according to the literature, the specific circumstances of this case suggest there may be additional factors at play.

To debug further:

  1. Monitor system logs for any other indicators of high load or contention around file operations.
  2. Check if the session file directory has any unusual properties that could affect locking behavior (e.g., mount options, permissions).
  3. Consider implementing a more robust session handling mechanism, such as using a database instead of files, to eliminate file-level concurrency issues.

Remember to always check the official PHP documentation for the most up-to-date information on session handling best practices and potential solutions to this issue.

Performance Errors

Our network