Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Cache] Fix filesystem cache collision#39786

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

@supersmile2009
Copy link

QA
Branch?4.4
Bug fix?yes
New feature?no
Deprecations?no
Ticketsn/a
LicenseMIT
Doc PRn/a

NOTE: this isn't a theoretical case but a real issue I had in a production environment.
Current filesystem cache write implementation leads to cache collision under certain conditions.
I'll try to explain the conditions that lead to it and the reasoning behind each change step by step.

  1. Once initialized,Symfony/Component/Cache/Traits/FilesystemCommonTrait::$tmp never changes. It means that if process is forked, both processes now use the same tmp file path to write to the cache. You don't really need that much concurrency to end up with a collision this way.
    The first fix iteration that comes to mind is removing theif condition here:

    if (null ===$this->tmp) {
    $this->tmp =$this->directory.uniqid('',true);
    }

    That would work better, but there's still a possibility of a race condition.

  2. As you knowuniqid() is based on timestamp in microseconds. With enough luck and concurrency you may get exactly the same value from it in different processes/threads. Themore_entropy argument is supposed to help with that, however it doesn't work very well if it had been used before forking.
    Check out this example:

<?phpecho uniqid('', true).PHP_EOL;$pid = pcntl_fork();if ($pid == -1) {    die('could not fork');} elseif ($pid) {    // we are the parent    echo uniqid('', true).PHP_EOL;    pcntl_wait($status); //Protect against Zombie children} else {    // we are the child    echo uniqid('', true).PHP_EOL;}

Output:

5ffc343b65a718.787463105ffc343b65dab3.197056775ffc343b7e6493.19705677

As you can see the 2nd and the 3rd line have the same "more entropy" value. The problem is that under the hood it's using anLCG algorithm. Once its constants are initialized, they never change and the sequence of generated numbers is pre-determined.
Thusuniqid() was replaced withbin2hex(random_bytes(40)).

  1. The change in the last step should be sufficient for the majority of use cases, however the statefulness of this code (i. e.$this->tmp) still leads to problems when used with coroutines (Swoole) and presumably - threads (I have no experience with multi-threading solutions in PHP, maybe they do some magic around it to prevent this kind of issue).
    As a result,$tmp property was replaced with a local variable.

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has acontribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (seehttps://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (seehttps://symfony.com/releases)
  • Features and deprecations must be submitted against the 5.x branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@supersmile2009supersmile2009force-pushed thebugfix/filesystem-cache-collision branch from2844a6f to185c17aCompareJanuary 11, 2021 15:23
trait FilesystemCommonTrait
{
private$directory;
private$tmp;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This is a BC break isn't it?

@nicolas-grekas
Copy link
Member

Thanks for raising the issue and sending a PR!
I started with a review but decided writing a PR would be more efficient for both of us.
See#39788

nicolas-grekas added a commit that referenced this pull requestJan 12, 2021
…lesystem adapter (nicolas-grekas)This PR was merged into the 4.4 branch.Discussion----------[Cache] fix possible collision when writing tmp file in filesystem adapter| Q             | A| ------------- | ---| Branch?       | 4.4| Bug fix?      | yes| New feature?  | no| Deprecations? | no| Tickets       |Fix#39786| License       | MIT| Doc PR        | -Commits-------340d15e [Cache] fix possible collision when writing tmp file in filesystem adapter
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@OskarStarkOskarStarkOskarStark left review comments

Assignees

No one assigned

Projects

None yet

Milestone

4.4

Development

Successfully merging this pull request may close these issues.

4 participants

@supersmile2009@carsonbot@nicolas-grekas@OskarStark

[8]ページ先頭

©2009-2025 Movatter.jp