Skip to content

fix(dependencies): double-checked locking pattern is a data race under free-threaded Python 3.13+ (PEP 703) #296

Closed

Description

@piyushmakhija28

Summary

The singleton initialization pattern in backend/app/api/dependencies.py uses double-checked locking: check _cache is None, acquire lock, check again, create. This is safe under CPython's GIL but becomes a data race under Python 3.13+ free-threaded mode (PEP 703, nogil). Two threads can both pass the outer None check and construct duplicate SQLAlchemy engines, leaking connection pools.

Fix

backend/app/api/dependencies.py: move the singleton initialization to module level (eager init at import time) or initialize inside the lock without the outer None check. This removes the TOCTOU window entirely.

Impact

Under free-threaded Python 3.13+, multiple competing SQLAlchemy engine instances are created, each with their own connection pool, causing pool exhaustion and connection leaks under concurrent startup.

Activity

  1. added
    bugSomething isn't working
    priority: lowMinor correctness or invariant violation
    code-reviewFound via automated code review
    on Jun 21, 2026
  2. piyushmakhija28 commented on Jun 21, 2026

    @piyushmakhija28
    ContributorAuthor

    Fixed in PR #280 (Sprint R3/R4: fix(sprint-r3): root-cause fixes for all 39 bugs from code review round 3 + Sprint R4 round 4)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcode-reviewFound via automated code reviewpriority: lowMinor correctness or invariant violation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions