Skip to content

Keep concurrent usage collectors off one shared temp file - #6654

Merged
dhh merged 1 commit into
quattrofrom
fix-agent-usage-cache-write-race
Aug 9, 2026
Merged

dhh merged 1 commit into
quattrofrom
fix-agent-usage-cache-write-race

Conversation

@dhh

@dhh dhh commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Fixes #6649.

write_json() built its temp path from the target — claude-opencode-*.json.tmp for
claude-opencode-*.json. Two collectors running at once therefore wrote the same temp file,
and the one that called replace() second found it already moved away:

FileNotFoundError: [Errno 2] No such file or directory:
  '.../claude-opencode-3756ad4dd55fc8fe.json.tmp' -> '.../claude-opencode-3756ad4dd55fc8fe.json'

That overlap is normal: omarchy-agent-usage-update backgrounds one collector per agent while
the panel refreshes on its own. The reporter hit it during omarchy update, when migration
1786099804 primed the data files alongside a periodic refresh.

cached_scan() was already serialized by an fcntl.flock, but the three other write_json()
call sites — the pi/omp session cache, the opencode cache, and the limits probe cache — were
not, so locking only the opencode scan would have left two identical races behind. Giving
write_json() a per-writer temp name via tempfile.mkstemp closes all of them at once and
keeps the replace atomic; the explicit chmod preserves the 0644 these caches had before,
since mkstemp opens at 0600.

The failure was transient and self-healed on the next run, but it aborted the collector, so
that run produced no Claude record and the update printed claude collector failed.

— 🤖 Claude, posting on behalf of @dhh

Two Claude collectors running at once both wrote the cache through a temp
path derived from the target, so the second replace found the file already
moved away and crashed the update with a FileNotFoundError.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Copilot AI balanced review requested due to automatic review settings August 9, 2026 11:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes concurrent Claude usage collectors racing over a shared temporary cache file.

Changes:

  • Uses unique temporary files while preserving atomic replacement and permissions.
  • Adds concurrency, integrity, cleanup, and permission regression coverage.

Tip

If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.

Reviewed changes

Copilot reviewed 1 out of 2 changed files in this pull request and generated no comments.

File Description
bin/omarchy-agent-usage-claude Makes JSON cache writes concurrency-safe.
test/shell.d/agent-usage-claude-scanner-test.sh Tests concurrent cache writes and permissions.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@dhh
dhh merged commit e4d85bd into quattro Aug 9, 2026
4 checks passed
@dhh
dhh deleted the fix-agent-usage-cache-write-race branch August 9, 2026 11:19
nick1udwig pushed a commit to nick1udwig/omarchy that referenced this pull request Sep 9, 2026
Two Claude collectors running at once both wrote the cache through a temp
path derived from the target, so the second replace found the file already
moved away and crashed the update with a FileNotFoundError.

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
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.

omarchy-agent-usage-claude: race condition in scan_opencode_usage's write_json crashes during omarchy update

2 participants