Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): report a failed force-kill as a 502, not a clean kill

Yudhi Armyndharis15375c02dd21
diff --git a/src/modules/session/session-engine-controls.ts b/src/modules/session/session-engine-controls.ts
index 6e258c874..bbe84d128 100644
--- a/src/modules/session/session-engine-controls.ts
+++ b/src/modules/session/session-engine-controls.ts
@@ -533,8 +533,21 @@ export class SessionEngineControls {
// Await THIS engine's in-flight INITIALIZING write before teardown / the final DISCONNECTED
// write so a delayed pre-initialize status update can never settle after the retirement.
await this.fences.awaitInitialStatus(id, engine);
- await this.fences.teardownEngineSafely(id, engine, e => e.forceDestroy(), 'force-destroy');
+ const tornDown = await this.fences.teardownEngineSafely(id, engine, e => e.forceDestroy(), 'force-destroy');
this.engines.deleteIfLive(id, engine);
+ if (!tornDown) {
+ // As in stop(): local state is settled, but a kill that may have left the process alive is
+ // reported as incomplete, not claimed clean (the controller audits only after a resolve).
+ await this.host.updateStatus(id, SessionStatus.DISCONNECTED);
+ throw new BadGatewayException({
+ statusCode: HttpStatus.BAD_GATEWAY,
+ message:
+ 'Session was stopped locally, but the engine force-kill did not complete: the engine ' +
+ 'process may still be running. Restart the node to reap a leaked process.',
+ error: 'Bad Gateway',
+ code: 'SESSION_FORCE_KILL_INCOMPLETE',
+ });
+ }
this.logger.warn(`Session force-killed: ${session.name}`, {
sessionId: id,
diff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.ts
index e32fbeee3..d98dfd91f 100644
--- a/src/modules/session/session.service.spec.ts
+++ b/src/modules/session/session.service.spec.ts
@@ -656,14 +656,21 @@ describe('SessionService', () => {
expect(result).toBeDefined();
});
- it('forceKill() completes even when forceDestroy() rejects (best-effort recovery)', async () => {
+ it('forceKill() surfaces a 502 when forceDestroy() fails, after settling local state', async () => {
(repository.findOne as jest.Mock).mockResolvedValue(createMockSession());
(repository.update as jest.Mock).mockResolvedValue({ affected: 1 });
const engine = { forceDestroy: jest.fn().mockRejectedValue(new Error('still wedged')) };
enginesOf().set('sess-uuid-1', engine);
- await expect(service.forceKill('sess-uuid-1')).resolves.toBeDefined();
+ const thrown = await service.forceKill('sess-uuid-1').catch((e: unknown) => e);
+
+ // Not reported as a clean kill (the controller audits only a resolved one): the process may live.
+ expect(thrown).toBeInstanceOf(BadGatewayException);
+ expect(((thrown as BadGatewayException).getResponse() as { code?: string }).code).toBe(
+ 'SESSION_FORCE_KILL_INCOMPLETE',
+ );
expect(enginesOf().has('sess-uuid-1')).toBe(false); // map reconciled despite the failure
+ expect(repository.update).toHaveBeenCalledWith('sess-uuid-1', { status: SessionStatus.DISCONNECTED });
});
it('forceKill() throws NotFoundException for an unknown session', async () => {