Skip to content

fix(session): atomic write for sessions.json to prevent data loss on crash#611

Merged
teknium1 merged 1 commit into
NousResearch:mainfrom
alireza78a:fix/shell-injection-expand-path
Mar 10, 2026
Merged

fix(session): atomic write for sessions.json to prevent data loss on crash#611
teknium1 merged 1 commit into
NousResearch:mainfrom
alireza78a:fix/shell-injection-expand-path

Conversation

@alireza78a

Copy link
Copy Markdown
Contributor

What

_save() in gateway/session.py was writing sessions.json like this:

with open(sessions_file, "w") as f:
    json.dump(data, f, indent=2)

open(..., "w") truncates the file immediately. If the process crashes or gets OOM-killed mid-write, sessions.json ends up empty. On next startup _ensure_loaded() silently skips the broken file — every user gets a fresh session.

Fix

Replaced with tempfile.mkstemp + os.replace — same pattern already used in cron/jobs.save_jobs():

  1. Write to a temp file in the same directory
  2. fsync to flush to disk
  3. os.replace — atomic rename, readers always see a complete file

Impact

  • Prevents silent session loss on crash or OOM kill
  • No behaviour change under normal operation

@alireza78a

Copy link
Copy Markdown
Contributor Author

Checked the failing test — it fails on main too, unrelated to this change.

@teknium1 teknium1 merged commit 8f0b07e into NousResearch:main Mar 10, 2026
1 check failed
@teknium1

Copy link
Copy Markdown
Contributor

Merged in 8f0b07e. Good catch @alireza78a — the truncate-on-crash risk was real. Resolved a minor conflict (added encoding='utf-8' to the fdopen call from a parallel change). Matches our existing atomic write pattern in cron/jobs.py.

angelburgosrosado pushed a commit to angelburgosrosado/hermes-agent that referenced this pull request Apr 27, 2026
…on to prevent data loss on crash

Authored by alireza78a.

Replaces open('w') + json.dump with tempfile.mkstemp + os.replace atomic
write pattern, matching the existing pattern in cron/jobs.py. Prevents
silent session loss if the process crashes or gets OOM-killed mid-write.

Resolved conflict: kept encoding='utf-8' from HEAD in the new fdopen call.
02356abc pushed a commit to 02356abc/hermes-agent that referenced this pull request May 14, 2026
…on to prevent data loss on crash

Authored by alireza78a.

Replaces open('w') + json.dump with tempfile.mkstemp + os.replace atomic
write pattern, matching the existing pattern in cron/jobs.py. Prevents
silent session loss if the process crashes or gets OOM-killed mid-write.

Resolved conflict: kept encoding='utf-8' from HEAD in the new fdopen call.
olympus-terminal pushed a commit to olympus-terminal/hermes-agent that referenced this pull request May 16, 2026
…on to prevent data loss on crash

Authored by alireza78a.

Replaces open('w') + json.dump with tempfile.mkstemp + os.replace atomic
write pattern, matching the existing pattern in cron/jobs.py. Prevents
silent session loss if the process crashes or gets OOM-killed mid-write.

Resolved conflict: kept encoding='utf-8' from HEAD in the new fdopen call.
Egavasyug pushed a commit to Egavasyug/hermes-agent that referenced this pull request Jun 10, 2026
…on to prevent data loss on crash

Authored by alireza78a.

Replaces open('w') + json.dump with tempfile.mkstemp + os.replace atomic
write pattern, matching the existing pattern in cron/jobs.py. Prevents
silent session loss if the process crashes or gets OOM-killed mid-write.

Resolved conflict: kept encoding='utf-8' from HEAD in the new fdopen call.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants