Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): end a read retry when its engine failed to initialize

Yudhi Armyndharisd54aff41674a
diff --git a/src/modules/session/session-engine-lifecycle-races.spec.ts b/src/modules/session/session-engine-lifecycle-races.spec.ts
index f6c5c1105..70551c75d 100644
--- a/src/modules/session/session-engine-lifecycle-races.spec.ts
+++ b/src/modules/session/session-engine-lifecycle-races.spec.ts
@@ -538,6 +538,60 @@ describe('SessionEngineLifecycle races', () => {
expect(engineFactory.create).toHaveBeenCalledTimes(2);
});
+ // The browser of an attempt dies during its own init: it reports the disconnect while the session
+ // read fails once, which arms a read retry on the episode's timer, and the same crash rejects init.
+ describe('when the disconnect of its own engine armed a read retry', () => {
+ afterEach(() => jest.useRealTimers());
+
+ const crashDuringInit = async (readFails: boolean): Promise<ReconnectState> => {
+ jest.useFakeTimers({ doNotFake: ['setImmediate', 'nextTick', 'queueMicrotask'] });
+ const state: ReconnectState = {
+ attempts: 1,
+ timer: null,
+ maxAttempts: Number.POSITIVE_INFINITY,
+ baseDelay: 5000,
+ };
+ internals.reconnectStates.set(ID, state);
+ const init = deferred();
+ const engine = { ...makeEngine(), initialize: jest.fn().mockReturnValue(init.promise) };
+ engineFactory.create.mockReturnValueOnce(engine);
+ const attempt = internals.executeReconnect(ID, session(), state);
+ await flush();
+ const callbacks = (engine.initialize.mock.calls as [EngineEventCallbacks][])[0][0];
+
+ if (readFails) repository.findOne.mockRejectedValueOnce(new Error('SQLITE_BUSY: database is locked'));
+ callbacks.onDisconnected?.('NAVIGATION');
+ await flush();
+ init.reject(new Error('Protocol error: Target closed'));
+ await attempt;
+ await flush();
+ return state;
+ };
+
+ it('arms the next attempt instead of leaving the session with no engine and no timer', async () => {
+ const state = await crashDuringInit(true);
+
+ for (let i = 0; i < 4; i++) {
+ jest.advanceTimersByTime(300_000);
+ await flush();
+ }
+
+ expect(engineFactory.create.mock.calls.length).toBeGreaterThan(1);
+ expect(state.readRetry).toBeUndefined();
+ });
+
+ it('relaunches the same way when the read answered', async () => {
+ await crashDuringInit(false);
+
+ for (let i = 0; i < 4; i++) {
+ jest.advanceTimersByTime(300_000);
+ await flush();
+ }
+
+ expect(engineFactory.create.mock.calls.length).toBeGreaterThan(1);
+ });
+ });
+
it('re-arms when the attempt armed after it was cancelled by a READY before it fired', async () => {
const state: ReconnectState = { attempts: 1, timer: null, maxAttempts: 5, baseDelay: 5000 };
internals.reconnectStates.set(ID, state);
diff --git a/src/modules/session/session-engine-lifecycle.service.ts b/src/modules/session/session-engine-lifecycle.service.ts
index fba6b0798..0997e6423 100644
--- a/src/modules/session/session-engine-lifecycle.service.ts
+++ b/src/modules/session/session-engine-lifecycle.service.ts
@@ -1442,6 +1442,10 @@ export class SessionEngineLifecycle {
this.cancelReconnect(id);
return;
}
+ // A read retry the disconnect of this attempt's own engine armed waits on an engine that is gone
+ // now: it would hold the timer the next attempt needs, then fire against the evicted engine and
+ // end at its fence, leaving the session with no engine and nothing armed.
+ this.endReadRetry(id);
// Schedule another attempt. The row stays INITIALIZING through the backoff (an active status, with
// lastError still showing the reason), so a retryable failure writes nothing per attempt.
this.scheduleReconnect(id, session);