Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(engine): fence the wwjs stuck-auth profile removal against restarts
Yudhi Armyndharisd566677990fc
diff --git a/src/engine/adapters/wwebjs-stuck-auth.spec.ts b/src/engine/adapters/wwebjs-stuck-auth.spec.tsnew file mode 100644index 000000000..b14b249e4--- /dev/null+++ b/src/engine/adapters/wwebjs-stuck-auth.spec.ts@@ -0,0 +1,46 @@+import * as fs from 'fs';+import { WwebjsStuckAuth, type WwebjsStuckAuthHost } from './wwebjs-stuck-auth';+import { EngineStatus } from '../interfaces/whatsapp-engine.interface';+import { createLogger } from '../../common/services/logger.service';++/**+ * The stuck-auth recovery removes the session's LocalAuth profile. A stop and start arriving while+ * that rm is still walking the tree would launch a new Chromium into the directory being deleted,+ * so the rm must be registered with the credential-teardown fence before it is awaited, exactly as+ * the logout path registers its own removal.+ */+describe('WwebjsStuckAuth.recoverFromStuckAuth', () => {+ afterEach(() => jest.restoreAllMocks());++ it('registers the profile removal with the teardown fence while it is still running', async () => {+ let finishRm!: () => void;+ jest.spyOn(fs.promises, 'rm').mockReturnValue(new Promise<void>(resolve => (finishRm = resolve)));+ const registered: Promise<void>[] = [];+ const setStatus = jest.fn();+ const host: WwebjsStuckAuthHost = {+ logger: createLogger('wwebjs-stuck-auth.spec'),+ config: { sessionId: 's1', sessionDataPath: './data/sessions', puppeteer: {} },+ getClient: () => null,+ setClient: jest.fn(),+ setStatus,+ getCallbacks: () => ({+ claimStuckAuthRecovery: () => true,+ onCredentialTeardownStarted: (operation: Promise<void>) => registered.push(operation),+ }),+ };++ const recovery = new WwebjsStuckAuth(host).recoverFromStuckAuth();+ await Promise.resolve();++ expect(registered).toHaveLength(1);+ let settled = false;+ void registered[0].then(() => (settled = true));+ await Promise.resolve();+ expect(settled).toBe(false);++ finishRm();+ await recovery;+ expect(settled).toBe(true);+ expect(setStatus).toHaveBeenCalledWith(EngineStatus.DISCONNECTED);+ });+});diff --git a/src/engine/adapters/wwebjs-stuck-auth.ts b/src/engine/adapters/wwebjs-stuck-auth.tsindex e488c6798..2020c5705 100644--- a/src/engine/adapters/wwebjs-stuck-auth.ts+++ b/src/engine/adapters/wwebjs-stuck-auth.ts@@ -84,8 +84,12 @@ export class WwebjsStuckAuth {});}// Clear auth + disconnect (the recovery path), then finish the client teardown in the background- // so a hung destroy can't block (or skip) the recovery.- await this.clearLocalAuth();+ // so a hung destroy can't block (or skip) the recovery. The removal is registered with the+ // credential-teardown fence before it is awaited, as the logout path does: a stop and start+ // arriving meanwhile would otherwise launch a new Chromium into the directory being deleted.+ const removal = this.clearLocalAuth();+ this.host.getCallbacks().onCredentialTeardownStarted?.(removal);+ await removal;this.host.setStatus(EngineStatus.DISCONNECTED);// onDisconnected drives the lifecycle's reconnect, which re-creates the engine with no saved auth// → a fresh QR. (A no-op once the engine is superseded/torn down.)