Skip to content

aitools: retry a config read that hits a Windows sharing violation - #7870

Merged
denik merged 7 commits into
mainfrom
denik/agentconfig-read-in-use
Nov 22, 2026
Merged

denik merged 7 commits into
mainfrom
denik/agentconfig-read-in-use

Conversation

@denik

@denik denik commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Changes

On Windows, agentconfig retries 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 in Upsert, and checkWritable open 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 with ERROR_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 list and add would fail the same way for a user whose Claude Code is saving its config.

Why

TestLockedEditLosesNoUpdates failed on main twice on Windows. Each time the failure was in ReadAt (writer_test.go:1254):

error snippet
error/3800 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

  • New Windows-only TestReadConfigWaitsForFileInUse and TestCheckWritableWaitsForFileInUse. Each holds the config open with no sharing for 100 ms and checks that readConfig returns the content, or that checkWritable passes. Without the fix both fail with the sharing violation.
  • On PRs, aitools changes don't run the Windows unit tests. I pushed a temporary commit that triggered the full matrix, then reverted it. Both Windows jobs passed all three tests.
  • TestLockedEditLosesNoUpdates still checks the same property: no update is lost on either side.

This pull request and its description were written by Isaac.

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>
@github-actions github-actions Bot added the aitools AI tools (databricks aitools) related changes label Nov 21, 2026
Co-authored-by: Isaac <no-reply@databricks.com>
@eng-dev-ecosystem-bot

eng-dev-ecosystem-bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

✅ Integration test report

Commit: 8d11e99

Run: 44900602104

Env 💚​RECOVERED ✅​pass 🙈​skip Time
💚​ aws linux-2core-8gb 1 272 19 3:56
💚​ aws-windows-latest-4core-16gb 1 274 17 3:33
💚​ azure linux-2core-8gb 1 271 19 3:59
💚​ azure-windows-latest-4core-16gb 1 273 17 4:31
💚​ gcp linux-2core-8gb 1 272 19 3:50
💚​ gcp-windows-latest-4core-16gb 1 274 17 4:27
Test Name aws linux-2core-8gb aws-windows-latest-4core-16gb azure linux-2core-8gb azure-windows-latest-4core-16gb gcp linux-2core-8gb gcp-windows-latest-4core-16gb
💚​ TestAccept 💚​R 💚​R 💚​R 💚​R 💚​R 💚​R
Top 3 slowest tests (at least 2 minutes):
duration env testname
4:29 azure-windows-latest-4core-16gb TestAccept
4:25 gcp-windows-latest-4core-16gb TestAccept
3:32 aws-windows-latest-4core-16gb TestAccept

@denik
denik marked this pull request as ready for review November 21, 2026 08:42
@denik
denik requested review from a team and lennartkats-db as code owners November 21, 2026 08:42
The full matrix passed on Windows, including TestReadConfigWaitsForFileInUse
and TestLockedEditLosesNoUpdates.

Co-authored-by: Isaac <no-reply@databricks.com>

@review-oss-bot review-oss-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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: checkWritable opens the file with os.OpenFile(path, os.O_WRONLY, 0) and is called outside the lock in Upsert (line 135, !found path) and RemoveAt (line 247). On Windows, if Claude Code is renaming at that instant, ERROR_SHARING_VIOLATION doesn't match fs.ErrNotExist or fs.ErrPermission, so it falls to the generic branch (line 560) and surfaces a confusing error. The same transient race readConfig now retries is unhandled here. Could checkWritable get a similar retry, or is the rarity of this path not worth it?

denik and others added 3 commits November 21, 2026 11:44
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>
@denik

denik commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Done in cf7fef9: checkWritable now opens through the same retrying openConfig as the read, and a Windows-only test covers it (TestCheckWritableWaitsForFileInUse). It passed on both Windows jobs.

AI agent (opus5.5, isaac_claude)

@denik
denik added this pull request to the merge queue Nov 22, 2026
Merged via the queue into main with commit 1b24b1d Nov 22, 2026
23 checks passed
@denik
denik deleted the denik/agentconfig-read-in-use branch November 22, 2026 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

aitools AI tools (databricks aitools) related changes factory-generated

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants