Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): drop the reconnect state of a retired start

Yudhi Armyndharis06dd91fd6ede
diff --git a/src/modules/session/session-engine-controls.ts b/src/modules/session/session-engine-controls.ts
index 18eab502f..32c9f955b 100644
--- a/src/modules/session/session-engine-controls.ts
+++ b/src/modules/session/session-engine-controls.ts
@@ -459,13 +459,22 @@ export class SessionEngineControls {
}
}
- /** Tears down the engine a start registered for a session that was stopped or deleted meanwhile. */
+ /**
+ * Tears down the engine a start registered for a session that was stopped or deleted meanwhile, and drops
+ * the reconnect state it armed for it.
+ */
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);
}
+ // The reconnect state this start armed up front serves no engine now, and the stop or delete that retired it
+ // may have run before the state was armed, so nothing else would drop it. A replacement engine that
+ // registered meanwhile keeps its own.
+ if (!this.engines.has(id)) {
+ this.host.cancelReconnect(id);
+ }
// 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);
diff --git a/src/modules/session/session-engine-lifecycle-races.spec.ts b/src/modules/session/session-engine-lifecycle-races.spec.ts
index c2d58387e..3eb6ae972 100644
--- a/src/modules/session/session-engine-lifecycle-races.spec.ts
+++ b/src/modules/session/session-engine-lifecycle-races.spec.ts
@@ -76,6 +76,7 @@ const flush = async (): Promise<void> => {
interface Internals {
engines: EngineRegistry;
+ controls: { reapRetiredEngine(id: string): Promise<void> };
stoppingSessions: Set<string>;
reconnectStates: Map<string, ReconnectState>;
logger: { warn: (...args: unknown[]) => void };
@@ -93,10 +94,12 @@ describe('SessionEngineLifecycle races', () => {
let webhookService: { dispatch: jest.Mock };
let config: Record<string, unknown>;
let dataSource: Record<string, unknown>;
+ let hookManager: { execute: jest.Mock };
beforeEach(async () => {
config = {};
dataSource = {};
+ hookManager = { execute: jest.fn().mockResolvedValue({ continue: true, data: {} }) };
repository = {
findOne: jest.fn().mockResolvedValue(session()),
update: jest.fn().mockResolvedValue({ affected: 1 }),
@@ -131,7 +134,7 @@ describe('SessionEngineLifecycle races', () => {
},
},
{ provide: WebhookService, useValue: { dispatch: jest.fn().mockResolvedValue(undefined) } },
- { provide: HookManager, useValue: { execute: jest.fn().mockResolvedValue({ continue: true, data: {} }) } },
+ { provide: HookManager, useValue: hookManager },
{
provide: ConfigService,
useValue: { get: jest.fn(<T>(key: string, def?: T): T => (key in config ? (config[key] as T) : (def as T))) },
@@ -356,6 +359,81 @@ describe('SessionEngineLifecycle races', () => {
await settled;
expect(internals.engines.has(ID)).toBe(false);
});
+
+ // A start waiting in its session:starting hook has armed no reconnect state yet, so the stop or delete that
+ // retires it has nothing to cancel. The start arms the state afterwards and must drop it when it is retired.
+ describe('a start retired while it waits in its session:starting hook', () => {
+ const parkInHook = async (): Promise<{ release: () => void; outcome: Promise<unknown> }> => {
+ const hook = deferred();
+ hookManager.execute.mockImplementation(async (event: string) => {
+ if (event === 'session:starting') await hook.promise;
+ return { continue: true, data: {} };
+ });
+ const outcome = lifecycle.start(ID, { explicit: true }).then(
+ () => undefined,
+ (e: unknown) => e,
+ );
+ await flush();
+ expect(internals.reconnectStates.has(ID)).toBe(false);
+ return { release: hook.resolve, outcome };
+ };
+
+ it('leaves no reconnect state behind after a stop', async () => {
+ const { release, outcome } = await parkInHook();
+ await lifecycle.stop(ID);
+
+ release();
+ await outcome;
+
+ expect(internals.engines.has(ID)).toBe(false);
+ expect(internals.reconnectStates.has(ID)).toBe(false);
+ });
+
+ it('leaves no reconnect state behind after a delete that is still running', async () => {
+ const { release, outcome } = await parkInHook();
+ const gate = hold();
+ const deleted = settle(lifecycle.delete(ID));
+ await flush();
+
+ release();
+ await outcome;
+
+ expect(internals.engines.has(ID)).toBe(false);
+ expect(internals.reconnectStates.has(ID)).toBe(false);
+ gate.resolve([]);
+ await deleted;
+ });
+
+ it('leaves no reconnect state behind after a delete that has finished', async () => {
+ const { release, outcome } = await parkInHook();
+ await settle(lifecycle.delete(ID));
+ repository.findOne.mockResolvedValue(null);
+
+ release();
+ await outcome;
+
+ expect(internals.engines.has(ID)).toBe(false);
+ expect(internals.reconnectStates.has(ID)).toBe(false);
+ });
+
+ it('keeps the state of an engine that registered while the retired one was torn down', async () => {
+ const destroyed = deferred();
+ const retired = { ...makeEngine(), destroy: jest.fn().mockReturnValue(destroyed.promise) };
+ const replacement = makeEngine();
+ const state: ReconnectState = { attempts: 0, timer: null, maxAttempts: 5, baseDelay: 5000 };
+ internals.engines.set(ID, retired as never);
+
+ const reaping = internals.controls.reapRetiredEngine(ID);
+ await flush();
+ internals.engines.set(ID, replacement as never);
+ internals.reconnectStates.set(ID, state);
+ destroyed.resolve();
+ await reaping;
+
+ expect(internals.engines.get(ID)).toBe(replacement);
+ expect(internals.reconnectStates.get(ID)).toBe(state);
+ });
+ });
});
describe('stop against an explicit start still reading its row', () => {