@@ -9,14 +9,14 @@ import { FileSystem } from "@spt/utils/FileSystem";
9
9
import { HashUtil } from "@spt/utils/HashUtil" ;
10
10
import { JsonUtil } from "@spt/utils/JsonUtil" ;
11
11
import { Timer } from "@spt/utils/Timer" ;
12
- import { Mutex } from "async-mutex" ;
12
+ import { Mutex , MutexInterface , withTimeout } from "async-mutex" ;
13
13
import { inject , injectAll , injectable } from "tsyringe" ;
14
14
15
15
@injectable ( )
16
16
export class SaveServer {
17
17
protected profileFilepath = "user/profiles/" ;
18
18
protected profiles : Map < string , ISptProfile > = new Map ( ) ;
19
- protected profilesBeingSavedMutex : Map < string , Mutex > = new Map ( ) ;
19
+ protected profilesBeingSavedMutex : Map < string , MutexInterface > = new Map ( ) ;
20
20
protected onBeforeSaveCallbacks : Map < string , ( profile : ISptProfile ) => Promise < ISptProfile > > = new Map ( ) ;
21
21
protected saveSHA1 : { [ key : string ] : string } = { } ;
22
22
@@ -189,17 +189,19 @@ export class SaveServer {
189
189
* @returns A promise that resolves when saving is completed.
190
190
*/
191
191
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
+
192
196
// 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 } ` ) ) ;
194
200
this . profilesBeingSavedMutex . set ( sessionID , mutex ) ;
195
201
196
202
const release = await mutex . acquire ( ) ;
197
203
198
204
try {
199
- if ( ! this . profiles . get ( sessionID ) ) {
200
- throw new Error ( `Profile ${ sessionID } does not exist! Unable to save this profile!` ) ;
201
- }
202
-
203
205
const filePath = `${ this . profileFilepath } ${ sessionID } .json` ;
204
206
205
207
// Run pre-save callbacks before we save into json
0 commit comments