From 4b8b62ae8344cbc2a42135ca9225089809832873 Mon Sep 17 00:00:00 2001 From: DrakiaXYZ Date: Wed, 28 Feb 2024 07:56:53 +0000 Subject: [PATCH] Utilize the release callback returned by `lockFileSync` to release the lock file (!239) The error people are getting about a lock file already existing is due to `checkFileSync` returning false if the lock file is "stale". The default "stale" timeout is 10 seconds, so if a save takes longer than this, the user will end up in a state where they can no longer save. The documentation for `proper-lockfile` recommends using the callback returned by `lockFileSync` to remove the lock file, so I've switched to using this, and the error no longer occurs with long running save operations Co-authored-by: DrakiaXYZ <565558+TheDgtl@users.noreply.github.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/239 Co-authored-by: DrakiaXYZ Co-committed-by: DrakiaXYZ --- project/src/utils/VFS.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/project/src/utils/VFS.ts b/project/src/utils/VFS.ts index fa24c8ce..8e44fc30 100644 --- a/project/src/utils/VFS.ts +++ b/project/src/utils/VFS.ts @@ -175,7 +175,7 @@ export class VFS fs.writeFileSync(filepath, ""); } - this.lockFileSync(filepath); + const releaseCallback = this.lockFileSync(filepath); if (!append && atomic) { @@ -186,10 +186,7 @@ export class VFS fs.writeFileSync(filepath, data, options); } - if (this.checkFileSync(filepath)) - { - this.unlockFileSync(filepath); - } + releaseCallback(); } public async writeFileAsync(filepath: any, data = "", append = false, atomic = true): Promise @@ -307,12 +304,12 @@ export class VFS await this.renamePromisify(oldPath, newPath); } - protected lockFileSync(filepath: any): void + protected lockFileSync(filepath: any): () => void { - lockfile.lockSync(filepath); + return lockfile.lockSync(filepath); } - protected checkFileSync(filepath: any): any + protected checkFileSync(filepath: any): boolean { return lockfile.checkSync(filepath); }