Skip to content

Commit

Permalink
:electron: Adding retries in to the file system writes (#3406)
Browse files Browse the repository at this point in the history
* hardening the file system access by added retrys

* release notees

* remove unneeded console log

* promise-retry instead of my own

* tiny bit more info

* capital U
  • Loading branch information
MikesGlitch authored Sep 9, 2024
1 parent 3985d25 commit 6e8cdb3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
46 changes: 39 additions & 7 deletions packages/loot-core/src/platform/server/fs/index.electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import * as fs from 'fs';
import * as path from 'path';

import promiseRetry from 'promise-retry';

import type * as T from '.';

export { getDocumentDir, getBudgetDir, _setDocumentDir } from './shared';
Expand Down Expand Up @@ -110,13 +112,43 @@ export const readFile: T.ReadFile = (
});
};

export const writeFile: T.WriteFile = (filepath, contents) => {
return new Promise(function (resolve, reject) {
// @ts-expect-error contents type needs refining
fs.writeFile(filepath, contents, 'utf8', function (err) {
return err ? reject(err) : resolve(undefined);
});
});
export const writeFile: T.WriteFile = async (filepath, contents) => {
try {
await promiseRetry(
(retry, attempt) => {
return new Promise((resolve, reject) => {
// @ts-expect-error contents type needs refining
fs.writeFile(filepath, contents, 'utf8', err => {
if (err) {
console.error(
`Failed to write to ${filepath}. Attempted ${attempt} times. Something is locking the file - potentially a virus scanner or backup software.`,
);
reject(err);
} else {
if (attempt > 1) {
console.info(
`Successfully recovered from file lock. It took ${attempt} retries`,
);
}

resolve(undefined);
}
});
}).catch(retry);
},
{
retries: 20,
minTimeout: 100,
maxTimeout: 500,
factor: 1.5,
},
);

return undefined;
} catch (err) {
console.error(`Unable to recover from file lock on file ${filepath}`);
throw err;
}
};

export const removeFile = filepath => {
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/3406.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Maintenance
authors: [MikesGlitch]
---

Adding retries into filesystem calls to mitigate locking issues caused by virus scanners.

0 comments on commit 6e8cdb3

Please sign in to comment.