Repository navigation
aitools: retry a config read that hits a Windows sharing violation - #7870
Conversation
A read without the agent's lock can run while Claude Code renames a new config over the file. On Windows, opening the file then fails with a sharing violation; TestLockedEditLosesNoUpdates hit this in ReadAt. Co-authored-by: Isaac <no-reply@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>
✅ Integration test reportCommit: 8d11e99
Top 3 slowest tests (at least 2 minutes):
|
The full matrix passed on Windows, including TestReadConfigWaitsForFileInUse and TestLockedEditLosesNoUpdates. Co-authored-by: Isaac <no-reply@databricks.com>
There was a problem hiding this comment.
Automated review at 591ffec. Each finding was verified against the code.
No blocking issues found. 1 non-blocking comment(s) below.
This PR adds a retry loop around os.ReadFile in load to handle transient ERROR_SHARING_VIOLATION on Windows when Claude Code renames a new config version over the file. The approach is sound: a platform-specific fileInUse check gates the retry, the non-Windows stub is a no-op, and a new Windows-only test covers the fix. One sibling code path (checkWritable) has the same unlocked exposure to sharing violations but is not covered by the retry.
Comments outside the changed lines
- [nice to have]
libs/aitools/mcp/agentconfig/writer.go:553:checkWritableopens the file withos.OpenFile(path, os.O_WRONLY, 0)and is called outside the lock inUpsert(line 135,!foundpath) andRemoveAt(line 247). On Windows, if Claude Code is renaming at that instant,ERROR_SHARING_VIOLATIONdoesn't matchfs.ErrNotExistorfs.ErrPermission, so it falls to the generic branch (line 560) and surfaces a confusing error. The same transient racereadConfignow retries is unhandled here. CouldcheckWritableget a similar retry, or is the rarity of this path not worth it?
checkWritable opens the config without the lock as well, so it can hit the same sharing violation during a rename over the file. Co-authored-by: Isaac <no-reply@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>
The full matrix passed on Windows, including the new WaitsForFileInUse tests and TestLockedEditLosesNoUpdates. Co-authored-by: Isaac <no-reply@databricks.com>
|
Done in cf7fef9: AI agent (opus5.5, isaac_claude) |
Changes
On Windows,
agentconfigretries opening a config that fails with a sharing violation, for up to 2 s. This applies to the read (load) and to the write check (checkWritable).ReadAt, the first check inUpsert, andcheckWritableopen the config without the agent's lock. Claude Code can be renaming a new version over the file at that moment. On Windows, opening a file during a rename over it fails withERROR_SHARING_VIOLATION("The process cannot access the file because it is being used by another process"). That only lasts as long as the rename, so we read again. Opens under the lock aren't affected, because Claude Code doesn't rename while we hold the lock. Other platforms are unchanged.I fixed the reader in the production code rather than taking the lock in the test, because
aitools mcp listandaddwould fail the same way for a user whose Claude Code is saving its config.Why
TestLockedEditLosesNoUpdatesfailed on main twice on Windows. Each time the failure was inReadAt(writer_test.go:1254):read C:/Users/RUNNER~1/AppData/Local/Temp/TestLockedEditLosesNoUpdates.../001/.claude.json: open ...\.claude.json: The process cannot access the file because it is being used by another process.Dashboard: libs/aitools/mcp/agentconfig.TestLockedEditLosesNoUpdates, error/3800
Tests
TestReadConfigWaitsForFileInUseandTestCheckWritableWaitsForFileInUse. Each holds the config open with no sharing for 100 ms and checks thatreadConfigreturns the content, or thatcheckWritablepasses. Without the fix both fail with the sharing violation.TestLockedEditLosesNoUpdatesstill checks the same property: no update is lost on either side.This pull request and its description were written by Isaac.