-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(windows): workaround for intermittent locks on windows when renam…
…ing files
- Loading branch information
1 parent
936becb
commit c894f7e
Showing
3 changed files
with
127 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
MIT License | ||
Copyright (c) 2015 - present Microsoft Corporation | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
// This file is based on https://github.com/microsoft/vscode/blob/f860fcf11022f10a992440fd54c6e45674e39617/src/vs/base/node/pfs.ts | ||
// See the LICENSE at the top of the file | ||
|
||
import * as fs from 'fs-extra' | ||
|
||
/** | ||
* A drop-in replacement for `fs.rename` that: | ||
* - allows to move across multiple disks | ||
* - attempts to retry the operation for certain error codes on Windows | ||
*/ | ||
export async function rename( | ||
source: string, | ||
target: string, | ||
windowsRetryTimeout: number | false = 60000 /* matches graceful-fs */ | ||
): Promise<void> { | ||
if (source === target) { | ||
return // simulate node.js behaviour here and do a no-op if paths match | ||
} | ||
|
||
if (process.platform === 'win32' && typeof windowsRetryTimeout === 'number') { | ||
// On Windows, a rename can fail when either source or target | ||
// is locked by AV software. We do leverage fs-extra/graceful-fs to iron | ||
// out these issues, however in case the target file exists, | ||
// fs-extra/graceful-fs will immediately return without retry for fs.rename(). | ||
await renameWithRetry(source, target, Date.now(), windowsRetryTimeout) | ||
} else { | ||
await fs.rename(source, target) | ||
} | ||
} | ||
|
||
async function renameWithRetry( | ||
source: string, | ||
target: string, | ||
startTime: number, | ||
retryTimeout: number, | ||
attempt = 0 | ||
): Promise<void> { | ||
try { | ||
return await fs.rename(source, target) | ||
} catch (error: any) { | ||
if ( | ||
error.code !== 'EACCES' && | ||
error.code !== 'EPERM' && | ||
error.code !== 'EBUSY' | ||
) { | ||
throw error // only for errors we think are temporary | ||
} | ||
|
||
if (Date.now() - startTime >= retryTimeout) { | ||
console.error( | ||
`[node.js fs] rename failed after ${attempt} retries with error: ${error}` | ||
) | ||
|
||
throw error // give up after configurable timeout | ||
} | ||
|
||
if (attempt === 0) { | ||
let abortRetry = false | ||
try { | ||
const stat = await fs.stat(target) | ||
if (!stat.isFile()) { | ||
abortRetry = true // if target is not a file, EPERM error may be raised and we should not attempt to retry | ||
} | ||
} catch (error) { | ||
// Ignore | ||
} | ||
|
||
if (abortRetry) { | ||
throw error | ||
} | ||
} | ||
|
||
// Delay with incremental backoff up to 100ms | ||
await timeout(Math.min(100, attempt * 10)) | ||
|
||
// Attempt again | ||
return renameWithRetry(source, target, startTime, retryTimeout, attempt + 1) | ||
} | ||
} | ||
|
||
const timeout = (millis: number) => | ||
new Promise((resolve) => setTimeout(resolve, millis)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { unlink, writeFile } from 'fs-extra' | ||
import { rename } from './rename' | ||
|
||
export async function writeFileAtomic( | ||
filePath: string, | ||
content: string | ||
): Promise<void> { | ||
const tempPath = filePath + '.tmp.' + Math.random().toString(36).slice(2) | ||
try { | ||
await writeFile(tempPath, content, 'utf-8') | ||
await rename(tempPath, filePath) | ||
} catch (e) { | ||
try { | ||
await unlink(tempPath) | ||
} catch { | ||
// ignore | ||
} | ||
throw e | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters