Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): clear the phone of an unlinked session first

Yudhi Armyndharisc924edd72731
diff --git a/src/modules/session/session-engine-controls.ts b/src/modules/session/session-engine-controls.ts
index 216d69ad3..4ff733f42 100644
--- a/src/modules/session/session-engine-controls.ts
+++ b/src/modules/session/session-engine-controls.ts
@@ -571,6 +571,13 @@ export class SessionEngineControls {
// the row is later deleted/recreated.
unlinked = await this.fences.teardownEngineSafely(id, engine, e => e.logout(), 'logout', session.name);
this.engines.deleteIfLive(id, engine);
+ // Clear `phone` AFTER the attempt, whatever its outcome, to take the session out of the boot
+ // auto-start query (phone IS NOT NULL). A completed engine-backed unlink wipes the stored
+ // credentials, so the session can never reach READY again without a fresh QR/pairing; an
+ // incomplete one tore the local credentials down, so re-entering auto-start would only wedge it.
+ // Before the status write, so a failed status write cannot leave the flag behind and resurrect
+ // the session into a QR it can never pass on every restart. onReady rewrites it on the next link.
+ await this.sessionRepository.update(id, { phone: null });
await this.host.updateStatus(id, SessionStatus.DISCONNECTED);
} finally {
if (this.operatorTeardowns.get(id) === engine) {
@@ -583,13 +590,10 @@ export class SessionEngineControls {
sessionId: id,
action: 'logout_incomplete',
});
- // The local teardown already ran (map reconciled, status DISCONNECTED), but the unlink operation
- // did not complete. Clear `phone` AFTER the attempt and BEFORE throwing so the boot auto-start
- // does not resurrect the session into a credential state that can no longer reach READY — the
- // local credentials were torn down, so re-entering auto-start would only wedge it. The retryable
- // 502 carries a stable machine code so the dashboard can branch on origin without guessing from
- // the status/message; no success audit is written on this path.
- await this.sessionRepository.update(id, { phone: null });
+ // The local teardown already ran (map reconciled, `phone` cleared, status DISCONNECTED), but the
+ // unlink operation did not complete. The retryable 502 carries a stable machine code so the
+ // dashboard can branch on origin without guessing from the status/message; no success audit is
+ // written on this path.
throw new BadGatewayException({
statusCode: HttpStatus.BAD_GATEWAY,
message:
@@ -600,12 +604,6 @@ export class SessionEngineControls {
});
}
- // A completed engine-backed unlink wipes the stored credentials, so this session can never
- // reach READY again without a fresh QR/pairing. Clear `phone` to take it out of the boot
- // auto-start query (phone IS NOT NULL) instead of resurrecting it into a QR it can never pass
- // on every restart. onReady rewrites it on the next successful link.
- await this.sessionRepository.update(id, { phone: null });
-
this.logger.log(`Session logged out: ${session.name}`, {
sessionId: id,
action: 'logout',
diff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.ts
index 16bc7fe47..ebe139e0e 100644
--- a/src/modules/session/session.service.spec.ts
+++ b/src/modules/session/session.service.spec.ts
@@ -516,6 +516,21 @@ describe('SessionService', () => {
expect(result.phone).toBeNull();
});
+ // `phone` is the flag that keeps an unlinked session out of the boot auto-start query. It must not
+ // depend on the status write that follows the unlink: a failure there reports the logout as failed
+ // (the unlink is done either way) but must not leave the session to be resurrected into a QR.
+ it('logout() clears phone before the DISCONNECTED write, so a failed status write cannot leave the flag behind', async () => {
+ (repository.findOne as jest.Mock).mockResolvedValue(createMockSession());
+ (repository.update as jest.Mock).mockImplementation((_id: string, patch: Record<string, unknown>) =>
+ 'status' in patch ? Promise.reject(new Error('db down')) : Promise.resolve({ affected: 1 }),
+ );
+ enginesOf().set('sess-uuid-1', { logout: jest.fn().mockResolvedValue(undefined) });
+
+ await expect(service.logout('sess-uuid-1')).rejects.toThrow('db down');
+
+ expect(repository.update).toHaveBeenCalledWith('sess-uuid-1', { phone: null });
+ });
+
it('logout() rejects with 502 + stable code SESSION_LOGOUT_INCOMPLETE when the unlink is incomplete — but still tears down locally AND clears phone', async () => {
(repository.findOne as jest.Mock).mockResolvedValue(createMockSession());
(repository.update as jest.Mock).mockResolvedValue({ affected: 1 });