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

Safe UUID - is not safe. #118241

Open
cfrademan opened this issue Apr 24, 2024 · 1 comment
Open

Safe UUID - is not safe. #118241

cfrademan opened this issue Apr 24, 2024 · 1 comment
Labels
type-bug An unexpected behavior, bug, or error

Comments

@cfrademan
Copy link

cfrademan commented Apr 24, 2024

Bug report

Bug description:

The following works as expected.

import uuid
import multiprocessing

def worker(_):
    # Happy days.. she is unique...
    print(uuid.uuid1())

with multiprocessing.Pool(4) as p:
    r = p.imap_unordered(worker, list(range(4)))
    for x in r:
        _ = x

output:

chris@bounce:~/code$ python3 working.py
1f791abc-026b-11ef-aeb4-d3e6c19eeb6e
1f791de6-026b-11ef-aeb5-d3e6c19eeb6e
1f792836-026b-11ef-aeb5-d3e6c19eeb6e
1f792912-026b-11ef-aeb6-d3e6c19eeb6e
chris@bounce:~/code$

However, just 1 line, by issuing a unique uuid1 before the forking process, breaks it...

import uuid
import multiprocessing

# NOTE(cfrademan): Chaos. (such is life)
break_it = uuid.uuid1()

def worker(_):
    # She is not unique :-(
    print(uuid.uuid1())

with multiprocessing.Pool(4) as p:
    r = p.imap_unordered(worker, list(range(4)))
    for x in r:
        _ = x

output:

chris@bounce:~/code$ python3 broken.py
352a17db-026b-11ef-aeb6-d3e6c19eeb6e
352a17db-026b-11ef-aeb6-d3e6c19eeb6e
352a17db-026b-11ef-aeb6-d3e6c19eeb6e
352a17db-026b-11ef-aeb6-d3e6c19eeb6e

It seems like a global state being passed over to a forked process from the C side.

Our workaround is to ensure we don't hit this. We run os.getpid() on every call on our UUID wrapper to raise a RunTimeError if one of our devs decides to use our wrapper before any processes are forked. Luckily, we had this wrapper to check that the UUID was safely generated.

If this is expected behaviour, it should be documented for whatever reason.

My recommendation is that UUID should have a keyword argument to raise if not safely generated. Now we have a wrapper that has to do two more things than it should.

No rush, but if I have any time available, I would help on this.

Many thanks!

CPython versions tested on:

3.10

Operating systems tested on:

Linux

@colesbury
Copy link
Contributor

This looks like a duplicate of the issue you linked to #105337

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants