Skip to content

FileLock: re-resolve the lock path only when the target file changed (#214) - #225

Merged
thorwhalen merged 3 commits into
masterfrom
filelock-path-cache-214
Dec 1, 2026
Merged

thorwhalen merged 3 commits into
masterfrom
filelock-path-cache-214

Conversation

@thorwhalen

@thorwhalen thorwhalen commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Closes #214.

FileLock.__enter__ resolved its target with os.path.realpath (one lstat per path component) plus the lock-name normalisation on every entry. That was most of the cost of LockedAppender.append, which access trackers call on every store read.

Change. FileLock keeps its resolved lock path while a cheap check says a fresh resolution would give the same path: the target is not itself a symlink, and its folder is the folder the lock file sits in (stat of both, same device and inode; a folder has one real path). Otherwise, including whenever this cannot be told (a symlink target, a folder gone, no inode numbers), it resolves again as before. There is no public API change, and the lock path is the one master computes in every case, so the #188 guarantees hold: a retargeted link, a moved folder and a changed working directory are all followed.

Measured (Linux, path 13 components deep, min of 5×20000):

master this PR
FileLock enter+exit 50.7 µs 13.6 µs
LockedAppender.append 68.1 µs 25.3 µs

A target that is itself a symlink pays three extra stat calls per entry, since it resolves every time, as before.

Review (independent Opus subagent). Round 1 found a blocker in the first version, which checked the target file's identity. A file moved to another folder keeps its inode, and so does a file recreated on a reused inode (ext4 reused it 50 times in 50). The cached path then went stale, and two FileLocks on one file could both hold at once. The reviewer reproduced this. Fixed in aafa2b0 with the reviewer's folder-identity check. The reviewer also flagged that one test passed on master and would pass with a cache that never expires; it was replaced by the deterministic moved-file and mv+chdir tests. Round 2 verdict: see the comment below.

Tests (dol/tests/test_atomic.py):

  • no resolution across a missing, then created, rewritten and atomically replaced target;
  • resolution on every entry through a symlink;
  • a file moved into the folder a retargeted folder link now names;
  • a relative file moved along with the working directory;
  • a retargeted folder link.

The moved-file tests check both that the path equals a fresh resolution and that a second FileLock from the same thread is refused. Both moved-file tests fail on the first version. Full suite: 1085 passed, 11 skipped.

🤖 Generated with Claude Code

thorwhalen and others added 3 commits December 1, 2026 15:13
…214)

FileLock resolved its target with realpath (one lstat per path component) on
every entry. It now remembers the identity of the file the path named when it
resolved (one lstat, plus a stat through a symlink) and resolves again only when
that identity changed or cannot be told (missing file, no inode numbers).
A retargeted link, a replaced file and another working directory are still
followed. Measured on a 13-component path: entry 50.7 -> 8.4 us,
LockedAppender.append 68 -> 18 us.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…214 review)

The first version checked the target file's identity, which a file moved to
another folder keeps (as does a file recreated on a reused inode): the cached
lock path went stale and two FileLocks on one file could both hold. The cache
is now valid only while the target is not itself a symlink and its folder is
the lock file's folder (same device and inode), which matches a fresh
resolution exactly. Tests for the moved-file and mv+chdir cases; entry
50 -> 14 us, append 68 -> 25 us.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… round 2)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thorwhalen

Copy link
Copy Markdown
Member Author

Adversarial review, round 2 (independent Opus subagent, on aafa2b0): merge. No case found where the check passes and mutual exclusion breaks. It tried directory inode reuse, a folder recreated or mounted over, .. after a symlinked folder, case-insensitive filesystems, Windows case normalisation, threads and fork: each either gives the same lock file or resolves again. The reviewer's break script now behaves as master does. Minor wording fix applied in the next commit: across a bind mount the path strings can differ while naming the same lock file. The check-then-open race is the same one master has between realpath and open.

@thorwhalen
thorwhalen merged commit 3bb6941 into master Dec 1, 2026
12 checks passed
@thorwhalen
thorwhalen deleted the filelock-path-cache-214 branch December 1, 2026 15:21
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.

FileLock: cache the resolved lock path (realpath per entry costs ~40us)

1 participant