Repository navigation
fix(desktop): retry the staging rename while Windows holds the folder #2161
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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
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
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
22 changes: 22 additions & 0 deletions
22
surfsense_local/electron/scripts/windows-locks/rename-when-unlocked.mjs
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,22 @@ | ||
| // A rename that waits out Windows holding a file open inside the folder being renamed. | ||
| import { renameSync } from "node:fs" | ||
| import { setTimeout as sleep } from "node:timers/promises" | ||
|
|
||
| // Only Windows answers this way for a file another process holds open; elsewhere they are real permission errors. | ||
| const LOCKED = new Set(["EPERM", "EACCES", "EBUSY"]) | ||
|
|
||
| /** Rename `from` to `to`, retrying while another process holds a file in it, such as a Windows | ||
| * service reading a binary we just ran. 60 s by default: the window graceful-fs waits for the same lock. */ | ||
| export async function renameWhenUnlocked(from, to, { rename = renameSync, timeoutMs = 60_000, platform = process.platform } = {}) { | ||
| const start = Date.now() | ||
| for (let attempt = 1; ; attempt++) { | ||
| try { | ||
| return rename(from, to) | ||
| } catch (error) { | ||
| if (platform !== "win32" || !LOCKED.has(error.code) || Date.now() - start >= timeoutMs) throw error | ||
| if (attempt === 1) console.warn(`rename of ${from} blocked (${error.code}), retrying for up to ${timeoutMs / 1000} s`) | ||
| await sleep(Math.min(10 * attempt, 100)) | ||
| } | ||
| } | ||
| } | ||
|
|
38 changes: 38 additions & 0 deletions
38
surfsense_local/electron/scripts/windows-locks/rename-when-unlocked.test.mjs
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,38 @@ | ||
| import assert from "node:assert/strict" | ||
| import test from "node:test" | ||
|
|
||
| import { renameWhenUnlocked } from "./rename-when-unlocked.mjs" | ||
|
|
||
| /** A stand-in for renameSync that fails with each code in turn, then succeeds. */ | ||
| function renameFailingWith(...codes) { | ||
| return () => { | ||
| const code = codes.shift() | ||
| if (code) throw Object.assign(new Error(`rename failed: ${code}`), { code }) | ||
| } | ||
| } | ||
|
|
||
| test("a rename that fails for another reason is rethrown without a retry", async () => { | ||
| await assert.rejects(renameWhenUnlocked("stage", "out", { rename: renameFailingWith("ENOENT"), platform: "win32" }), { | ||
| code: "ENOENT", | ||
| }) | ||
| }) | ||
|
|
||
| test("a rename Windows refuses twice goes through on the third try", async () => { | ||
| await assert.doesNotReject( | ||
| renameWhenUnlocked("stage", "out", { rename: renameFailingWith("EPERM", "EPERM"), platform: "win32" }), | ||
| ) | ||
| }) | ||
|
|
||
| test("a rename locked for longer than the time limit gives up and rethrows EPERM", async () => { | ||
| const lockedFor24Tries = renameFailingWith(...Array(24).fill("EPERM")) | ||
| await assert.rejects(renameWhenUnlocked("stage", "out", { rename: lockedFor24Tries, timeoutMs: 50, platform: "win32" }), { | ||
| code: "EPERM", | ||
| }) | ||
| }) | ||
|
|
||
| test("off Windows, a refused rename is rethrown without a retry", async () => { | ||
| await assert.rejects( | ||
| renameWhenUnlocked("stage", "out", { rename: renameFailingWith("EPERM"), platform: "linux" }), | ||
| { code: "EPERM" }, | ||
| ) | ||
| }) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: MODSetter/SurfSense
Length of output: 20904
🏁 Script executed:
Repository: MODSetter/SurfSense
Length of output: 3475
Retry the backup rename too.
During a restage, a Windows lock can make the direct
renameSync(OUT, backup)fail beforerenameWhenUnlockedruns. Use the retry helper for the backup move as well.🐛 Suggested fix
🤖 Prompt for AI Agents