Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): disarm a pending reconnect when the engine reaches READY
Yudhi Armyndharis9a217b72aca3
diff --git a/src/modules/session/session-engine-lifecycle.service.ts b/src/modules/session/session-engine-lifecycle.service.tsindex 620d3e0ec..596cd9c26 100644--- a/src/modules/session/session-engine-lifecycle.service.ts+++ b/src/modules/session/session-engine-lifecycle.service.ts@@ -815,10 +815,17 @@ export class SessionEngineLifecycle {},);- // Reset reconnect attempts and clear any stale failure reason on success+ // Reset reconnect attempts and clear any stale failure reason on success. READY also ends the+ // episode: a reconnect still pending (the watchdog reported this engine, then it recovered on+ // its own) would tear the recovered engine down. Only the timer goes; the state keeps the+ // session's reconnect settings for its next drop.const reconnectState = this.reconnectStates.get(id);if (reconnectState) {reconnectState.attempts = 0;+ if (reconnectState.timer) {+ clearTimeout(reconnectState.timer);+ reconnectState.timer = null;+ }}// A fresh READY stretch starts the watchdog's failure budget clean too.this.watchdog.clear(id);diff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.tsindex 767105104..599b6eeea 100644--- a/src/modules/session/session.service.spec.ts+++ b/src/modules/session/session.service.spec.ts@@ -2304,6 +2304,33 @@ describe('SessionService', () => {jest.useRealTimers();}});++ // A wedged engine the watchdog reported can recover on its own (Baileys reconnects its socket+ // internally) before the gateway's backoff elapses. The timer armed for it must not then tear+ // down the engine that just came back.+ it('disarms a pending reconnect when the same engine reaches READY, keeping the reconnect state', () => {+ jest.useFakeTimers();+ try {+ const i = internals();+ const state = { attempts: 0, timer: null, maxAttempts: Number.POSITIVE_INFINITY, baseDelay: 150000 };+ i.reconnectStates.set('sess-uuid-1', state);+ (repository.update as jest.Mock).mockResolvedValue({ affected: 1 });+ const engine = { getStatus: jest.fn().mockReturnValue(EngineStatus.READY) };+ i.engines.set('sess-uuid-1', engine);+ const reconnect = jest.spyOn(i, 'executeReconnect').mockResolvedValue(undefined);++ i.scheduleReconnect('sess-uuid-1', createMockSession());+ i.handleEngineReady('sess-uuid-1', engine, '628123', 'Tester');+ jest.advanceTimersByTime(151000);++ expect(reconnect).not.toHaveBeenCalled();+ expect(state.timer).toBeNull();+ expect(i.reconnectStates.get('sess-uuid-1')).toBe(state);+ } finally {+ jest.clearAllTimers();+ jest.useRealTimers();+ }+ });});describe('start() stale reconnect timer', () => {