Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): reap only the reconnect's own engine when its init fails

Yudhi Armyndharisc9d66ae210d3
diff --git a/src/modules/session/session-engine-lifecycle.service.ts b/src/modules/session/session-engine-lifecycle.service.ts
index b4d1f9046..f44aa7dea 100644
--- a/src/modules/session/session-engine-lifecycle.service.ts
+++ b/src/modules/session/session-engine-lifecycle.service.ts
@@ -1225,6 +1225,9 @@ export class SessionEngineLifecycle {
if (this.reconnectStates.get(id) === state) this.cancelReconnect(id);
return;
}
+ // The engine this attempt registers, captured like start() does: the catch must reap this one, not
+ // whatever a later attempt has registered by the time the init rejects.
+ let mine: IWhatsAppEngine | undefined;
try {
// Clean up old engine. Time-bound the teardown: a wedged Chromium (the common reconnect
// trigger) makes destroy() hang, and a raw await here would stall the reconnect forever —
@@ -1261,7 +1264,11 @@ export class SessionEngineLifecycle {
// parkReconnectInitFailure): a failed launch must retry, not land FAILED and strand the session.
state.initInFlight = true;
try {
- await this.initializeEngine(id, session);
+ const before = this.engines.get(id);
+ const init = this.initializeEngine(id, session);
+ const registered = this.engines.get(id);
+ mine = registered !== before ? registered : undefined;
+ await init;
} finally {
state.initInFlight = false;
}
@@ -1315,11 +1322,13 @@ export class SessionEngineLifecycle {
// initializeEngine registers the engine in the map BEFORE engine.initialize() runs, so a rejected
// re-init leaves a half-built engine behind. Evict + reap it: otherwise a reconnect that later
// exhausts its attempts strands an orphaned Chromium holding a concurrency slot, and the next
- // start() sees the session as "already started".
- const halfBuilt = this.engines.get(id);
- if (halfBuilt) {
- this.evictAndForceDestroy(id, halfBuilt);
- }
+ // start() sees the session as "already started". Only while it is still registered: otherwise the
+ // init deadline has reaped it already, or a later attempt destroyed it as its old engine.
+ if (mine && this.engines.isLive(id, mine)) this.evictAndForceDestroy(id, mine);
+ // A disconnect of this attempt's engine mid-init re-armed the same state, and a later attempt
+ // registered a replacement before this init rejected. That attempt owns the episode: arming
+ // another would destroy its engine as the old one, or kill it on the exhausted branch.
+ if (this.engines.has(id)) return;
if (this.stoppingSessions.has(id)) {
this.cancelReconnect(id);
return;
diff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.ts
index 433dbb6ca..e32fbeee3 100644
--- a/src/modules/session/session.service.spec.ts
+++ b/src/modules/session/session.service.spec.ts
@@ -2957,6 +2957,26 @@ describe('SessionService', () => {
for (const state of internals().reconnectStates.values()) if (state.timer) clearTimeout(state.timer);
});
+ it('does not reap a replacement registered while its init ran, nor arm an attempt over it', async () => {
+ // A disconnect of this attempt's engine mid-init re-arms the same state, and the next attempt
+ // registers a replacement before this init rejects. That replacement owns the episode now.
+ const i = internals();
+ const init = deferredInit();
+ const state = armState();
+ const replacement = { ...mockEngine, forceDestroy: jest.fn().mockResolvedValue(undefined) };
+
+ const run = i.executeReconnect(ID, createMockSession(), state);
+ await flush();
+ i.engines.set(ID, replacement as never);
+ init.fail('net::ERR_CONNECTION_RESET');
+ await run;
+ await flush();
+
+ expect(i.engines.get(ID)).toBe(replacement);
+ expect(replacement.forceDestroy).not.toHaveBeenCalled();
+ expect(state.timer).toBeNull();
+ });
+
it('retries a network rejection: same state re-armed, no FAILED, no session:error, engine evicted once', async () => {
const i = internals();
mockEngine.initialize.mockImplementationOnce(failingInit('net::ERR_NAME_NOT_RESOLVED'));