Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): make a start waiting on its claim yield to a force-kill

Yudhi Armyndharis248c4e0549df
diff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.ts
index 0ce3489c5..805e0c35c 100644
--- a/src/modules/session/session.service.spec.ts
+++ b/src/modules/session/session.service.spec.ts
@@ -674,9 +674,11 @@ describe('SessionService', () => {
expect(repository.update).toHaveBeenCalledWith('sess-uuid-1', { status: SessionStatus.DISCONNECTED });
});
- it('forceKill() throws NotFoundException for an unknown session', async () => {
+ it('forceKill() throws NotFoundException for an unknown session and keeps no stop request', async () => {
(repository.findOne as jest.Mock).mockResolvedValue(null);
await expect(service.forceKill('nope')).rejects.toThrow(NotFoundException);
+ // An id that only ever answers 404 must not leave an entry behind.
+ expect((service as unknown as { stopRequests: Map<string, number> }).stopRequests.has('nope')).toBe(false);
});
it('forceKill() rejects with BadRequestException when the session has no live engine', async () => {
@@ -1481,6 +1483,47 @@ describe('SessionService', () => {
expect(ownership.release.mock.calls.length).toBeGreaterThan(releasesBefore);
});
+ // The same race with a force-kill: the start sent while the engine still ran launched a new one
+ // once its claim resolved and cleared the stop record the kill had just written.
+ it('start() refuses with 409 when a forceKill() lands while its claim is pending', async () => {
+ const ownership = withOwnership();
+ const desired = trackDesiredState();
+ (service as unknown as { engines: Map<string, unknown> }).engines.set('sess-uuid-1', {
+ forceDestroy: jest.fn().mockResolvedValue(undefined),
+ });
+ let finishClaim: () => void = () => undefined;
+ ownership.claim.mockImplementationOnce(
+ () => new Promise<boolean>(resolve => (finishClaim = () => resolve(true))),
+ );
+
+ const starting = service.start('sess-uuid-1', { explicit: true });
+ const outcome = starting.catch((error: unknown) => error);
+ await service.forceKill('sess-uuid-1');
+ finishClaim();
+
+ expect(await outcome).toBeInstanceOf(SessionStoppedException);
+ expect(mockEngine.initialize).not.toHaveBeenCalled();
+ expect(lifecycle.isEngineActive('sess-uuid-1')).toBe(false);
+ expect(desired()).toBe('stopped');
+ });
+
+ // A refused force-kill took nothing down, so a start waiting on its claim must still launch.
+ it('start() still launches after a forceKill() refused with 400 while its claim is pending', async () => {
+ const ownership = withOwnership();
+ trackDesiredState();
+ let finishClaim: () => void = () => undefined;
+ ownership.claim.mockImplementationOnce(
+ () => new Promise<boolean>(resolve => (finishClaim = () => resolve(true))),
+ );
+
+ const starting = service.start('sess-uuid-1', { explicit: true });
+ await expect(service.forceKill('sess-uuid-1')).rejects.toThrow(BadRequestException);
+ finishClaim();
+ await starting;
+
+ expect(lifecycle.isEngineActive('sess-uuid-1')).toBe(true);
+ });
+
// The teardown finished while a start sent after it still waited on its claim, and handed back
// the claim that start had just taken: the start then ran its engine on a row no node held.
it.each([
diff --git a/src/modules/session/session.service.ts b/src/modules/session/session.service.ts
index 8fdbcc8aa..2d2f21e59 100644
--- a/src/modules/session/session.service.ts
+++ b/src/modules/session/session.service.ts
@@ -771,12 +771,16 @@ export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicat
}
async forceKill(id: string): Promise<Session> {
+ // Counted like stop(), so a start still waiting on its claim yields rather than relaunching.
+ this.countStopRequest(id);
try {
// The engine kill records the stop itself, once it has an engine to kill.
const session = await this.engineLifecycle.forceKill(id);
await this.releaseAfterTeardown(id);
return session;
} catch (error) {
+ // A 400 "not started" or 404 took nothing down; the 502 did evict the engine and stays counted.
+ if (error instanceof BadRequestException || error instanceof NotFoundException) this.uncountStopRequest(id);
// Same 400 rule as logout(): a "not started" refusal took nothing down.
if (!(error instanceof BadRequestException)) await this.releaseAfterTeardown(id);
throw error;