nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] atomicWriteFile fails with EPERM on Windows when two writers rename onto the same file

Open addyCooks opened this issue 1 week ago • 2 comments

Description

atomicWriteFile writes a temp file and renames it over the target. On Windows, rename onto a file that another rename or read is touching fails with EPERM, and the helper does not retry. PR #2207 added a retry to the session index only; every other user of this helper (artifacts, memory, stats, timeline, lockfile) is still affected.

Environment

  • OS: Windows 11 Home 10.0.26300
  • Node version: v22.22.3
  • Nanocoder version: 1.32.0 (built from main at eb7dee57)
  • Provider: Not provider-specific
  • Model: Not model-specific

Steps to Reproduce

  1. In a loop of 20 rounds, call atomicWriteFile(target, text) 16 times at once for the same target (Promise.allSettled).
  2. Count the rejected writes.

Expected Behavior

All 320 writes succeed (the last one wins).

Actual Behavior

79 of 320 writes fail.

Logs/Screenshots

EPERM: 79   (241 writes succeeded)

Additional Context

  • [x] I have searched existing issues to ensure this is not a duplicate
  • [x] I can reproduce this issue consistently
  • [x] This issue occurs with the latest version of nanocoder (main)

Suggested fix:

  • source/utils/fs/atomic-write.ts:56-72 (atomicWriteFile, and the sync variant above it): retry rename a few times with a short delay (for example 10, 20, 40 ms) when the code is EPERM, EBUSY or EACCES, on Windows only. Then session-manager.ts can drop its own copy of the retry.
  • Add a test with concurrent writers like the steps above.

Related: #2195 / PR #2207 (session index only).

addyCooks avatar Nov 23 '26 07:11 addyCooks

I'd like to pick this up. I reproduced it on Windows 11 (16 concurrent writers fail with EPERM in the first round) and have a fix that retries the rename on EPERM/EBUSY/EACCES with a short backoff (10–160 ms, then rethrows), like the session index retry from #2207, plus specs. Could you assign me? I'll open the PR then.

seragAhmed avatar Nov 23 '26 09:11 seragAhmed

just assigned @seragAhmed, go ahead

akramcodez avatar Nov 24 '26 08:11 akramcodez