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
  • 34,448
  • 50
  • 182
  • 322
JSGarcia
  • 556
  • 7
  • 18
  • I had a similar issue and found a solution here: https://stackoverflow.com/questions/53012940/avoid-locking-session-php-in-symfony – Blafasel42 Jul 23 '21 at 20:42

1 Answers1

3

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
  • 1,096
  • 1
  • 9
  • 18
  • The answers in the linked thread are targeted at php being single-threaded. This is missing the point completely. The session lock is the problem and can be fixed with memcache or redis (or database) sessions. – Blafasel42 Jul 22 '21 at 10:06
  • Could you kindly point to where in my answer I said this is because PHP is single threaded? I was talking about exclusive file handles, and how that is a constraint from the underlying operating system. I also finished up by stating the problem can be resolved with memory based session storage. – Adambean Jul 22 '21 at 19:25
  • Your comment was correct, but you referred to another article in the link prefixed by "likely duplicate" and that contains mostly references to single-threadedness. Also: memcache session handling is now also blocking by default afaik. can be switched off though. – Blafasel42 Jul 23 '21 at 20:39