Retry hosts-file writes to survive transient locks - #1
Conversation
Restoring (and applying) occasionally failed with "The process cannot access the file ...\etc\hosts because it is being used by another process." This is a transient lock from Defender real-time scanning, the DNS Client service, or an open editor, so it only happened sometimes. Add Invoke-WithRetry (6 attempts, 400ms backoff) and route the backup copy, block removal (Set-Content) and block write (Add-Content) through it. A genuinely stuck file now fails with an actionable message instead of a raw IOException stack trace.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughAdded an ChangesHosts-file I/O Resilience
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Problem
Restoring (and occasionally applying) failed with:
It was intermittent because the lock is transient — Defender real-time scanning the hosts file, the DNS Client service, or an editor left open holds it for a moment, and the single write attempt threw and aborted the whole revert.
Fix
Invoke-WithRetry(6 attempts, 400ms backoff) forIOException.Set-Content), block write (Add-Content).Parse-checked clean with the PowerShell AST parser.
Summary by CodeRabbit