Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): reap the engine of a start whose row is gone
Yudhi Armyndharisdeb6c2bd25aa
diff --git a/src/modules/session/session-engine-controls.ts b/src/modules/session/session-engine-controls.tsindex 555d1f037..18eab502f 100644--- a/src/modules/session/session-engine-controls.ts+++ b/src/modules/session/session-engine-controls.ts@@ -435,14 +435,7 @@ export class SessionEngineControls {retired = false;}if (retired) {- const resurrected = this.engines.get(id);- if (resurrected) {- await this.fences.destroyWithEscalation(id, resurrected);- this.engines.deleteIfLive(id, resurrected);- }- // A delete() that raced this start purged the on-disk auth dirs BEFORE this init re-created- // them — purge again so the window leaves no credential residue behind (no-op for a stop()).- await this.host.purgeAuthDirsIfDeleted(id);+ await this.reapRetiredEngine(id);} else if (this.host.isClosed()) {// Shutdown destroys every engine it finds, this one included, and skips initialize() for an// engine it caught mid-start. Either way the session is not kept, so this is no success. A@@ -455,6 +448,9 @@ export class SessionEngineControls {// 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.+ // A row found gone only now is the retirement the failed read above could not report: the engine+ // goes with it.+ if (!retired && error instanceof NotFoundException) await this.reapRetiredEngine(id);if (retired || error instanceof NotFoundException) throw error;return session;}@@ -463,6 +459,18 @@ export class SessionEngineControls {}}+ /** Tears down the engine a start registered for a session that was stopped or deleted meanwhile. */+ private async reapRetiredEngine(id: string): Promise<void> {+ const resurrected = this.engines.get(id);+ if (resurrected) {+ await this.fences.destroyWithEscalation(id, resurrected);+ this.engines.deleteIfLive(id, resurrected);+ }+ // A delete() that raced this start purged the on-disk auth dirs BEFORE this init re-created them, so+ // purge again: the window then leaves no credential residue behind (no-op for a stop()).+ await this.host.purgeAuthDirsIfDeleted(id);+ }+async stop(id: string, hooks: StopHooks = {}): Promise<Session> {const session = await this.requireSessionOrDropStopMark(id, hooks.onReadFailed);// Recorded only once the row is known to exist, and before anything is torn down.diff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.tsindex ebe139e0e..d682c6e74 100644--- a/src/modules/session/session.service.spec.ts+++ b/src/modules/session/session.service.spec.ts@@ -8044,6 +8044,25 @@ describe('SessionService', () => {expect(mockEngine.forceDestroy).not.toHaveBeenCalled();});+ // A failed retirement read assumes the session is still there, so the read that follows has the last word:+ // a row found gone then leaves the engine this start registered with no session to serve.+ it('tears the engine down when the retirement read failed and the row turns out to be gone', async () => {+ (repository.findOne as jest.Mock).mockResolvedValue(createMockSession());+ (repository.update as jest.Mock).mockResolvedValue({ affected: 1 });+ mockEngine.initialize.mockImplementationOnce(() => {+ (repository.findOne as jest.Mock)+ .mockRejectedValueOnce(new Error('SQLITE_BUSY: database is locked'))+ .mockResolvedValue(null);+ return Promise.resolve();+ });++ await expect(service.start('sess-uuid-1')).rejects.toThrow(NotFoundException);++ expect(mockEngine.destroy).toHaveBeenCalled();+ expect(service.getEngine('sess-uuid-1')).toBeUndefined();+ expect(engineFactory.purgeSessionData).toHaveBeenCalledWith('sess-uuid-1');+ });+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 });