Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): log a failed status write on disconnect and exhaustion
Yudhi Armyndharis160c0d7ac86b
diff --git a/src/modules/session/session-engine-lifecycle-races.spec.ts b/src/modules/session/session-engine-lifecycle-races.spec.tsindex 4cdbb482b..91571a011 100644--- a/src/modules/session/session-engine-lifecycle-races.spec.ts+++ b/src/modules/session/session-engine-lifecycle-races.spec.ts@@ -228,4 +228,41 @@ describe('SessionEngineLifecycle races', () => {expect(failedWrites()).toBe(0);});});++ describe('engine-driven status writes', () => {+ const rejectStatus = (status: SessionStatus): void => {+ repository.update.mockImplementation((_id: unknown, patch: { status?: unknown }) =>+ patch.status === status ? Promise.reject(new Error('SQLITE_BUSY')) : Promise.resolve({ affected: 1 }),+ );+ };++ it('logs a failed DISCONNECTED write instead of leaving it unhandled', async () => {+ const warn = jest.spyOn(internals.logger, 'warn');+ rejectStatus(SessionStatus.DISCONNECTED);+ const engine = makeEngine();+ internals.engines.set(ID, engine as never);++ await lifecycle.handleEngineDisconnected(ID, engine as never, 'NAVIGATION');+ await flush();++ expect(warn).toHaveBeenCalledWith('Failed to persist the disconnected status', {+ sessionId: ID,+ error: 'SQLITE_BUSY',+ });+ });++ it('logs a failed FAILED write when reconnect attempts run out', async () => {+ const warn = jest.spyOn(internals.logger, 'warn');+ rejectStatus(SessionStatus.FAILED);+ internals.reconnectStates.set(ID, { attempts: 5, timer: null, maxAttempts: 5, baseDelay: 5000 });++ internals.scheduleReconnect(ID, session());+ await flush();++ expect(warn).toHaveBeenCalledWith('Failed to persist the reconnect-exhausted FAILED state', {+ sessionId: ID,+ error: 'SQLITE_BUSY',+ });+ });+ });});diff --git a/src/modules/session/session-engine-lifecycle.service.ts b/src/modules/session/session-engine-lifecycle.service.tsindex d365956fc..497484e14 100644--- a/src/modules/session/session-engine-lifecycle.service.ts+++ b/src/modules/session/session-engine-lifecycle.service.ts@@ -1065,7 +1065,12 @@ export class SessionEngineLifecycle {},);- void this.updateStatus(id, SessionStatus.DISCONNECTED);+ void this.updateStatus(id, SessionStatus.DISCONNECTED).catch(err =>+ this.logger.warn('Failed to persist the disconnected status', {+ sessionId: id,+ error: err instanceof Error ? err.message : String(err),+ }),+ );// Pre-schedule fence: scheduleReconnect's timer eventually calls executeReconnect, which does// a fresh engines.get(id) and destroys whatever engine currently owns the id. If this engine@@ -1128,7 +1133,14 @@ export class SessionEngineLifecycle {// Same ownership fence as the engine callbacks: a reconnect chain that exhausts itself after// this node's lease lapsed must not park a peer's session in FAILED, which nothing resets// automatically. The in-memory error above is per-process and harmless either way.- if (this.ownsSession(id)) void this.updateStatus(id, SessionStatus.FAILED);+ if (this.ownsSession(id)) {+ void this.updateStatus(id, SessionStatus.FAILED).catch(err =>+ this.logger.warn('Failed to persist the reconnect-exhausted FAILED state', {+ sessionId: id,+ error: err instanceof Error ? err.message : String(err),+ }),+ );+ }// The hook signal a terminal failure carries: a failed re-init no longer fires session:error per// attempt, so the episode's one terminal end reports it here.void this.hookManager.execute('session:error', { reason }, { sessionId: id, source: 'SessionService' });