Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): drop a refused account's engine before logging it out
Yudhi Armyndharis0cc765c91bfe
diff --git a/src/modules/session/session-engine-lifecycle-races.spec.ts b/src/modules/session/session-engine-lifecycle-races.spec.tsindex 9c2b77d25..4cdbb482b 100644--- a/src/modules/session/session-engine-lifecycle-races.spec.ts+++ b/src/modules/session/session-engine-lifecycle-races.spec.ts@@ -187,4 +187,45 @@ describe('SessionEngineLifecycle races', () => {expect(internals.engines.has(ID)).toBe(true);});});++ describe('rejectRebind', () => {+ const failedWrites = (): number =>+ repository.update.mock.calls.filter(+ ([, patch]) => (patch as { status?: unknown }).status === SessionStatus.FAILED,+ ).length;++ it("retires the refused account's engine before its logout runs", async () => {+ const logout = deferred();+ const engine = { ...makeEngine(), logout: jest.fn().mockReturnValue(logout.promise) };+ internals.engines.set(ID, engine as never);++ const rejecting = internals.rejectRebind(ID, engine, NAME, '628111', '628999');+ await flush();++ // Every inbound callback is gated on this, so nothing the refused account sends while the+ // logout runs is stored or dispatched under this session.+ expect(engine.logout).toHaveBeenCalledTimes(1);+ expect(internals.engines.isLive(ID, engine as never)).toBe(false);++ logout.resolve();+ await rejecting;+ expect(failedWrites()).toBe(1);+ });++ it('leaves a start that registered an engine during the logout alone', async () => {+ const logout = deferred();+ const engine = { ...makeEngine(), logout: jest.fn().mockReturnValue(logout.promise) };+ const replacement = makeEngine();+ internals.engines.set(ID, engine as never);++ const rejecting = internals.rejectRebind(ID, engine, NAME, '628111', '628999');+ await flush();+ internals.engines.set(ID, replacement as never);+ logout.resolve();+ await rejecting;++ expect(internals.engines.get(ID)).toBe(replacement);+ expect(failedWrites()).toBe(0);+ });+ });});diff --git a/src/modules/session/session-engine-lifecycle.service.ts b/src/modules/session/session-engine-lifecycle.service.tsindex 6975cc03b..d365956fc 100644--- a/src/modules/session/session-engine-lifecycle.service.ts+++ b/src/modules/session/session-engine-lifecycle.service.ts@@ -814,13 +814,16 @@ export class SessionEngineLifecycle {metadata: { previousPhone, incomingPhone },errorMessage: reason,});+ // Evicted before the logout, which can take up to its 10s deadline: every inbound callback is gated+ // on the live engine, so the refused account's messages and history sync, which arrive right after+ // the link opens, are dropped instead of stored and dispatched under this session. A start() racing+ // the logout still waits for it: the logout registers its credential fence in this same tick.+ this.engines.deleteIfLive(id, engine);// logout() wipes the wrong account's credentials and removes this device from that account.await this.teardownEngineSafely(id, engine, e => e.logout(), 'logout', sessionName);// Re-fence after the await, as handleEngineDisconnected does: a concurrent start() may have- // replaced the engine while logout ran. If this handler is now stale it must not delete the new- // engine or write FAILED over a fresh start.- if (!this.isLiveEngine(id, engine)) return;- this.engines.deleteIfLive(id, engine);+ // registered an engine while logout ran. If so it must not get FAILED written over it.+ if (this.engines.has(id)) return;// Fenced on ownership like every engine-driven terminal write: a lapsed lease must not park a// peer's session unrecoverably in FAILED. Defensively caught like handleEngineReady's row write.if (this.ownsSession(id)) {