[Bug] Session save fails with EPERM on Windows when another process is reading sessions.json
Description
On Windows, saving a session throws EPERM: operation not permitted, rename ... sessions.json.<id>.tmp if another process is reading sessions.json at that moment. The session file is written but the index update fails, so the session is missing from /resume until the next start. This is a follow-up to #1706 / PR #1732: the cross-process lock covers writers but not readers.
Environment
- OS: Windows 11 Home 10.0.26300
- Node version: v22.22.3
- Nanocoder version: 1.32.0 (built from main at f92cce9c)
- Provider: Not provider-specific
- Model: Not model-specific
Steps to Reproduce
- Write a worker script using
SessionManageron one shared directory that loopscreateSession(writer) orlistSessions(reader). - Start 4 writers (40 saves each) and 1 reader (400
listSessionscalls) at the same time. - Count errors, session files and index entries.
Expected Behavior
No errors. The index lists every session file.
Actual Behavior
Several saves throw EPERM, and the index misses sessions.
Logs/Screenshots
ERR w EPERM: operation not permitted, rename 'C:\...\sessions.json.<uuid>.tmp' (5 in one run, 48 in another)
session files: 121, index entries: 100
With 4 writers and no reader: 0 errors, 100 files, 100 index entries. After a restart (initialize() then listSessions()) all 121 sessions show again.
Additional Context
- [x] I have searched existing issues to ensure this is not a duplicate
- [x] I can reproduce this issue consistently (intermittent under load)
- [x] This issue occurs with the latest version of nanocoder (main)
Suggested fix:
source/session/session-manager.ts:55-63(atomicWriteFile): retry the rename a few times with a short delay when it fails with EPERM, EBUSY or EACCES (Windows only).- Optionally read the index under the same lock (
listSessions, line 385). - Add a test with a concurrent reader.
Related: #1706 (closed), PR #1732.
@addyCooks May I take this issue? I’m new to the repo and will try to fix it, will update here if I have questions
Go ahead @Ember452 !