OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

[Bug]: Stale .path.ovlock permanently blocks add_resource into any ancestor — non-blocking tree lock never checks lease expiry

Open leo8103lee opened this issue 2 months ago • 3 comments

Issue Origin

Observed or reproduced in a real environment.

Split out of #4919. Not covered by #4934 or #4936 — see "Why this is not a duplicate" below.

Bug Description

A single leftover .path.ovlock file anywhere inside a subtree makes every add_resource targeting an ancestor of that path return 409 forever, even after the lease in the lock token has expired and nothing is refreshing it.

openviking/service/resource_service.py (v0.4.19), lines 899 and 1310:

resource_lock = await self._viking_fs._async_agfs.pathlock_acquire_tree(
    dst_path,
    timeout_secs=0.0,
)

timeout_secs=0.0 is a single non-blocking attempt: no wait, no retry. The expiry encoded in the lock token is never consulted, so an expired lease is indistinguishable from a held one. The subtree stays unwritable until someone deletes the file by hand on the data volume.

In my instance three lock files under resources/AgoraRtcKit/4.6.2/symbols/ carried expiry timestamps 61 minutes in the past, with mtimes that had not been refreshed since, and they still blocked every import into that parent directory.

Steps to Reproduce

  1. Ingest resources into a directory, e.g. viking://resources/demo/docs, until at least one child directory exists.

  2. Leave a stale lock behind. Either kill the container mid-ingest, or simulate it directly:

    docker exec openviking sh -c \
      'printf "%s:%s:T" "$(uuidgen)" "$(( ($(date +%s) - 3600) * 1000000000 ))" \
       > /app/.openviking/data/viking/ACCOUNT/resources/demo/docs/SOME_CHILD/.path.ovlock'
    

    The expiry is one hour in the past, so the lease is already dead.

  3. Confirm the lock is invisible through the API — every ancestor reports isLocked: false:

    curl -H "X-API-Key: $KEY" \
      "$OV/api/v1/fs/stat?uri=viking://resources/demo/docs"
    
  4. Import any resource into that parent:

    curl -X POST -H "X-API-Key: $KEY" -H "Content-Type: application/json" \
      -d '{"temp_file_id":"...","to":"viking://resources/demo/docs","create_parent":true}' \
      "$OV/api/v1/resources"
    

Repeat step 4 any number of times, at any interval. It returns 409 every time.

Expected Behavior

An expired lease is not a held lock. pathlock_acquire_tree should reclaim or ignore it, and the import should proceed.

Failing that, the 409 should identify which path holds the conflicting lock, so the condition is diagnosable without shell access to the data volume.

Actual Behavior

409 forever, with an error that points away from the cause:

LockAcquisitionError: lock acquire timed out after 25ms

There is no wait with timeout_secs=0.0. The 25ms is how long the single attempt took. Reading this as a timeout sends you looking at storage latency and lock contention, neither of which is involved. Nothing in the message names the lock.

The second diagnostic gap: fs/stat reports isLocked for the requested path only, never for descendants. The tree lock covers the whole subtree, so the state that fails the acquire is not observable through any API — only by listing *ovlock* files inside the container.

How it feeds #4919

  1. Stale lock in the subtree blocks add_resource into the parent.
  2. The leaf is left as a bare file: no directory wrapper, no .abstract.md.
  3. The queued semantic message retries against that path and calls ls() on a file.
  4. AGFSNotADirectoryError, previously classified transient, re-enqueued indefinitely.

Once #4934 and #4936 land, step 4 terminates properly and step 3 is rejected at the door. Steps 1 and 2 are unchanged: the subtree still silently stops accepting writes, it just fails quietly with a 409 instead of loudly with a retry loop.

Why this is not a duplicate of #4919

  • #4936 rejects a to that already exists as a file. This report is about a to that is a perfectly normal directory, blocked by a lock on a sibling or descendant node. Different precondition, different failure path.
  • #4934 changes how a message is classified after it reaches the semantic queue. This failure happens before anything is enqueued.
  • Both PRs merged, this still reproduces. The symptom changes from an infinite retry loop to a silent permanent 409, which is quieter but not better.

Error Logs

