Skip to content

fix: retry the cache info rename while Windows reports the file in use - #537

Open
mtallenca wants to merge 1 commit into
Baseflow:mainfrom
tecarta:fix/windows-cache-info-rename-retry
Open

mtallenca wants to merge 1 commit into
Baseflow:mainfrom
tecarta:fix/windows-cache-info-rename-retry

Conversation

@mtallenca

Copy link
Copy Markdown

✨ What kind of change does this PR introduce? (Bug fix, feature, docs update...)

Bug fix.

⤵️ What is the current behavior?

On Windows, JsonCacheInfoRepository's atomic write (temp file + rename, since 3.4.3) fails with PathAccessException: Cannot rename file ... (OS Error: The process cannot access the file because it is being used by another process, errno = 32) whenever another process has the cache info file or the fresh temp file open without delete sharing. Antivirus and the search indexer routinely do that just after a file is written. The write is reported through FlutterError and stays pending until the next change, so it's lost if the app exits first. We see this in production at app startup.

🆕 What is the new behavior (if this is a feature change)?

When the rename fails with ERROR_SHARING_VIOLATION (32) or ERROR_ACCESS_DENIED (5), it's retried four times, with pauses of 50, 100, 200 and 400 ms (about 750 ms in all), before the failure is reported as before. Other errors are not retried.

💥 Does this PR introduce a breaking change?

No

🐛 Recommendations for testing

New tests use a forwarding file system whose rename fails a set number of times: a refusal that clears is retried and persisted with no error reported, persistent refusals are reported after five attempts, and other error codes are not retried. The first two fail without the change. flutter test, flutter analyze and dart format --set-exit-if-changed . pass locally. Not run on a Windows machine with a real file lock.

📝 Links to relevant issues/docs

MoveFileEx (used by File.rename on Windows), system error codes.

🤔 Checklist before submitting

  • All projects build
  • Follows style guide lines (code style guide)
  • Relevant documentation was updated
  • Rebased onto current main
  • Version bumped and dated CHANGELOG entry added

JsonCacheInfoRepository writes through a temp file and renames it over the
cache info file. Windows refuses that rename while another process holds
either file open without delete sharing, which antivirus and the search
indexer routinely do just after a file is written, so the write failed
with ERROR_SHARING_VIOLATION and stayed pending until the next change.

The rename is now retried a few times, about 750ms in all, when it fails
with ERROR_SHARING_VIOLATION (32) or ERROR_ACCESS_DENIED (5).
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.

1 participant