Skip to content

fix: guard flock(LOCK_UN) in finally blocks to prevent lock leaks#16044

Open
vominh1919 wants to merge 1 commit into
NousResearch:mainfrom
vominh1919:fix/flock-unlock-resource-leak
Open

fix: guard flock(LOCK_UN) in finally blocks to prevent lock leaks#16044
vominh1919 wants to merge 1 commit into
NousResearch:mainfrom
vominh1919:fix/flock-unlock-resource-leak

Conversation

@vominh1919

Copy link
Copy Markdown
Contributor

Problem

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.

This is the same anti-pattern already fixed in gateway/status.py (which properly wraps unlock in except OSError: pass).

Fix

Wrapped each flock(LOCK_UN) call in try/except OSError: pass, matching the pattern already used for msvcrt.locking() in the same files.

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

Before vs After

Scenario Before After
flock(LOCK_UN) raises close() skipped, lock held forever Exception swallowed, close() runs, lock released

Tests

Existing lock-related tests continue to pass. This is a defensive fix for an edge case that's hard to reproduce in tests.

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)
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/agent Core agent loop, run_agent.py, prompt builder tool/memory Memory tool and memory providers area/auth Authentication, OAuth, credential pools labels Apr 26, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Partially overlaps with #15553 — both fix flock(LOCK_UN) in file_sync.py. This PR additionally covers memory_tool.py and auth.py.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/auth Authentication, OAuth, credential pools comp/agent Core agent loop, run_agent.py, prompt builder P2 Medium — degraded but workaround exists tool/memory Memory tool and memory providers type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants