Skip to content

fix(generator): makes generator state fork- and thread-safe - #73

Merged
wgordon17 merged 2 commits into
mainfrom
fix/fork-thread-safety
Oct 30, 2026
Merged

wgordon17 merged 2 commits into
mainfrom
fix/fork-thread-safety

Conversation

@wgordon17

@wgordon17 wgordon17 commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Makes Cuid, create_id(), cuid_wrapper(), and utils.create_counter() thread-safe with re-entrant locks (so a signal handler or finalizer that generates an ID can't deadlock): on free-threaded 3.14t the unlocked counter returned ~323k–380k unique values from 1.6M calls and the lazy default generator was built up to 8 times per first-call race (also on GIL builds)
  • Makes generators fork-safe without os.register_at_fork: each Cuid keeps per-process state that the first generate() in a new process rebuilds exactly once (fresh lock, reseeded RNG, counter, and child-PID fingerprint), covering forks that skip Python's fork hooks such as uWSGI's default mode; copy.deepcopy() now returns an independent generator
  • Adds free-threaded 3.14t and 3.15t CI jobs and the Free Threading :: 3 - Stable classifier, plus regression guards that fail if a CI version ≥ 3.14 lacks a t job or a free-threaded run has the GIL re-enabled

Replaces the os.register_at_fork hook and WeakSet registry with a per-process
state bundle that the first generate() call in a new process rebuilds once,
covering forks that skip Python's fork hooks (uWSGI default mode). Switches to
re-entrant locks, adds Cuid.__deepcopy__, adds 3.15t to CI, and adds
free-threading regression guards.
@wgordon17
wgordon17 marked this pull request as ready for review October 30, 2026 18:30
@wgordon17
wgordon17 requested a review from a team October 30, 2026 18:30
@wgordon17
wgordon17 merged commit 7b26e8d into main Oct 30, 2026
28 of 55 checks passed
@wgordon17
wgordon17 deleted the fix/fork-thread-safety branch October 30, 2026 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant