Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(media): kill ffmpeg runs from the media service's shutdown hook

Yudhi Armyndharis70801f3a7468
diff --git a/src/modules/media/ffmpeg-run.spec.ts b/src/modules/media/ffmpeg-run.spec.ts
index 257bf9cc7..8d9b4c737 100644
--- a/src/modules/media/ffmpeg-run.spec.ts
+++ b/src/modules/media/ffmpeg-run.spec.ts
@@ -174,7 +174,8 @@ esac
expect(process.listeners('exit')).toContain(killRunningConversions);
const pidFile = `${stubPath}.pid`;
await rm(pidFile, { force: true });
- const run = runFfmpeg(Buffer.from('input'), 'bin', 'ogg', mode('orphan'), options({ timeoutMs: 10_000 }));
+ // The run's own timeout is longer than this test may take, so only the kill below can end it.
+ const run = runFfmpeg(Buffer.from('input'), 'bin', 'ogg', mode('orphan'), options({ timeoutMs: 60_000 }));
const settled = run.catch((error: unknown) => error);
let grandchild = 0;
@@ -191,10 +192,13 @@ esac
}
};
try {
+ // The stub's own sleep ends in 10 s, so the kill has to land well before that to count.
+ const killedAt = Date.now();
killRunningConversions();
expect(await settled).toBeInstanceOf(FfmpegConversionError);
for (let i = 0; i < 40 && alive(); i++) await new Promise(r => setTimeout(r, 50));
expect(alive()).toBe(false);
+ expect(Date.now() - killedAt).toBeLessThan(2_000);
} finally {
if (alive()) process.kill(grandchild, 'SIGKILL');
}
diff --git a/src/modules/media/ffmpeg.ts b/src/modules/media/ffmpeg.ts
index f87ff4439..bfd87ab9c 100644
--- a/src/modules/media/ffmpeg.ts
+++ b/src/modules/media/ffmpeg.ts
@@ -213,8 +213,9 @@ const runningGroups = new Set<number>();
/**
* SIGKILL every ffmpeg process group still running. Each run is detached into its own group, so a
* Ctrl-C or a signal to the gateway's group no longer reaches it, and its timeout dies with this
- * process. Registered on `exit`, which every shutdown path reaches; `process.kill` is synchronous, so
- * it still runs there.
+ * process. Called from the media service's shutdown hook, which Nest runs before it re-raises a
+ * signal (the process then dies without emitting `exit`), and registered on `exit` for every path
+ * that ends in `process.exit`; `process.kill` is synchronous, so it still runs there.
*/
export function killRunningConversions(): void {
for (const pid of runningGroups) {
diff --git a/src/modules/media/media-conversion.service.spec.ts b/src/modules/media/media-conversion.service.spec.ts
index 1335e331a..087f107c0 100644
--- a/src/modules/media/media-conversion.service.spec.ts
+++ b/src/modules/media/media-conversion.service.spec.ts
@@ -345,4 +345,16 @@ describe('MediaConversionService', () => {
expect(runFfmpeg).toHaveBeenCalledTimes(5);
});
});
+
+ describe('shutdown', () => {
+ // Nest's own signal handler re-raises the signal after its hooks run, so the process dies from it
+ // and never emits `exit`. The shutdown hook is what still reaches the detached ffmpeg groups then.
+ it('kills the running ffmpeg groups when the application shuts down', () => {
+ const kill = jest.spyOn(ffmpeg, 'killRunningConversions').mockImplementation(() => undefined);
+
+ makeService().onApplicationShutdown();
+
+ expect(kill).toHaveBeenCalledTimes(1);
+ });
+ });
});
diff --git a/src/modules/media/media-conversion.service.ts b/src/modules/media/media-conversion.service.ts
index 677c2f7a8..4a044452a 100644
--- a/src/modules/media/media-conversion.service.ts
+++ b/src/modules/media/media-conversion.service.ts
@@ -1,4 +1,4 @@
-import { BadRequestException, Injectable, ServiceUnavailableException } from '@nestjs/common';
+import { BadRequestException, Injectable, OnApplicationShutdown, ServiceUnavailableException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { InjectRepository } from '@nestjs/typeorm';
import { isUUID } from 'class-validator';
@@ -13,6 +13,7 @@ import { assertBase64WithinMediaCap, stripBase64DataUri } from '../message/media
import {
FfmpegConversionError,
FfmpegSpawnError,
+ killRunningConversions,
probeFfmpeg,
runFfmpeg,
videoEncodeArgs,
@@ -31,7 +32,7 @@ export interface ConvertedMedia {
}
@Injectable()
-export class MediaConversionService {
+export class MediaConversionService implements OnApplicationShutdown {
private readonly logger = createLogger('MediaConversionService');
/**
* Result of the binary probe. Only a successful probe is kept: a failure can be a timeout or a
@@ -56,6 +57,15 @@ export class MediaConversionService {
this.ffmpegGate = new ConcurrencyLimiter(concurrency, concurrency * 4);
}
+ /**
+ * Kill the detached ffmpeg groups still running. Nest's own signal handler re-raises the signal
+ * once its hooks finish, so the process dies from it without ever emitting `exit`; this hook runs
+ * on that path and on `app.close()`.
+ */
+ onApplicationShutdown(): void {
+ killRunningConversions();
+ }
+
/**
* Convert to a WhatsApp voice note: Ogg/Opus, which is what produces a playable mic bubble.
*