Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): count reconnect downtime from the last READY

Yudhi Armyndharis1c6bec883425
diff --git a/src/modules/session/session-engine-event-wiring.ts b/src/modules/session/session-engine-event-wiring.ts
index fba34f34d..bd6544eb9 100644
--- a/src/modules/session/session-engine-event-wiring.ts
+++ b/src/modules/session/session-engine-event-wiring.ts
@@ -138,10 +138,12 @@ export class SessionEngineEventWiring {
// presence is the relink signal used below; its value gates onReady's account-binding check.
const previouslyLinked = Boolean(previousPhone);
let relinkWarned = false;
- // Start of the current engine-internal reconnect episode (epoch ms), stamped on attempt 1.
- // Closure state rather than a Map: it is scoped to this engine instance, which is exactly the
- // lifetime of an episode. A service-level reconnect builds a new engine and a new callback table,
- // so nothing has to be cleaned up and a previous episode's clock can never be inherited.
+ // When the session was last seen up before the current engine-internal reconnect (epoch ms),
+ // stamped on attempt 1 or on the first attempt after a READY, and cleared by onReady. An engine
+ // can carry its attempt counter across a short-lived READY (Baileys does, so a link that drops
+ // right after opening keeps backing off), and time spent READY is not downtime.
+ // Closure state rather than a Map: it is scoped to this engine instance. A service-level
+ // reconnect builds a new engine and a new callback table, so nothing has to be cleaned up.
let reconnectingSince = 0;
/**
* Persist an engine-driven status, but only while this node still owns the session.
@@ -212,6 +214,7 @@ export class SessionEngineEventWiring {
persistStatus(SessionStatus.QR_READY);
},
onReady: (phone, pushName): void => {
+ reconnectingSince = 0;
// Account-binding guard: a ready link whose number differs from the one this session is
// already bound to is a different account scanning its QR (a takeover), not a re-link. Refuse
// it rather than silently overwrite the binding. An empty incoming phone (wwjs can report one)
@@ -298,7 +301,7 @@ export class SessionEngineEventWiring {
// attempts scheduled across all sessions", and an engine that retries internally was simply
// never counted, so the series read 0 on Baileys however long a session looped.
incrementSessionReconnectAttempts();
- if (attempt === 1) reconnectingSince = Date.now();
+ if (attempt === 1 || reconnectingSince === 0) reconnectingSince = Date.now();
// Below the loop threshold this is a blip, not an episode: the 515 restart WhatsApp asks for
// right after a successful pairing is one attempt, and so is any drop that comes straight
diff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.ts
index dd3ddb48d..89691706d 100644
--- a/src/modules/session/session.service.spec.ts
+++ b/src/modules/session/session.service.spec.ts
@@ -3703,6 +3703,33 @@ describe('SessionService', () => {
'Reconnecting after a dropped connection (attempt 5, down for 80s).',
);
});
+
+ // Baileys keeps its attempt counter across a READY that lasted under five minutes, so the next
+ // drop can arrive as attempt 5. The time the session spent READY is not downtime.
+ it('measures the downtime from the last READY when the attempt count carries across it', async () => {
+ const callbacks = await startAndCapture();
+ const sessionErrors = (service as unknown as { sessionErrors: SessionErrorStore }).sessionErrors;
+ const start = Date.now();
+ const now = jest.spyOn(Date, 'now');
+ try {
+ for (let attempt = 1; attempt <= 4; attempt++) {
+ now.mockReturnValue(start + (attempt - 1) * 5_000);
+ callbacks.onReconnecting?.(attempt, 8_000);
+ }
+ now.mockReturnValue(start + 20_000);
+ callbacks.onReady?.('628123', 'Tester');
+ now.mockReturnValue(start + 4 * 60_000);
+ callbacks.onReconnecting?.(5, 16_000);
+ now.mockReturnValue(start + 4 * 60_000 + 16_000);
+ callbacks.onReconnecting?.(6, 32_000);
+ } finally {
+ now.mockRestore();
+ }
+
+ expect(sessionErrors.get('sess-uuid-1')).toBe(
+ 'Reconnecting after a dropped connection (attempt 6, down for 16s).',
+ );
+ });
});
// A restriction is WhatsApp judging the account, not a fault on our side, so it has its own