From 706a450fe61c020bcad35ca9edf28dcc4bab14a0 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 26 Aug 2021 11:11:46 +0200 Subject: Fix test event leak --- shared/extra-utils/server/server.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/shared/extra-utils/server/server.ts b/shared/extra-utils/server/server.ts index bc5e1cd5f..f7048ba43 100644 --- a/shared/extra-utils/server/server.ts +++ b/shared/extra-utils/server/server.ts @@ -221,11 +221,17 @@ export class PeerTubeServer { this.app = fork(join(root(), 'dist', 'server.js'), options.peertubeArgs || [], forkOptions) - const onExit = function () { - return rej(new Error('Process exited')) + const onPeerTubeExit = () => rej(new Error('Process exited')) + const onParentExit = () => { + if (!this.app || !this.app.pid) return + + try { + process.kill(self.app.pid) + } catch { /* empty */ } } - this.app.on('exit', onExit) + this.app.on('exit', onPeerTubeExit) + process.on('exit', onParentExit) this.app.stdout.on('data', function onStdout (data) { let dontContinue = false @@ -254,16 +260,11 @@ export class PeerTubeServer { if (options.hideLogs === false) { console.log(data.toString()) } else { + process.removeListener('exit', onParentExit) self.app.stdout.removeListener('data', onStdout) - self.app.removeListener('exit', onExit) + self.app.removeListener('exit', onPeerTubeExit) } - process.on('exit', () => { - try { - process.kill(self.app.pid) - } catch { /* empty */ } - }) - res() }) }) -- cgit v1.2.3