2

I use Symfony as a framework for a web application, and I have the problem of PHP locking sessions.

When I open a browser tab and I access a page of the application that processes a large amount of data (and it takes between 12 and 18 seconds), if I open another tab and access another page of the application lighter (such as the index , which loads in less than a second), the latter tab does not load until the previous tab has finished.

I think this is because: when I open the first page, the Symfony controller automatically opens the session, PHP locks the file where it stores the session, so, until this page does not finish processing, the controller closes the session and PHP release the file where the session is stored, the following pages (which share session) will not load.

I tried to change the PHP handler to Memcache and solved this error, but I would like to know if you know any simple way to avoid locking PHP sessions in Symfony when PHP stores sessions in files.

BenMorel
  • 30,280
  • 40
  • 163
  • 285
JSGarcia
  • 516
  • 4
  • 16

1 Answers1

2

This situation is not unique to Symfony. You can experience exactly the same thing in almost any other PHP based web app making use of sessions and parallel requests from the same client.

A prime example is WordPress where there are multiple plug-ins needing an update. If you click the AJAX update link on all the plug-ins at the same time they won't all update together in parallel. (The first plug-in you clicked update on needs to finish before the 2nd plug-in starts updating, etc.)

Likely duplicate: How to process multiple parallel requests from one client to one PHP script

Part of the problem that can be Symfony specific is that it really really wants to have a session available, because parts of the framework (such as the security bundle) require a session. When the session is file based you hit a limit because you can't have multiple write handles to the same file at any given time, which is a caveat imposed by the underlying operating system rather than PHP. -- This is not a problem with memory handles, which is why memcached solved this issue for you.

Is there any reason why you want to use file based sessions instead of memcached? There are plenty of reasons why memcached is a better choice, as discussed here: Session VS File VS Memcache for a Cache in PHP?

Adambean
  • 898
  • 7
  • 14