Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): release the stored failure reason when a session is deleted (#505)

Yudhi Armyndharis43403a88303b
diff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.ts
index 2718badca..cf423ce19 100644
--- a/src/modules/session/session.service.spec.ts
+++ b/src/modules/session/session.service.spec.ts
@@ -650,6 +650,20 @@ describe('SessionService', () => {
expect(result.lastError).toBe('chromium missing');
});
+ it('clears the stored failure reason when the session is deleted (no in-memory leak)', async () => {
+ const callbacks = await startAndCapture();
+ callbacks.onError?.('chromium missing');
+
+ const sessionErrors = (service as unknown as { sessionErrors: Map<string, string> }).sessionErrors;
+ expect(sessionErrors.has('sess-uuid-1')).toBe(true); // precondition: the FAILED reason is recorded
+
+ (repository.findOne as jest.Mock).mockResolvedValue(createMockSession({ status: SessionStatus.FAILED }));
+ await service.delete('sess-uuid-1');
+
+ // Without cleanup, the entry would linger forever keyed by a deleted UUID (unbounded growth).
+ expect(sessionErrors.has('sess-uuid-1')).toBe(false);
+ });
+
it('does not surface lastError once the session has recovered', async () => {
const callbacks = await startAndCapture();
callbacks.onError?.('transient failure');
diff --git a/src/modules/session/session.service.ts b/src/modules/session/session.service.ts
index cf7453fc5..188a55f37 100644
--- a/src/modules/session/session.service.ts
+++ b/src/modules/session/session.service.ts
@@ -383,6 +383,9 @@ export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicat
// Always clear the teardown mark so a later recreate/start with this id isn't suppressed.
this.stoppingSessions.delete(id);
this.lastDispatchedStatus.delete(id);
+ // Drop the FAILED-reason entry too: it's keyed by a now-deleted UUID that can never be read
+ // again, so leaving it would grow the map without bound across create/fail/delete churn.
+ this.sessionErrors.delete(id);
}
}