Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpFoundation]  Add lock in MockFileSession #39808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

jderusse
Copy link
Member

Q A
Branch? 4.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets Fix #39167
License MIT
Doc PR -

This PR locks the files generated by MockFileSession to provide atomic writes.

I'm not sure this should target 4.4.

note: By using flock, the drawback is: files are not deleted on destroy.
If this is an issue, we could provide a LockRegistry similar to Cache stamped, but the drawback would be the number of concurrent sessions (users) will be limited.

@carsonbot carsonbot added this to the 4.4 milestone Jan 12, 2021
@carsonbot carsonbot changed the title [HttpFoundation] Add lock in MockFileSession [HttpFoundation]  Add lock in MockFileSession Jan 12, 2021
@nicolas-grekas
Copy link
Member

nicolas-grekas commented Jan 12, 2021

Given that most session storages don't lock, I'd prefer using a temporary file instead to write atomically. Wdyt?

@jderusse
Copy link
Member Author

Given that most session storages don't lock, I'd prefer using a temporary file instead to write atomically. Wdyt?

I'm not sure to understand. The file have to be kept and reopened between each request..

If we don't care about atomicity, then, file_put_content with LOCK_EX + remove unlink should do the trick

@nicolas-grekas
Copy link
Member

See #39816

@mpdude
Copy link
Contributor

mpdude commented Jan 13, 2021

#39816 is a good shot at avoiding "hard" errors that can occur when the MockFileSession is used by several processes/requests concurrently: Avoid a warning when the session file on disk has been unlink()ed by another process, and avoid garbled data in the file when several processes try to write session data simultaneously and/or one process reads it while the other tries to write.

The more general issue of session locking to avoid soft/logic errors would require that once one process starts a session (= reads session data from disk, memory, whereever), that process would need to hold a write lock on the session data. All other processes trying to read the same session data would have to wait until the first process finished and wrote back the session. That's the way PHP's session_start() behaves. This is not a lock while session data is being read or written, but a lock held throughout a good part of the request's duration (!).

For a session implementation like the one in Symfony, that would probably be a bigger behavioral change, and it should work the same way for all kinds of session storage for consistency. I am not sure this is feasible.

Side note: IIRC, in the context where I reported #39167, we also used the Remember me feature from security, which (if memory serves me correctly) writes updated session data on every request and changes some internal token. Because requests may be interleaved, some of them read old session data or wrote back outdated tokens, eventually losing the "logged in"-state. This is the second, "logic" issue. #39816 is against the race condition when two processes work on the session file at the same time.

@jderusse jderusse closed this Jan 13, 2021
@jderusse
Copy link
Member Author

I'm not sure we need such advanced behavior in Symfony. Not all adapter provides locking.

People can use native php sessions if they want lockig.

nicolas-grekas added a commit that referenced this pull request Jan 14, 2021
…ge (nicolas-grekas)

This PR was merged into the 4.4 branch.

Discussion
----------

[HttpFoundation] use atomic writes in MockFileSessionStorage

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #39167
| License       | MIT
| Doc PR        | -

Instead of #39808

Commits
-------

5290e97 [HttpFoundation] use atomic writes in MockFileSessionStorage
@jderusse jderusse deleted the mock-session-lock branch January 25, 2021 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants