-
-
Notifications
You must be signed in to change notification settings - Fork 36.1k
fs: treat std::errc::permission_denied as EPERM error
#64698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PickBas
wants to merge
2
commits into
nodejs:main
Choose a base branch
from
JaneaSystems:win-rmsync-eperm-retry-test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+91
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,85 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
|
|
||
| if (!common.isWindows) | ||
| common.skip('Windows-specific: EPERM sharing-violation retry in rmSync'); | ||
|
|
||
| const tmpdir = require('../common/tmpdir'); | ||
| const assert = require('assert'); | ||
| const { once } = require('events'); | ||
| const { spawn } = require('child_process'); | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
|
|
||
| tmpdir.refresh(); | ||
|
|
||
| // UV_FS_O_EXLOCK opens with share mode 0, so deletion fails with EPERM | ||
| // until this process kills the child. | ||
| const lockerScript = ` | ||
| const fs = require('fs'); | ||
| const UV_FS_O_EXLOCK = 0x10000000; | ||
| fs.openSync(process.argv[1], fs.constants.O_RDWR | UV_FS_O_EXLOCK); | ||
| process.stdout.write('locked'); | ||
| setInterval(() => {}, 60_000); | ||
| `; | ||
|
|
||
| async function spawnLocker(file) { | ||
| const child = spawn(process.execPath, ['-e', lockerScript, file], | ||
| { stdio: ['ignore', 'pipe', 'inherit'] }); | ||
| const [data] = await once(child.stdout, 'data'); | ||
| assert.strictEqual(data.toString(), 'locked'); | ||
| return child; | ||
| } | ||
|
|
||
| // Sleep before retry i is i * retryDelay ms, so all retries take at least | ||
| // retryDelay * (1 + 2 + ... + maxRetries) ms. | ||
| function minRetryTime({ maxRetries, retryDelay }) { | ||
| return retryDelay * maxRetries * (maxRetries + 1) / 2; | ||
| } | ||
|
|
||
| function timedRmThrowsEPERM(dir, options) { | ||
| const start = Date.now(); | ||
| assert.throws(() => { | ||
| fs.rmSync(dir, { recursive: true, ...options }); | ||
| }, { | ||
| code: 'EPERM', | ||
| name: 'Error', | ||
| syscall: 'rm', | ||
| }); | ||
| return Date.now() - start; | ||
| } | ||
|
|
||
| (async () => { | ||
| const dir = tmpdir.resolve('rm-eperm-retries'); | ||
| const file = path.join(dir, 'locked.txt'); | ||
| fs.mkdirSync(dir); | ||
| fs.writeFileSync(file, 'hello'); | ||
|
|
||
| const child = await spawnLocker(file); | ||
| try { | ||
| // Proves the lock is effective: no retries means an immediate EPERM. | ||
| timedRmThrowsEPERM(dir, { maxRetries: 0, retryDelay: 0 }); | ||
| assert.strictEqual(fs.existsSync(file), true); | ||
|
|
||
| const options = { maxRetries: 4, retryDelay: 100 }; | ||
| const expected = minRetryTime(options); // 100+200+300+400 = 1000 ms. | ||
| const elapsed = timedRmThrowsEPERM(dir, options); | ||
|
|
||
| // Windows timer granularity may shave a few ms off each Sleep() call. | ||
| const slack = 16 * options.maxRetries; | ||
| assert.ok(elapsed >= expected - slack, | ||
| `rmSync() gave up after ${elapsed}ms; expected it to spend at ` + | ||
| `least ~${expected}ms on ${options.maxRetries} retries of ` + | ||
| `${options.retryDelay}ms escalating delay`); | ||
|
|
||
| // Catches unit confusion (e.g. seconds vs. milliseconds) in the delay. | ||
| assert.ok(elapsed < common.platformTimeout(expected * 10), | ||
| `rmSync() gave up after ${elapsed}ms; expected roughly ` + | ||
| `${expected}ms for ${options.maxRetries} retries`); | ||
| } finally { | ||
| child.kill(); | ||
| } | ||
| await once(child, 'exit'); | ||
| assert.strictEqual(fs.existsSync(file), true); | ||
| })().then(common.mustCall()); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.