Skip to content

uuid1 generates identical UUIDs after a fork, even though it returns SafeUUID.safe #105337

@guillaumematheron

Description

@guillaumematheron

Bug report

Minimal reproductible example:

import os
from uuid import uuid1

print(uuid1(), flush=True)
os.fork()
u = uuid1()
print(u, u.is_safe)

Expected output: Either all UUIDs are different, or u.is_safe is not safe

Actual output:

774c65f2-03e1-11ee-a9b2-134f3c9f0156
774c65f3-03e1-11ee-a9b2-134f3c9f0156 SafeUUID.safe
774c65f3-03e1-11ee-a9b2-134f3c9f0156 SafeUUID.safe

If the initial call to uuid1() is omitted, or if using spawn instead of fork, then the bug is not triggered.

By following the code, the underlying function called seems to be uuid_generate_time_safe. I was not able to reproduce the bug outside of python by calling the C method directly, so I assume python sets a global state the first time uuid1() is called, and this state is copied when the fork occurs.

This issue was originally discovered in clickhouse-connect then discussed in the context of multiprocessing.

Documentation issue

This may require a separate issue, but the documentation of uuid1() states:

uuid.uuid1(node=None, clock_seq=None)

Generate a UUID from a host ID, sequence number, and the current time. If node is not given, getnode() is used to obtain the hardware address. If clock_seq is given, it is used as the sequence number; otherwise a random 14-bit sequence number is chosen.

This seems to be false on my platform since only a counter seems to be incremented between two calls to uuid1().

Environment

  • Python 3.10.6
  • Ubuntu 22.04 LTS 5.15.0-58-generic x86_64

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions