From ea1c008ded7bcbf16e2ff03f9fd8106605be647f Mon Sep 17 00:00:00 2001 From: John Livingston Date: Mon, 11 Sep 2023 09:30:20 +0200 Subject: [PATCH] Fix: readData must have an atomic part. --- server/lib/room-channel/room-channel-class.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/server/lib/room-channel/room-channel-class.ts b/server/lib/room-channel/room-channel-class.ts index 6c2c13d2..c8fbef54 100644 --- a/server/lib/room-channel/room-channel-class.ts +++ b/server/lib/room-channel/room-channel-class.ts @@ -87,9 +87,6 @@ class RoomChannel { public async readData (): Promise { // Reading the data file (see https://livingston.frama.io/peertube-plugin-livechat/fr/technical/data/) - this.room2Channel.clear() - this.channel2Rooms.clear() - let content: string try { content = (await fs.promises.readFile(this.dataFilePath)).toString() @@ -107,6 +104,18 @@ class RoomChannel { return false } + // This part must be done atomicly: + return this._readData(data) + } + + /** + * _readData is the atomic part of readData: + * once the date are read from disk, object data must be emptied and filled atomicly. + */ + protected _readData (data: any): boolean { + this.room2Channel.clear() + this.channel2Rooms.clear() + if (typeof data !== 'object') { this.logger.error('Invalid room-channel data file content') return false