Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(baileys): drop a failed reject's handle after a teardown
Yudhi Armyndharis10c33598a8b2
diff --git a/src/engine/adapters/baileys-events.ts b/src/engine/adapters/baileys-events.tsindex 75a0501f8..149d9bfa2 100644--- a/src/engine/adapters/baileys-events.ts+++ b/src/engine/adapters/baileys-events.ts@@ -166,6 +166,17 @@ export interface BaileysEventsHost {getOnCallOutcome(): EngineEventCallbacks['onCallOutcome'];}+/** Every teardown clears the live-call map; counting those clears lets a reject in flight tell that its+ * connection was torn down meanwhile. */+class LiveCallMap<V> extends Map<string, V> {+ clears = 0;++ override clear(): void {+ this.clears++;+ super.clear();+ }+}+export class BaileysEvents {/** How long a received call's handle stays rejectable. Calls ring for roughly a minute, so* two minutes covers the ringing window with margin without pinning dead calls for long. */@@ -174,10 +185,13 @@ export class BaileysEvents {/** Live incoming calls by call id, holding the raw `from` JID sock.rejectCall() needs — the* call event is long gone by the time a reject arrives, so it must be cached at event time.* Readonly reference, owned here; the adapter's lifecycle clears it on teardown. */- readonly liveCalls = new Map<- string,- { callFrom: string; expiresAt: number; from: string; isVideo: boolean; isGroup: boolean }- >();+ readonly liveCalls = new LiveCallMap<{+ callFrom: string;+ expiresAt: number;+ from: string;+ isVideo: boolean;+ isGroup: boolean;+ }>();/** How many ids the record of deletes for everyone keeps before it forgets the oldest. */static readonly DELETED_FOR_EVERYONE_LIMIT = 5_000;@@ -1057,7 +1071,7 @@ export class BaileysEvents {* Reject a currently-ringing call. The entry is evicted before the attempt, so an outcome that* arrives meanwhile cannot publish a second one, and a rejected call does not become rejectable* again. A failed attempt leaves the call ringing, so its entry is put back for a retry unless the- * id rang again meanwhile. An unknown id or an expired entry maps to CallNotFoundError (HTTP 404).+ * id rang again or the connection was torn down meanwhile. An unknown id or an expired entry maps to CallNotFoundError (HTTP 404).* A failure of the library's rejectCall() itself propagates as-is.*/async rejectCall(callId: string): Promise<void> {@@ -1071,6 +1085,7 @@ export class BaileysEvents {throw new EngineNotReadyError('Cannot reject a call before the engine is initialized.');}this.liveCalls.delete(callId);+ const clears = this.liveCalls.clears;try {await withQueryDeadline(sock.rejectCall(callId, entry.callFrom),@@ -1078,9 +1093,12 @@ export class BaileysEvents {'WhatsApp did not confirm the call rejection in time',);} catch (err) {+ // A teardown meanwhile ended the connection and its call handles, so the handle stays gone.// Known limit: an outcome that ended the call during the attempt found no entry and left no// trace, so the handle comes back even then, until its TTL runs out.- if (!this.liveCalls.has(callId) && entry.expiresAt > Date.now()) this.liveCalls.set(callId, entry);+ if (this.liveCalls.clears === clears && !this.liveCalls.has(callId) && entry.expiresAt > Date.now()) {+ this.liveCalls.set(callId, entry);+ }throw err;}// A rejection made HERE produces no inbound `reject` signal to observe, so without this thediff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.tsindex bce1abeb1..ebeb44d44 100644--- a/src/engine/adapters/baileys.adapter.spec.ts+++ b/src/engine/adapters/baileys.adapter.spec.ts@@ -7312,6 +7312,32 @@ describe('BaileysAdapter call outcomes', () => {expect(fakeSock.rejectCall).toHaveBeenLastCalledWith(CALL_ID, '[email protected]');});+ // A teardown ends the socket, which fails the pending attempt; the handle died with the connection+ // and must not come back, or a retry after a restart would reject a call from the old connection.+ it.each([+ ['a disconnect', (adapter: BaileysAdapter) => adapter.disconnect()],+ [+ 'a terminal close that keeps the socket',+ () =>+ fakeSock.fire('connection.update', {+ connection: 'close',+ lastDisconnect: { error: { output: { statusCode: 440 } } },+ }),+ ],+ ])('does not bring the call back when %s ends the pending rejection', async (_label, teardown) => {+ const adapter = await ringing({ onCall: jest.fn(), onCallOutcome: jest.fn(), onError: jest.fn() });+ let fail!: (err: Error) => void;+ fakeSock.rejectCall.mockReturnValueOnce(new Promise((_, reject) => (fail = reject)));++ const attempt = adapter.rejectCall(CALL_ID);+ await teardown(adapter);+ fail(new Error('Connection Closed'));+ await expect(attempt).rejects.toThrow('Connection Closed');++ await expect(adapter.rejectCall(CALL_ID)).rejects.toBeInstanceOf(CallNotFoundError);+ expect(fakeSock.rejectCall).toHaveBeenCalledTimes(1);+ });+// WhatsApp replays signalling for calls that ended while the session was disconnected. Announcing// those would report last week's declined call as if it had just happened.it('drops an offline-replayed outcome', async () => {