3

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.

1

1 Answer 1

4

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?

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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.
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.
While this gives a workaround for the session locking, I think it completely misses the point. Session locking is not a technical limitation of files, instead it is a question of semantics. The session locking of PHP ensures that all requests have a consistent view of the session data. Otherwise it could be that a request works with outdated data if another request writes to the session.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.