fix: guard flock(LOCK_UN) in finally blocks to prevent lock leaks#16044
Open
vominh1919 wants to merge 1 commit into
Open
fix: guard flock(LOCK_UN) in finally blocks to prevent lock leaks#16044vominh1919 wants to merge 1 commit into
vominh1919 wants to merge 1 commit into
Conversation
Three locations called fcntl.flock(fd, LOCK_UN) inside finally blocks without try/except protection. If the unlock syscall raised (e.g. due to an already-closed file descriptor or an interrupted syscall), the exception would propagate and skip the subsequent fd.close(), leaving the lock held indefinitely. Fixed by wrapping each flock(LOCK_UN) call in try/except OSError: pass, matching the pattern already used for msvcrt.unlocking() in the same files and in gateway/status.py. Files fixed: - tools/environments/file_sync.py (_sync_back_locked) - tools/memory_tool.py (_file_lock context manager) - hermes_cli/auth.py (_scoped_auth_lock context manager)
Collaborator
|
Partially overlaps with #15553 — both fix |
This was referenced Apr 27, 2026
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
Three locations called
fcntl.flock(fd, LOCK_UN)insidefinallyblocks withouttry/exceptprotection. If the unlock syscall raised (e.g. due to an already-closed file descriptor or an interrupted syscall), the exception would propagate and skip the subsequentfd.close(), leaving the lock held indefinitely.This is the same anti-pattern already fixed in
gateway/status.py(which properly wraps unlock inexcept OSError: pass).Fix
Wrapped each
flock(LOCK_UN)call intry/except OSError: pass, matching the pattern already used formsvcrt.locking()in the same files.Files fixed:
tools/environments/file_sync.py—_sync_back_locked()tools/memory_tool.py—_file_lockcontext managerhermes_cli/auth.py—_scoped_auth_lockcontext managerBefore vs After
flock(LOCK_UN)raisesclose()skipped, lock held foreverclose()runs, lock releasedTests
Existing lock-related tests continue to pass. This is a defensive fix for an edge case that's hard to reproduce in tests.