Skip to content

Commit 3edb8ca

Browse files
authored
Add timeout if saveProfile takes too long (#1120)
Current timeout is 5 seconds, will throw an error to indicate saving was not successful
2 parents 60a41b2 + 5eecb3f commit 3edb8ca

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

project/src/servers/SaveServer.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import { FileSystem } from "@spt/utils/FileSystem";
99
import { HashUtil } from "@spt/utils/HashUtil";
1010
import { JsonUtil } from "@spt/utils/JsonUtil";
1111
import { Timer } from "@spt/utils/Timer";
12-
import { Mutex } from "async-mutex";
12+
import { Mutex, MutexInterface, withTimeout } from "async-mutex";
1313
import { inject, injectAll, injectable } from "tsyringe";
1414

1515
@injectable()
1616
export class SaveServer {
1717
protected profileFilepath = "user/profiles/";
1818
protected profiles: Map<string, ISptProfile> = new Map();
19-
protected profilesBeingSavedMutex: Map<string, Mutex> = new Map();
19+
protected profilesBeingSavedMutex: Map<string, MutexInterface> = new Map();
2020
protected onBeforeSaveCallbacks: Map<string, (profile: ISptProfile) => Promise<ISptProfile>> = new Map();
2121
protected saveSHA1: { [key: string]: string } = {};
2222

@@ -189,17 +189,19 @@ export class SaveServer {
189189
* @returns A promise that resolves when saving is completed.
190190
*/
191191
public async saveProfile(sessionID: string): Promise<void> {
192+
if (!this.profiles.get(sessionID)) {
193+
throw new Error(`Profile ${sessionID} does not exist! Unable to save this profile!`);
194+
}
195+
192196
// Get the current mutex if it exists, create a new one if it doesn't for this profile
193-
const mutex = this.profilesBeingSavedMutex.get(sessionID) || new Mutex();
197+
const mutex =
198+
this.profilesBeingSavedMutex.get(sessionID) ||
199+
withTimeout(new Mutex(), 5000, new Error(`Saving timed out for profile ${sessionID}`));
194200
this.profilesBeingSavedMutex.set(sessionID, mutex);
195201

196202
const release = await mutex.acquire();
197203

198204
try {
199-
if (!this.profiles.get(sessionID)) {
200-
throw new Error(`Profile ${sessionID} does not exist! Unable to save this profile!`);
201-
}
202-
203205
const filePath = `${this.profileFilepath}${sessionID}.json`;
204206

205207
// Run pre-save callbacks before we save into json

0 commit comments

Comments
 (0)