[Bug]: PathLock stale takeover can delete a concurrently renewed token
Issue Origin
Observed or reproduced in a real environment: deterministic Rust regression test on Windows, through PathLockManager::acquire_tree. The concurrency schedule was injected at the provider boundary; this is not a report of a production outage. Investigation and reproduction were performed by an AI agent.
Bug Description
Stale-token takeover can delete a token that its original owner has already refreshed. In crates/ragfs/src/lock/manager.rs, try_acquire_one reads a token, checks its age, then calls remove_token(..., existing.owner_id, true) before creating the contender's token. Removal checks the owner but not the timestamp observed by the staleness check. A heartbeat can update the same owner's token between those operations, and the contender still removes it and acquires the lock.
The acquire-retry loop contains another stale-read/force-remove sequence with the same missing snapshot check.
Steps to Reproduce
- Create a
MemFileSystemwith/dataand aMemoryPathLockProvider. Seed/data/.path.ovlockwith a Tree token owned byoriginal-ownerandtime_ns = 1. - Wrap the provider so that immediately before a takeover mutation it delegates
refresh_token(lock_path, original_owner, now_ns)to the underlying provider. Delegate all other operations unchanged. This deterministically schedules the holder's heartbeat after the contender reads the expired snapshot. - Construct the production
PathLockManagerwith the default configuration and callacquire_tree("/data", Duration::ZERO, None). - Assert that acquisition fails and that the refreshed original token survives. On current main, this assertion fails: the contender acquires the path. The same schedule will be covered with a nonzero timeout to exercise retries.
Expected Behavior
If the stored token changes after the stale snapshot is read, takeover must not delete or replace the changed token. The contender should follow the existing conflict/retry/timeout behavior. A token that remains stale and unchanged should still be reclaimable immediately, including with a zero timeout.
Actual Behavior
The refreshed token is removed and acquire_tree returns a new lease for a different owner. The manager does not preserve the heartbeat that won the race.
Error Logs
Baseline: cargo test -p ragfs --lib lock:: --locked — 32 passed.
With the regression added, before changing production code:
cargo test -p ragfs --lib stale_takeover_preserves_a_concurrent_refresh --locked --offline
test lock::manager::tests::stale_takeover_preserves_a_concurrent_refresh ... FAILED
assertion failed: matches!(mgr.acquire_tree("/data", timeout, None).await,
Err(PathLockError::Timeout { .. }))
OpenViking Version
Main commit 4f430f65fe2710e159cd2e195b11bbfb8bd4e85b.
Python Version
Not involved; the reproduction uses the RAGFS Rust library directly (rustc 1.98.1, MSVC).
Operating System
Windows
Model Backend
Not involved.
Additional Context / Proposed Fix
Area: Storage / RAGFS PathLock.
I plan to contribute a focused fix using the existing compare_and_write_token operation to atomically replace the exact expired snapshot with the contender's token. A compare miss remains a retryable conflict. Remove the retry loop's separate force-deletion path: ancestor/descendant conflict checks already ignore expired tokens, and same-path takeover belongs in try_acquire_one. Preserve rollback and Busy retry behavior, with a regression for concurrent renewal and validation of genuine stale recovery.
No public API, token encoding, timeout configuration, or migration changes are intended. This fixes the ordering of existing operations. I searched the current issues and PRs for stale takeover, renewal, pathlock, and remove_token; #4576 introduced force removal, while #4938 describes a different stale-lock symptom. This report does not claim to reproduce or resolve #4938.