openviking.storage.errors.LockAcquisitionError: lock acquire timed out after 25ms
  File ".../openviking/pyagfs/async_client.py", line 400, in pathlock_acquire_tree
    resource_lock = await self._viking_fs._async_agfs.pathlock_acquire_tree(

Lock file contents, format UUID:EXPIRY_NANOS:T:

quic_transport.md    expiry -3692s   mtime -3693s
rte_error.md         expiry -3692s   mtime -3693s
rte_player.md        expiry -3692s   mtime -3693s

All three expired an hour earlier, none being refreshed, all still blocking.

Impact in my instance

36 documents in one directory sat file-shaped with no L0 abstract because every import into their parent was refused. Over 12 hours the log carried roughly 1000 AGFSNotADirectoryError and about 430 Circuit breaker is open per hour, continuously.

Recovery required deleting the .path.ovlock files inside the container, then re-ingesting each document with to set to the resource's own full path instead of the parent. Scoping the tree lock to a single node sidesteps stale locks on sibling nodes, which is a usable workaround but not something an API consumer can be expected to discover.

Suggestions

  • Treat an expired lease as free: reclaim or ignore it rather than failing the acquire.
  • Make the acquire timeout configurable instead of a hardcoded 0.0.
  • Include the conflicting lock path in LockAcquisitionError, and state that the wait was zero rather than reporting elapsed time as a timeout.
  • Let fs/stat optionally report descendant locks, so the condition is diagnosable through the API.
  • Consider sweeping expired lock files on startup.

OpenViking Version

v0.4.19

Python Version

3.13.15

Operating System

macOS (Docker via OrbStack, linux/arm64 container)

leo8103lee avatar Sep 11 '26 08:09 leo8103lee

Note: on current main, check_descendant_locks already ignores stale tokens, and same-path acquire removes stale tokens before create. Related CAS takeover follow-up is #4945. If add_resource still 409s forever with an expired child .path.ovlock on recent main, a minimal repro + OV version would help.

ktz03 avatar Sep 11 '26 15:09 ktz03

You are right and I was wrong. Retracting the stale-lock claim.

I checked crates/ragfs/src/lock/manager.rs at v0.4.19, the version I ran, against main: check_descendant_locks is byte-identical, and it already skips stale tokens via !self.is_stale(&token, now_ns). This was never missing on my version.

What my locks actually were

is_stale compares token.time_ns, the acquisition time, against lock_expire_secs (default 30s). I misread the middle field of the token as an expiry timestamp, which inverted the conclusion.

My own earlier measurement, which I then failed to apply:

rte_error.md   token.time_ns = 06:03:41   now = 06:03:49   age = 8s
mtime refreshed every ~10s

Age 8s against a 30s window. Those were live, actively refreshed locks, held by real in-flight work. The 409s were correct behaviour.

The -3692s figure in my report came from a later sample, taken after the holder had finished and after a backup restore had put the files back. At that point they were genuinely stale, but I never re-tested a parent-scoped add_resource against them, so I have no evidence that a stale lock blocks anything. I inferred it. That inference was the whole basis of the report.

What actually misled me, and I think is still worth fixing

With lock_timeout_secs = 0.0 a genuine Conflict is reported to the caller as a Timeout:

Err((err, pre_conflict)) => {
    if !Self::is_retryable_error(&err) { break Err(err); }   // Conflict is retryable
    ...
    self.metrics.write().await.record_conflict(PathLockConflict { lock_path, ... });
    if start.elapsed() >= timeout {                          // timeout 0 -> always true
        break Err(PathLockError::Timeout { elapsed_ms });    // lock_path and owner dropped
    }

The conflict is constructed, the conflicting lock_path and owner are known, and they are recorded into metrics one statement earlier. Then the error handed back is Timeout { elapsed_ms }, which carries neither.

resource_service.py calls pathlock_acquire_tree with a hardcoded timeout_secs=0.0, so this is the normal path for add_resource, not an edge case. What the operator sees is:

LockAcquisitionError: lock acquire timed out after 25ms

With zero timeout there is no waiting, so 25ms is just how long one attempt took. It reads as storage latency or lock contention under load. I spent a while on disk performance and on expired leases before the answer turned out to be "another operation holds a lock in this subtree", which the code knew all along.

Two small changes would have made this a five-minute diagnosis:

  1. When the wait budget is zero and the underlying error is a Conflict, return the Conflict rather than converting it to Timeout. There was no wait to time out.
  2. Failing that, carry lock_path and owner into the Timeout variant.

Separately

fs/stat reports isLocked for the requested path only, never for descendants. A tree lock covers the whole subtree, so the state that fails the acquire is not observable from the API at all. That is what forced me to go read *.ovlock files inside the container in the first place. If stat could optionally report descendant locks, neither of us would need a repro to answer "what is holding this subtree".

On this issue

The premise in the title is wrong, so it should not stay open as written. I am happy either way: close it and I will open a focused one on the Conflict to Timeout conversion, or I retitle this one to that. Tell me which you prefer.

Sorry for the noise. Thanks for pushing back with the specific function name instead of just asking for a repro; that is what made it quick to find my own error.

leo8103lee avatar Sep 11 '26 15:09 leo8103lee

Thanks for reporting this and for the follow-up investigation. Your correction establishes that the observed lock belonged to a live task and was being renewed; its acquisition timestamp was mistaken for an expiry timestamp. That does not demonstrate a stale lock permanently blocking its ancestors, and the current lock manager checks expiry when inspecting descendant locks.

Closing based on the corrected diagnosis. If you obtain a reproduction with an expired, unrenewed lock and no live holder that still blocks add_resource, please reopen, or comment requesting reopening, with the server version and a sanitized lock/owner/timestamp timeline. No manual deletion of a live task's lock is needed for the case described in the correction.

qin-ctx avatar Sep 18 '26 08:09 qin-ctx