Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): keep a start that worked when a later read fails
Yudhi Armyndharisa78c79bc1ec8
diff --git a/src/modules/session/session-engine-controls.ts b/src/modules/session/session-engine-controls.tsindex 0be9892c3..216d69ad3 100644--- a/src/modules/session/session-engine-controls.ts+++ b/src/modules/session/session-engine-controls.ts@@ -407,7 +407,15 @@ export class SessionEngineControls {// guard in executeReconnect; initialize()'s callbacks can also fire async after this returns).// delete() clears its teardown mark before this slow init resolves, so re-check the session row// exists, not just the mark; the requireSession below then surfaces a deleted session as NotFound.- if (await this.host.isSessionRetired(id)) {+ // Guarded like the same read in executeReconnect: a failed read assumes not-retired, so a+ // transient database error cannot fail a start whose engine is already registered and running.+ let retired: boolean;+ try {+ retired = await this.host.isSessionRetired(id);+ } catch {+ retired = false;+ }+ if (retired) {const resurrected = this.engines.get(id);if (resurrected) {await this.fences.destroyWithEscalation(id, resurrected);@@ -422,7 +430,15 @@ export class SessionEngineControls {// start retired above answers as the stop or delete that retired it instead.throw new ServerShuttingDownException();}- return this.requireSession(id);+ try {+ return await this.requireSession(id);+ } catch (error) {+ // The engine is registered and running. A failed read (a missing row stays NotFound) must not+ // report a start that worked as failed: the retry a transient database error buys would answer+ // "already started". The row read on entry stands in for the one that failed.+ if (retired || error instanceof NotFoundException) throw error;+ return session;+ }} finally {this.initializingSessions.delete(id);}diff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.tsindex 12636f8af..a1fce79ac 100644--- a/src/modules/session/session.service.spec.ts+++ b/src/modules/session/session.service.spec.ts@@ -7999,6 +7999,24 @@ describe('SessionService', () => {expect(engineFactory.purgeSessionData).not.toHaveBeenCalled();});+ // The engine is registered and running by then. Reporting the start as failed would buy a retry that+ // answers "already started", with no SESSION_STARTED audit row for a start that worked.+ it('keeps a start that worked when the database fails the reads that follow its init', async () => {+ (repository.findOne as jest.Mock).mockResolvedValue(createMockSession());+ (repository.update as jest.Mock).mockResolvedValue({ affected: 1 });+ mockEngine.initialize.mockImplementationOnce(() => {+ (repository.findOne as jest.Mock).mockRejectedValue(new Error('SQLITE_BUSY: database is locked'));+ return Promise.resolve();+ });++ const started = await service.start('sess-uuid-1');++ expect(started.id).toBe('sess-uuid-1');+ expect(service.getEngine('sess-uuid-1')).toBeDefined();+ expect(mockEngine.destroy).not.toHaveBeenCalled();+ expect(mockEngine.forceDestroy).not.toHaveBeenCalled();+ });+it('escalates to forceDestroy() when the retired engine fails its graceful destroy()', async () => {(repository.findOne as jest.Mock).mockResolvedValue(createMockSession());(repository.update as jest.Mock).mockResolvedValue({ affected: 1 });