Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): purge auth dirs a cancelled reconnect re-created

Yudhi Armyndharisfda16361ae8b
diff --git a/src/modules/session/session-engine-lifecycle.service.ts b/src/modules/session/session-engine-lifecycle.service.ts
index 94b06b66f..14d4403f6 100644
--- a/src/modules/session/session-engine-lifecycle.service.ts
+++ b/src/modules/session/session-engine-lifecycle.service.ts
@@ -1422,7 +1422,10 @@ export class SessionEngineLifecycle {
} finally {
if (state.started === attempt) state.initInFlight = false;
}
- if (this.reconnectStates.get(id) !== state || state.started !== attempt) return;
+ if (this.reconnectStates.get(id) !== state || state.started !== attempt) {
+ await this.purgeAuthDirsIfDeleted(id);
+ return;
+ }
// Init resolved, so a failure the engine reported meanwhile (a stuck-auth or readiness timer, an
// auth failure) is real: apply it, unless a stop/delete/start already replaced this state.
const parked = state.parkedFailure;
@@ -1461,7 +1464,10 @@ export class SessionEngineLifecycle {
});
// A delete (or delete + start) replaced this state while init ran: nothing here is ours any more,
// and a later attempt in the same episode owns its initialization marker and parked failure too.
- if (this.reconnectStates.get(id) !== state || state.started !== attempt) return;
+ if (this.reconnectStates.get(id) !== state || state.started !== attempt) {
+ await this.purgeAuthDirsIfDeleted(id);
+ return;
+ }
const parked = state.parkedFailure;
state.parkedFailure = undefined;
// A failure only the operator can fix (re-scan, stale profile) stays terminal on a reconnect too.
diff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.ts
index 42146d1e8..6a5baf408 100644
--- a/src/modules/session/session.service.spec.ts
+++ b/src/modules/session/session.service.spec.ts
@@ -4653,6 +4653,33 @@ describe('SessionService', () => {
// The success path has its own replacement check: a parked failure of the OLD attempt must not
// run against the state a delete + start put in its place while that init was still running.
+ it.each([
+ [false, false],
+ [false, true],
+ [true, false],
+ [true, true],
+ ])('rechecks cancelled reconnect auth (row survives: %s, init rejects: %s)', async (survives, rejects) => {
+ const i = internals();
+ let finishInit: () => void = () => undefined;
+ mockEngine.initialize.mockImplementationOnce(
+ () =>
+ new Promise<void>((resolve, reject) => {
+ finishInit = () => (rejects ? reject(new Error('late initialization failure')) : resolve());
+ }),
+ );
+ const state = armState();
+ const running = i.executeReconnect(ID, createMockSession(), state);
+ await flush();
+ if (survives) await service.stop(ID);
+ else await service.delete(ID);
+ (repository.findOne as jest.Mock).mockResolvedValue(survives ? createMockSession() : null);
+ (engineFactory.purgeSessionData as jest.Mock).mockClear();
+ finishInit();
+ await running;
+ if (survives) expect(engineFactory.purgeSessionData).not.toHaveBeenCalled();
+ else expect(engineFactory.purgeSessionData).toHaveBeenCalledWith(ID);
+ });
+
it('drops a parked failure when a delete-then-start replaced the state before init resolved', async () => {
const i = internals();
let callbacks: EngineEventCallbacks = {};