ActivityPub-federated video streaming platform using P2P directly in your web browser
Correctly cleanup lives on error
Chocobozzzbf623becd4ac
diff --git a/server/core/lib/live/live-manager.ts b/server/core/lib/live/live-manager.tsindex 1e10c1cccf..dcbcd2296f 100644--- a/server/core/lib/live/live-manager.ts+++ b/server/core/lib/live/live-manager.ts@@ -431,7 +431,15 @@ class LiveManager {const audioOnlyOutput = allResolutions.every(r => r === VideoResolution.H_NOVIDEO)- const liveSession = await this.saveStartingSession(videoLive)+ let liveSession: VideoLiveSessionModel+ try {+ liveSession = await this.saveStartingSession(videoLive)+ } catch (err) {+ logger.error('Cannot save starting live session.', { err, ...localLTags })++ this.videoSessions.delete(videoUUID)+ return this.abortSession(sessionId)+ }LiveQuotaStore.Instance.addNewLive(user.id, sessionId)@@ -511,6 +519,9 @@ class LiveManager {logger.error('Cannot run muxing.', { err, ...localLTags })this.muxingSessions.delete(sessionId)++ LiveQuotaStore.Instance.removeLive(user.id, sessionId)+muxingSession.destroy()this.stopSessionOfVideo({diff --git a/server/core/lib/live/shared/muxing-session.ts b/server/core/lib/live/shared/muxing-session.tsindex 7dbf6f336a..d47312e70e 100644--- a/server/core/lib/live/shared/muxing-session.ts+++ b/server/core/lib/live/shared/muxing-session.ts@@ -109,6 +109,7 @@ class MuxingSession extends EventEmitter implements MuxingSession {private liveReady = falseprivate aborted = false+ private cleanupScheduled = falseprivate readonly isAbleToUploadVideoWithCache = memoizee((channelUserId: number) => {return isUserQuotaValid({ channelUserId, uploadSize: 1000 })@@ -205,11 +206,25 @@ class MuxingSession extends EventEmitter implements MuxingSession {}destroy () {+ // Ensure the files watcher is always closed, even when the cleanup was never scheduled+ // (e.g. an ffmpeg error makes the transcoding wrapper abort() short-circuit without emitting 'end')+ this.closeWatcher()+ .catch(err => logger.error('Cannot close files watcher of %s.', this.outDirectory, { err, ...this.lTags() }))+this.removeAllListeners()this.isAbleToUploadVideoWithCache.clear()this.hasClientSocketInBadHealthWithCache.clear()}+ private closeWatcher () {+ if (!this.filesWatcher) return Promise.resolve()++ const watcher = this.filesWatcher+ this.filesWatcher = undefined++ return watcher.close()+ }+private watchMasterFile () {this.filesWatcher.on('add', async path => {if (path !== join(this.outDirectory, this.streamingPlaylist.playlistFilename)) return@@ -432,6 +447,10 @@ class MuxingSession extends EventEmitter implements MuxingSession {private onTranscodingError () {this.emit('transcoding-error', { videoUUID: this.videoUUID })++ // On ffmpeg error the transcoding wrapper abort() short-circuits and never emits 'end'+ // So schedule the cleanup here too+ this.scheduleCleanup()}private onTranscodedEnded () {@@ -440,14 +459,17 @@ class MuxingSession extends EventEmitter implements MuxingSession {// Don't log the input URL, which contains the stream key (a long lived secret)logger.info('RTMP transmuxing for video %s ended. Scheduling cleanup', this.videoUUID, this.lTags())- setTimeout(() => {- // Wait latest segments generation, and close watchers+ this.scheduleCleanup()+ }- const promise = this.filesWatcher- ? this.filesWatcher.close()- : Promise.resolve()+ private scheduleCleanup () {+ // Cleanup can be triggered by both the transcoding end and error paths: only run it once+ if (this.cleanupScheduled) return+ this.cleanupScheduled = true- promise+ setTimeout(() => {+ // Wait latest segments generation, and close watchers+ this.closeWatcher().then(() => {// Process remaining segments hashfor (const key of Object.keys(this.segmentsToProcessPerPlaylist)) {