From 7376f1dc04e4ccb8ce15ed3a1c246349a8fac505 Mon Sep 17 00:00:00 2001 From: Carlos Santos <4a.santos@gmail.com> Date: Tue, 29 Apr 2025 17:49:37 +0200 Subject: [PATCH] backend: Refactor recording lock acquisition logic to prevent unnecessary cleanup on active recordings --- backend/src/services/recording.service.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/backend/src/services/recording.service.ts b/backend/src/services/recording.service.ts index 168ba14..80290db 100644 --- a/backend/src/services/recording.service.ts +++ b/backend/src/services/recording.service.ts @@ -64,6 +64,11 @@ export class RecordingService { let timeoutId: NodeJS.Timeout | undefined; try { + // Attempt to acquire lock. If the lock is not acquired, the recording is already active. + acquiredLock = await this.acquireRoomRecordingActiveLock(roomId); + + if (!acquiredLock) throw errorRecordingAlreadyStarted(roomId); + const room = await this.roomService.getMeetRoom(roomId); if (!room) throw errorRoomNotFound(roomId); @@ -78,11 +83,6 @@ export class RecordingService { if (!hasParticipants) throw errorRoomHasNoParticipants(roomId); - // Attempt to acquire lock. If the lock is not acquired, the recording is already active. - acquiredLock = await this.acquireRoomRecordingActiveLock(roomId); - - if (!acquiredLock) throw errorRecordingAlreadyStarted(roomId); - const startTimeoutPromise = new Promise((_, reject) => { timeoutId = setTimeout(() => { this.systemEventService.off(SystemEventType.RECORDING_ACTIVE, eventListener); @@ -123,10 +123,14 @@ export class RecordingService { throw error; } finally { try { - clearTimeout(timeoutId); - this.systemEventService.off(SystemEventType.RECORDING_ACTIVE, eventListener); - - if (acquiredLock) await this.releaseRecordingLockIfNoEgress(roomId); + if (acquiredLock) { + // Only clean up resources if the lock was successfully acquired. + // This prevents unnecessary cleanup operations when the request was rejected + // due to another recording already in progress in this room. + clearTimeout(timeoutId); + this.systemEventService.off(SystemEventType.RECORDING_ACTIVE, eventListener); + await this.releaseRecordingLockIfNoEgress(roomId); + } } catch (e) { this.logger.warn(`Failed to release recording lock: ${e}`); }