Skip to content

Retry hosts-file writes to survive transient locks - #1

Open
upnorthmedia wants to merge 1 commit into
mainfrom
fix/hosts-file-lock-retry
Open

Retry hosts-file writes to survive transient locks#1
upnorthmedia wants to merge 1 commit into
mainfrom
fix/hosts-file-lock-retry

Conversation

@upnorthmedia

@upnorthmedia upnorthmedia commented May 29, 2026

Copy link
Copy Markdown
Owner

Problem

Restoring (and occasionally applying) failed with:

Set-Content : The process cannot access the file ...\etc\hosts because it is being used by another process.

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

  • Added Invoke-WithRetry (6 attempts, 400ms backoff) for IOException.
  • Routed all three hosts touches through it: backup copy, block removal (Set-Content), block write (Add-Content).
  • A genuinely stuck file now fails with an actionable message ("close any editor with hosts open, pause antivirus, and retry") instead of a raw stack trace.

Parse-checked clean with the PowerShell AST parser.

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability of apply and revert operations by automatically retrying on transient file access failures, reducing failures when system resources are temporarily unavailable.

Review Change Stack

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.
@coderabbitai

coderabbitai Bot commented May 29, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 8525f2f5-2673-49a2-9f3d-b0fc17bf1435

📥 Commits

Reviewing files that changed from the base of the PR and between 9a68d59 and 4c035ac.

📒 Files selected for processing (1)
  • scripts/Block-NvidiaTelemetry.ps1

📝 Walkthrough

Walkthrough

Added an Invoke-WithRetry helper function to retry Windows hosts-file operations on transient I/O locks with configurable backoff. Updated the hosts block removal, backup, and append operations to use this helper, reducing failures when the hosts file is temporarily locked by another process.

Changes

Hosts-file I/O Resilience

Layer / File(s) Summary
Invoke-WithRetry helper function
scripts/Block-NvidiaTelemetry.ps1
Invoke-WithRetry executes a provided scriptblock action and retries on System.IO.IOException with fixed backoff delay; exhaustion throws a detailed error message suggesting common causes such as open editors or antivirus scanning.
Resilient hosts-file operations
scripts/Block-NvidiaTelemetry.ps1
Remove-HostsBlock writes the reverted hosts block via Invoke-WithRetry when not in DryRun mode. Apply-Hosts backs up the hosts file and appends the new hosts block via Invoke-WithRetry when not in DryRun mode, making all three operations tolerant of transient file locks.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

A retry with patience, when hosts-file won't play,
We try, try again—lock it down, lock it away!
PowerShell persists where once it would fail,
With backoff and breathing, no more sorry tales. 🐰✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely describes the main change: adding retry logic for hosts-file write operations to handle transient lock failures, which is the core improvement in this PR.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/hosts-file-lock-retry

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@upnorthmedia
upnorthmedia deleted the fix/hosts-file-lock-retry branch June 9, 2026 14:08
@upnorthmedia
upnorthmedia restored the fix/hosts-file-lock-retry branch June 9, 2026 14:08
@upnorthmedia upnorthmedia reopened this Jun 9, 2026
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