fix(locks): retry atomic_write_bytes on transient PermissionError (WinError 5) - #255
Open
sebastianbraun25 wants to merge 1 commit into
Open
Conversation
…nError 5) A freshly written temp file can be briefly locked by antivirus/indexing/backup software right before os.replace(), causing a transient PermissionError. Retry with short exponential backoff so this is absorbed before it ever reaches the compiler's full-pipeline retry, which previously had to re-run the entire LLM compile just to redo a file rename.\n\nResolves VectifyAI#254
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
atomic_write_bytes()inopenkb/locks.py(the shared helper behindatomic_write_text/atomic_write_json, used by the compiler, indexer, converter, config, and page_ops) performed a single, unretriedos.replace(tmp_path, path)to publish a write. On Windows, a freshly-written temp file can be briefly locked by an external process (real-time antivirus scanning, the search indexer, backup/sync agents) right before the rename —os.replace()then raisesPermissionError(WinError 5, Access is denied) even though the lock typically clears within milliseconds to a couple of seconds.Because this exception surfaced all the way up,
_run_compile_with_retry()incli.py(added in #229) treated it like any other failure: it discarded the fully-generated summary/concept/entity content already held in memory and re-ran the entire LLM compile pipeline from scratch just to retry a single file rename — wasting the LLM cost and time of a full document compile for a transient, typically self-clearing lock.Root Cause
_compile_concepts()already holds all LLM-generated content (summary, concept/entity pages) in memory and only writes it to disk at the very end of the pipeline — so a write failure at that point has nothing to do with generation. The actual gap was one level lower:atomic_write_bytes()'sos.replace()call had no retry of its own, so any transient OS-level lock immediately propagated up to the much more expensive full-pipeline retry instead of being absorbed where it occurred.Solution / Changes
openkb/locks.py: new_replace_with_retry()wraps theos.replace()call inatomic_write_bytes()with a bounded retry (5 attempts, exponential backoff starting at 50ms). OnlyPermissionErroris retried — any otherOSError(a real permissions problem) is raised immediately, unchanged from before. Each retry logs at DEBUG level (surfaces in the per-file debug log whendebug: true/-vis enabled).tests/test_locks.py: three new tests — succeeds after transientPermissionErrors, re-raises after exhausting all attempts (and still cleans up the temp file), and does not retry an unrelatedOSError.Issues
Resolves #254. Related to #229 (origin of
_run_compile_with_retry, whose full-pipeline retry this change makes largely unnecessary for this failure class).