Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): release the claim when a stop ends incomplete

Yudhi Armyndhariseafd364c3b29
diff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.ts
index d3d9e04ee..e5338ef7b 100644
--- a/src/modules/session/session.service.spec.ts
+++ b/src/modules/session/session.service.spec.ts
@@ -1396,6 +1396,39 @@ describe('SessionService', () => {
expect(ownership.release).toHaveBeenCalledWith('sess-uuid-1');
});
+ // The incomplete stop evicted the engine and wrote DISCONNECTED. A claim left to lapse still names
+ // this node, and a peer's takeover sweep adopts exactly such a row: it would restart the session
+ // the operator just stopped. A released claim is not adopted.
+ it('stop() releases on the 502 SESSION_STOP_INCOMPLETE path when nothing stays alive here', async () => {
+ const ownership = withOwnership();
+ jest.spyOn(lifecycle, 'stop').mockRejectedValue(new BadGatewayException({ code: 'SESSION_STOP_INCOMPLETE' }));
+ jest.spyOn(lifecycle, 'isEngineActive').mockReturnValue(false);
+
+ await expect(service.stop('sess-uuid-1')).rejects.toThrow(BadGatewayException);
+
+ expect(ownership.release).toHaveBeenCalledWith('sess-uuid-1');
+ });
+
+ it('stop() keeps the claim on the 502 path when a concurrent start still holds the session here', async () => {
+ const ownership = withOwnership();
+ jest.spyOn(lifecycle, 'stop').mockRejectedValue(new BadGatewayException({ code: 'SESSION_STOP_INCOMPLETE' }));
+ jest.spyOn(lifecycle, 'isEngineActive').mockReturnValue(true);
+
+ await expect(service.stop('sess-uuid-1')).rejects.toThrow(BadGatewayException);
+
+ expect(ownership.release).not.toHaveBeenCalled();
+ });
+
+ it('stop() keeps the claim when it fails for any other reason', async () => {
+ const ownership = withOwnership();
+ jest.spyOn(lifecycle, 'stop').mockRejectedValue(new NotFoundException('Session not found'));
+ jest.spyOn(lifecycle, 'isEngineActive').mockReturnValue(false);
+
+ await expect(service.stop('sess-uuid-1')).rejects.toThrow(NotFoundException);
+
+ expect(ownership.release).not.toHaveBeenCalled();
+ });
+
it('forceKill() hands the claim back after the kill', async () => {
const ownership = withOwnership();
jest.spyOn(lifecycle, 'forceKill').mockResolvedValue(createMockSession());
diff --git a/src/modules/session/session.service.ts b/src/modules/session/session.service.ts
index 0bc192c12..9a97ef0df 100644
--- a/src/modules/session/session.service.ts
+++ b/src/modules/session/session.service.ts
@@ -1,4 +1,5 @@
import {
+ BadGatewayException,
HttpException,
Injectable,
NotFoundException,
@@ -595,10 +596,11 @@ export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicat
if (this.ownership) await this.assertNotHeldElsewhere(id);
session = await this.engineLifecycle.stop(id);
} catch (error) {
- // Deliberately no release here, unlike logout()/forceKill(): this catch also carries the
- // foreign-node 409, where the claim is the peer's and a blanket release would delete it. The
- // local-502 path keeps the claim, and only claims with a live engine are renewed — it lapses
- // at lease TTL instead of pinning the session here.
+ // Only the local 502 (SESSION_STOP_INCOMPLETE) releases: it evicted the engine and wrote
+ // DISCONNECTED, and a claim left to lapse still names this node, so a peer's takeover sweep
+ // would adopt the row and start the session the operator just stopped. A released claim is
+ // not adopted. The foreign-node 409 keeps its claim (it is the peer's), and so does a 404.
+ if (error instanceof BadGatewayException) await this.releaseUnlessEngineActive(id);
this.discardStopMarkForMissingSession(id, error);
throw error;
}