fix(install-state): stop concurrent Windows writers from starving on rename

On Windows, concurrent writeInstallState calls (and real-time antivirus
scans of each freshly published file) can hold replacement sharing locks
for over a second. renameAtomic retried at a fixed 25 ms interval, which
kept contenders in lockstep, so one writer could lose every round and
give up with EPERM at the 2 s deadline. This made
tests/lib/install-state.test.js fail in about half of isolated runs.

- Retry with a uniform random wait of 1-25 ms instead of a fixed 25 ms.
  Exponential backoff was measured and made starvation worse.
- Add a renameRetryBudgetMs option to writeFileAtomic. The default stays
  2 s, so hook callers keep their latency bound, and the attempt cap is
  derived from the budget (81 for the default).
- writeInstallState uses a 10 s budget, since it runs only during installs.

Measured on Windows 11 with Defender on: install-state test 0/12
failures (previously 4/8).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vnegriyandClaude Opus 5.5 committed 2026-11-18 20:47:15 +03:00
1 parent 3f14a103a8
commit aa92c3ec1e
3 files changed
+55 -8

No files matched your search

+22 -5
View File
@@ -25,10 +25,27 @@ function readFileWithSharingRetry(filePath, options) {
}
}
// Concurrent Windows writers (and real-time antivirus scans of each freshly
// published file) can hold replacement sharing locks for well over a second.
// Fixed-interval retries keep contenders in lockstep, so one writer can lose
// every round until it gives up. Short, fully jittered waits break that
// lockstep; longer (exponential) waits measurably starve the loser further.
const DEFAULT_RENAME_RETRY_BUDGET_MS = 2000;
const RENAME_RETRY_MIN_WAIT_MS = 1;
const RENAME_RETRY_MAX_WAIT_MS = 25;
function renameRetryWaitMs(remaining) {
const jittered = RENAME_RETRY_MIN_WAIT_MS
+ Math.random() * (RENAME_RETRY_MAX_WAIT_MS - RENAME_RETRY_MIN_WAIT_MS);
return Math.min(remaining, jittered);
}
function renameAtomic(tempPath, resolvedPath, options) {
// Concurrent Windows installers can hold replacement sharing locks beyond
// the reader's shorter retry budget. Keep publication bounded to two seconds.
const deadline = performance.now() + 2000;
// Keep publication bounded by an elapsed budget (callers that are not on a
// latency-sensitive hook path may raise it) and a hard attempt cap.
const budget = options.renameRetryBudgetMs || DEFAULT_RENAME_RETRY_BUDGET_MS;
const maxAttempts = Math.ceil(budget / 25) + 1;
const deadline = performance.now() + budget;
let attempts = 0;
let sleeper;
while (true) {
@@ -44,9 +61,9 @@ function renameAtomic(tempPath, resolvedPath, options) {
const remaining = deadline - performance.now();
if (process.platform !== 'win32'
|| !['EPERM', 'EACCES', 'EBUSY'].includes(error.code)
|| attempts >= 81 || remaining <= 0) throw error;
|| attempts >= maxAttempts || remaining <= 0) throw error;
sleeper ||= new Int32Array(new SharedArrayBuffer(4));
Atomics.wait(sleeper, 0, 0, Math.min(25, remaining));
Atomics.wait(sleeper, 0, 0, renameRetryWaitMs(remaining));
}
}
}
+3 -1
View File
@@ -370,7 +370,9 @@ function readInstallState(filePath) {
function writeInstallState(filePath, state) {
assertValidInstallState(state, filePath);
const { writeFileAtomic } = require('./atomic-write');
writeFileAtomic(filePath, `${JSON.stringify(state, null, 2)}\n`);
// Installs are not on a latency-sensitive hook path; concurrent installers
// on Windows need room to outlast each other's replacement locks.
writeFileAtomic(filePath, `${JSON.stringify(state, null, 2)}\n`, { renameRetryBudgetMs: 10000 });
return state;
}