fix(memory): concurrent writes silently drop entries — add file locking - #1726
Merged
Conversation
Two concurrent gateway sessions calling memory add/replace/remove simultaneously could both read the old state, apply their changes independently, and write — the last writer silently drops the first writer's entry. Fix: wrap each mutation in a file lock (fcntl.flock on a .lock file). Under the lock, re-read entries from disk to get the latest state, apply the mutation, then write. This ensures concurrent writers serialize properly. The lock uses a separate .lock file since the memory file itself is atomically replaced via os.replace() (can't flock a replaced file). Readers remain lock-free since atomic rename ensures they always see a complete file.
angelburgosrosado
pushed a commit
to angelburgosrosado/hermes-agent
that referenced
this pull request
Apr 27, 2026
…l-file-locking fix(memory): concurrent writes silently drop entries — add file locking
02356abc
pushed a commit
to 02356abc/hermes-agent
that referenced
this pull request
May 14, 2026
…l-file-locking fix(memory): concurrent writes silently drop entries — add file locking
gweeteve
pushed a commit
to gweeteve/hermes-agent
that referenced
this pull request
Jun 2, 2026
…l-file-locking fix(memory): concurrent writes silently drop entries — add file locking
waefrebeorn
pushed a commit
to waefrebeorn/slermes
that referenced
this pull request
Jul 2, 2026
…l-file-locking fix(memory): concurrent writes silently drop entries — add file locking
michielhdoteth
added a commit
to michielhdoteth/hermes-agent
that referenced
this pull request
Aug 27, 2026
…data loss save_to_disk() previously wrote self._entries_for(target) without re-reading the file, so a session's stale in-memory snapshot could overwrite entries from concurrent writers (other sessions, daemons, cron jobs, scripts) — issue NousResearch#85858. The fix re-reads the file under the already-held lock and merges on-disk entries with pending entries before writing. This preserves writes from external writers that don't use the memory tool's lock (the primary gap) and adds defense-in-depth for concurrent sessions (beyond PR NousResearch#1726's file locking). Merge strategy: union-where-session-wins — all pending entries preserved, on-disk entries not in pending appended, no duplicates. Conflicts (same entry modified by both) result in both versions present — safe, no data loss. Closes NousResearch#85858 Co-Authored-By: Hermes Agent <noreply@nousresearch.com>
This was referenced Aug 27, 2026
melon-xf
added a commit
to melon-xf/hermes-agent
that referenced
this pull request
Sep 3, 2026
…l-file-locking fix(memory): concurrent writes silently drop entries — add file locking
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.
Summary
Two concurrent gateway sessions calling memory
add/replace/removesimultaneously could both read the old state, apply their changes independently, and write back. The last writer'sos.replace()would silently overwrite the first writer's changes.What changed
tools/memory_tool.py: All three mutating methods (add,replace,remove) now:fcntl.flockon a.lockfile)The lock uses a separate
.lockfile since the memory file itself is atomically replaced viaos.replace()(can'tflocka file that gets replaced). Readers remain lock-free — atomic rename ensures they always see a complete file.Added helpers:
_file_lock()context manager,_path_for(),_reload_target().Test plan
python -m pytest tests/ -n0 -q -k memory→ 108 passed, 2 skipped ✔