Skip to content

🐛 fix(rw): reject misplaced singleton - #1058

Merged
gaborbernat merged 1 commit into
mainfrom
fix/rw-chdir-singleton-e82
Nov 20, 2026
Merged

gaborbernat merged 1 commit into
mainfrom
fix/rw-chdir-singleton-e82

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

ReadWriteLock resolved a relative path for its singleton cache key, then resolved it again while constructing the instance (old code). A concurrent working-directory change could make the instance open a different SQLite database. A later caller then reused that instance for the first path, allowing a second process to enter a write lock for the requested database. Construction now raises before caching an instance whose database path differs from its key. Fixes #1019.

The working directory changed between path resolutions

os.chdir() changes the working directory for the process. The metaclass resolved the relative path before calling the constructor; the constructor resolved it again after chdir(). The cache entry kept the first path, while the instance opened the second. The new path comparison refuses that entry before publishing it.

sequenceDiagram
    participant W as constructing thread
    participant C as directory-changing thread
    participant M as singleton metaclass
    participant L as later caller
    W->>+M: construct from relative path
    M->>M: resolve cache key in state directory
    C->>C: chdir to spool directory
    M->>M: constructor opens database in spool directory
    alt Before
        rect rgb(255, 214, 214)
            M-->>W: cache under state key
            L->>M: request state database
            M-->>L: return lock for spool database
        end
    else After
        rect rgb(216, 243, 220)
            M-->>W: raise RuntimeError, leave cache empty
        end
    end
    deactivate M
Loading

Behavior changes

A singleton constructor now raises RuntimeError if its database path changes during construction. It may have created a database at the second path before detecting the mismatch; that file remains. Callers that change directories concurrently can retry with an absolute path.

Reported by the November 2026 filelock review: ANT-2026-E82KKN0V.

A concurrent chdir could separate the cache key from the SQLite database
opened by a new singleton. Reject the instance before caching it so later
callers cannot receive a lock for another file.

Fixes #1019
@gaborbernat
gaborbernat marked this pull request as draft November 20, 2026 20:22
@gaborbernat
gaborbernat marked this pull request as ready for review November 20, 2026 20:24
@gaborbernat
gaborbernat merged commit aef02bd into main Nov 20, 2026
50 checks passed
@gaborbernat
gaborbernat deleted the fix/rw-chdir-singleton-e82 branch November 20, 2026 20:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ReadWriteLock singleton can cache a different path during chdir

1 participant