Skip to content
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

[Uid] Uuid v7 collisions #52338

Closed
vudaltsov opened this issue Oct 28, 2023 · 4 comments
Closed

[Uid] Uuid v7 collisions #52338

vudaltsov opened this issue Oct 28, 2023 · 4 comments

Comments

@vudaltsov
Copy link
Contributor

Symfony version(s) affected

6.3.0

Description

From time to time I get UUID v7 collisions.

How to reproduce

<?php

// generate_uuids_v7.php

use Symfony\Component\Uid\Uuid;

require_once __DIR__.'/vendor/autoload.php';

for ($j = 0; $j < 100; ++$j) {
    $uuids = '';

    for ($i = 0; $i < 1_000_000; ++$i) {
        $uuids .= Uuid::v7()->toRfc4122()."\n";
    }

    file_put_contents('uuids_v7.txt', $uuids, $j === 0 ? 0 : FILE_APPEND);
}
php generate_uuids_v7.php && sort uuids_v7.txt | uniq -cd

Expected behavior

Empty

Actual output (native PHP v8.2.10, macOS Ventura v13.3.1)

   2 018b73a2-4779-7f97-b675-5ce7ed6e1812
   2 018b73a2-9c97-7484-8a67-420747e8164a
   2 018b73a3-0f1b-7ac9-89dc-2b7f1e3625aa
   2 018b73a3-4da3-7bf5-bf20-aeaa68c6b109
   2 018b73a3-d9de-7850-9463-f4ee48ebfb02
   2 018b73a4-d6f4-7c55-a660-22114e5bf692
   2 018b73a5-f11b-7c99-884b-d741e7686016

Possible Solution

No response

Additional Context

No response

@vudaltsov
Copy link
Contributor Author

Originally posted in ramsey/uuid#518. Reproduced same issue in Symfony.

@OskarStark
Copy link
Contributor

@fancyweb do you have an idea?

@vudaltsov
Copy link
Contributor Author

For the record. Generating UUID v7 according to the standard (basically having smth like uuidFromTimeAndRandom(time_in_milliseconds(), random_int())) does not give any collisions. I checked that in ramsey/uuid v4.5.1.

So the problem is in the seed mechanism introduced by @nicolas-grekas here, which was also used by @ramsey in ramsey/uuid@0ba1ffb.

nicolas-grekas added a commit that referenced this issue Oct 31, 2023
…ekas)

This PR was merged into the 6.3 branch.

Discussion
----------

[Uid] Fix UuidV7 collisions within the same ms

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #52338
| License       | MIT

Off-by-one errors...

Commits
-------

e3bf65b [Uid] Fix UuidV7 collisions within the same ms
@vudaltsov
Copy link
Contributor Author

Checked, it fixed the problem, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants