Skip to content

🐛 fix(rw): protect private database links - #1096

Merged
gaborbernat merged 8 commits into
tox-dev:mainfrom
gaborbernat:fix/rw-private-link-lifecycle
Nov 21, 2026
Merged

gaborbernat merged 8 commits into
tox-dev:mainfrom
gaborbernat:fix/rw-private-link-lifecycle

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Processes sharing a ReadWriteLock or AsyncReadWriteLock database need to open the same file. On POSIX, filelock now opens the private hard link with SQLite's mode=rw. If the link disappears before SQLite opens it, acquisition fails. Upgrade every process sharing the database before resuming lock operations. Earlier releases can create a separate database at a missing link. Fixes #1005.

The final link sits inside a new directory that only the locking account can access. If inherited permissions let other accounts in, filelock sets mode 0700 and checks the directory again before creating the link. This keeps the final link name hidden from anyone watching the enclosing directory. Private link advisory

Cleanup checks a 50 ms deadline between directory entries. When a scan runs out of time, that process skips cleanup for the database for one minute. Its next eligible connection resumes the scan. Each process keeps up to 16 paused scans. An evicted scan starts over. Linux processes that cannot verify a holder's PID namespace leave its links in place. Stop all database users before removing stale directories. Cleanup advisory

flowchart TD
    A["Open by private hard link"] --> B["Create a private directory"]
    B --> G["Tighten inherited permissions if needed"]
    G --> C{"Only owner has access?"}
    C -->|Yes| D["Open existing link"]
    C -->|No| E["Refuse acquisition"]
    D -->|Link exists| F["Use shared database"]
    D -->|Link missing| E
    classDef work fill:#cfe4ff,stroke:#1f6feb,color:#0b1f3a
    classDef stop fill:#ffe3a3,stroke:#d29200,color:#3a2c00
    class A,B,C,D,F,G work
    class E stop
Loading

Cleanup in another PID namespace can mistake a live owner for dead and unlink
its private alias before SQLite opens it. A missing alias now fails instead
of creating a second database. When procfs cannot verify namespace identity,
cleanup leaves links in place, so stale links may need manual removal.

Fixes tox-dev#1005
A busy shared directory can exhaust the cleanup budget before a process
finds a dead holder’s link. A later connection can retry the sweep.
A watch on a shared-directory entry can outlive a permissions change.
Create the final link name inside a fresh private child, and reject
inherited permissions that would expose that name.
@gaborbernat
gaborbernat marked this pull request as draft November 20, 2026 15:32
@gaborbernat
gaborbernat marked this pull request as ready for review November 20, 2026 17:40
@gaborbernat
gaborbernat marked this pull request as draft November 20, 2026 19:32
Filesystems with inherited ACLs can grant group access to newly created
directories. Set mode 0700 on the nested directory and check it again
before creating the private link.
Starting over on each connection kept large directories from ever reaching
their last entry. Pause between scan slices and retain up to 16 directory
iterators so later connections can continue. Drop inherited iterators in a
fork child and start a fresh scan if the parent directory changes.
@gaborbernat
gaborbernat marked this pull request as ready for review November 20, 2026 22:03
@gaborbernat
gaborbernat marked this pull request as draft November 20, 2026 23:57
The child must leave an inherited SQLite connection open and close its
copy of a paused cleanup iterator. Keep both tests and their platform
requirements.
@gaborbernat
gaborbernat marked this pull request as ready for review November 21, 2026 00:13
@gaborbernat
gaborbernat merged commit d7324f5 into tox-dev:main Nov 21, 2026
49 checks passed
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 can admit two writers after a private link is swept

1 participant