OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

Semantic message carrying a stale lock handoff requeues in a tight loop after server restart (every ~200ms, no backoff, no cap)

Open pengfus opened this issue 4 months ago • 1 comments

Environment

  • OpenViking v0.4.5 (docker); relevant code unchanged in current main (2026-07-10)

Bug

If the server restarts while a semantic message is in flight, the message persisted in the queue still carries a lock handoff whose handle died with the old process. After restart, processing that message fails in:

semantic_processor.py:367  SemanticLockScope.resolve(...)
semantic_lock.py:59        OwnedLockLease.from_handoff(...)
lock_lease.py:137          raise LockAcquisitionError(f"Lock handle is no longer active: {ref.handle_id}")

The handler treats this as a transient lock error ("Lock error processing semantic message, re-enqueueing without tripping API circuit breaker") and requeues the message unchanged — stale handoff included. The next dequeue fails identically ~200ms later. The loop is infinite: no backoff, no retry cap, no dead-letter, and the stale handoff can never become valid again. The result is a permanent hot loop burning CPU and flooding the log (a WARNING + full traceback + two INFO lines every 200ms) until manual intervention.

We hit this after a routine docker restart of the container: one in-flight message (a temp-session archive result) poisoned the Semantic queue indefinitely.

Reproduction

  1. Ensure a semantic message is being processed (e.g. register a resource with heavy semantic work).
  2. Restart the server process while it is in flight.
  3. After restart, the log loops on LockAcquisitionError: Lock handle is no longer active: <old handle id> for the same message id, ~5 times/second, forever.

Workaround (manual)

The message must be deleted from _system/queue/queue.db (queue_messages table) while the server is stopped — deleting it while running loses the race, because the requeue re-inserts it (with a new row id) immediately. Note the message body is stored as a JSON-wrapped byte array, so text LIKE matching on URIs/ids does not work; with a single pending row, DELETE FROM queue_messages while stopped is the practical fix.

Expected behavior

  • When from_handoff reports the handle is no longer active, the processor should discard the stale handoff and acquire a fresh lock for the message's scope (the owner is provably gone).
  • Independently, lock-error requeues should have exponential backoff and a retry cap with dead-lettering — an unconditional 200ms hot loop should not be reachable.

pengfus avatar Jul 11 '26 05:07 pengfus

Update after re-reading current main (e123bbc0) — one correction and a sharper root cause.

Correction: my claim that the stale handoff "can never become valid again" was incomplete. #2214 (c99623fe, already included in v0.4.5) added a recovery path in SemanticLockScope.resolve (openviking/storage/queuefs/semantic_lock.py:56-81): on LockAcquisitionError from from_handoff it extracts tree paths from lock_handoff.lock_paths and reacquires a fresh lock.

Why the infinite loop still happens: the recovery only recognizes paths ending with /<LOCK_FILE_NAME> (_tree_paths_from_handoff, semantic_lock.py:26-34). When tree_paths is empty it bare-raises (semantic_lock.py:61-63) — which is exactly the traceback I reported (lock_lease.py:137 propagating out of resolve). Handoffs can legitimately carry non-tree lock paths:

  • PathLockEngine.acquire_tree on a path that exists as a file degenerates to a prefixed exact-lock file (.mw_exact_<name>.<sha1>), not <path>/<LOCK_FILE_NAME> (openviking/storage/transaction/path_lock.py:627-634 + :146-150);
  • messages persisted in queue.db by an older version may carry an empty lock_paths.

Either case makes recovery unreachable and the message loops forever, because the requeue path for lock errors (semantic_processor.py:463-475) has no backoff, no retry cap, and no dead-letter — the only throttle in _reenqueue_semantic_msg (semantic_processor.py:234-236) comes from the API circuit breaker, which lock errors deliberately do not trip.

Refined ask:

  1. When tree_paths is empty (or reacquisition keeps failing), drop the stale handoff and acquire a fresh lock derived from msg.uri instead of re-raising.
  2. Independently of (1), lock-error requeues need exponential backoff and a retry cap with dead-lettering — any future unrecoverable lock state would otherwise hot-loop the same way.

pengfus avatar Jul 11 '26 12:07 pengfus