OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

[Bug]: PathLock stale takeover can delete a concurrently renewed token

Open Tong-bit-art opened this issue 2 months ago • 0 comments

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

  1. Create a MemFileSystem with /data and a MemoryPathLockProvider. Seed /data/.path.ovlock with a Tree token owned by original-owner and time_ns = 1.
  2. 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.
  3. Construct the production PathLockManager with the default configuration and call acquire_tree("/data", Duration::ZERO, None).
  4. 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.

Tong-bit-art avatar Sep 11 '26 09:09 Tong-bit-art