fix: guard fcntl.flock(LOCK_UN) against OSError in finally blocks#23819
Closed
vanthinh6886 wants to merge 1 commit into
Closed
fix: guard fcntl.flock(LOCK_UN) against OSError in finally blocks#23819vanthinh6886 wants to merge 1 commit into
vanthinh6886 wants to merge 1 commit into
Conversation
When fcntl.flock(fd, LOCK_UN) raises OSError in a finally block, the subsequent cleanup code (e.g., fd.close()) is skipped, causing the file descriptor to leak and the lock to be held indefinitely. This pattern existed in 5 files: - agent/shell_hooks.py: _allowlist_editor() - tools/skill_usage.py: _file_lock() - tools/memory_tool.py: _file_lock() - tools/environments/file_sync.py: sync_back() - hermes_cli/auth.py: _credential_file_lock() The Windows msvcrt path was already guarded with try/except in all these files, but the POSIX fcntl path was not. Fix: wrap each fcntl.flock(LOCK_UN) call in try/except OSError: pass, matching the defensive pattern already used for the msvcrt path. Fixes NousResearch#21719
Contributor
This was referenced May 11, 2026
Contributor
|
This appears to be implemented on current Evidence:
Thanks for the focused cleanup here; the prior duplicate note on #16274/#21719/#20529 matches what main now contains. |
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
When
fcntl.flock(fd, LOCK_UN)raisesOSErrorin afinallyblock, the subsequent cleanup code (e.g.,fd.close()) is skipped. This causes:The Windows
msvcrtpath was already guarded withtry/exceptin all affected files, but the POSIXfcntlpath was not.Affected Files
agent/shell_hooks.py_allowlist_editor()tools/skill_usage.py_file_lock()tools/memory_tool.py_file_lock()tools/environments/file_sync.pysync_back()hermes_cli/auth.py_credential_file_lock()The most dangerous case is
file_sync.pywherelock_fd.close()follows the unlock — if the unlock raises, the fd leaks and the lock is held forever.Fix
Wrap each
fcntl.flock(fd, LOCK_UN)intry: ... except OSError: pass, matching the defensive pattern already used for themsvcrtpath in the same files.Tests
ast.parse()on all 5 filesflocksucceeds normallyFixes #21719