Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): do not fail a start or stop over a claim write error
Yudhi Armyndharis188d85685fff
diff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.tsindex b7a605aa2..221bca6e9 100644--- a/src/modules/session/session.service.spec.ts+++ b/src/modules/session/session.service.spec.ts@@ -1718,6 +1718,32 @@ describe('SessionService', () => {expect(ownership.release).toHaveBeenCalledTimes(1);});+ it('start() keeps its own outcome when the claim the last start owes cannot be handed back', async () => {+ const ownership = withOwnership();+ ownership.release.mockRejectedValue(new Error('database unreachable'));+ let finishFirstClaim: () => void = () => undefined;+ ownership.claim+ .mockImplementationOnce(() => new Promise<boolean>(resolve => (finishFirstClaim = () => resolve(true))))+ .mockResolvedValue(true);+ let launchSecond: () => void = () => undefined;+ jest+ .spyOn(lifecycle, 'start')+ .mockImplementationOnce(+ () => new Promise<Session>(resolve => (launchSecond = () => resolve(createMockSession()))),+ )+ .mockRejectedValueOnce(new BadRequestException('refused'));+ jest.spyOn(lifecycle, 'isEngineActive').mockReturnValue(false);++ const first = service.start('sess-uuid-1').catch((error: unknown) => error);+ const second = service.start('sess-uuid-1');+ await new Promise(resolve => setImmediate(resolve));+ finishFirstClaim();+ await first;+ launchSecond();++ await expect(second).resolves.toBeDefined();+ });+it('start() hands the claim back once when two overlapping starts both fail', async () => {const ownership = withOwnership();jest.spyOn(lifecycle, 'start').mockRejectedValue(new BadRequestException('refused'));diff --git a/src/modules/session/session.service.ts b/src/modules/session/session.service.tsindex 2dd73d06c..ad82a3a36 100644--- a/src/modules/session/session.service.ts+++ b/src/modules/session/session.service.ts@@ -975,7 +975,18 @@ export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicatreturn;}this.releaseOwed.delete(id);- await this.ownership.release(id);+ // Best-effort: the outcome of the start or the teardown that asks is already decided, and a claim that+ // cannot be handed back lapses with its lease, which a peer then adopts. A failure here must not+ // replace that outcome (a started session reported as failed, a stopped one as an error).+ try {+ await this.ownership.release(id);+ } catch (error) {+ this.logger.warn(`Could not hand back the claim of session ${id}; it lapses with its lease`, {+ sessionId: id,+ action: 'session_claim_release_failed',+ error: error instanceof Error ? error.message : String(error),+ });+ }}async getQRCode(id: string): Promise<{ qrCode: string; status: SessionStatus }> {