Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(engine): leave the auth dir alone after a stop during connect

Yudhi Armyndharis7c84eea39385
diff --git a/src/engine/adapters/baileys-lifecycle.spec.ts b/src/engine/adapters/baileys-lifecycle.spec.ts
index 012170ba3..f90a8e21a 100644
--- a/src/engine/adapters/baileys-lifecycle.spec.ts
+++ b/src/engine/adapters/baileys-lifecycle.spec.ts
@@ -263,6 +263,26 @@ describe('BaileysLifecycle proxy fetch dispatcher', () => {
return { engine, resolve, authRequested, loadAuth };
}
+ // Opening the auth state creates the session's directory, which a deleted session no longer has.
+ it('does not open the auth state when the session stops while the library loads', async () => {
+ const engine = lifecycle();
+ engine.sock = null;
+ let finishLibrary: (lib: typeof BaileysLib) => void = () => undefined;
+ jest.spyOn(engine, 'loadLib').mockReturnValueOnce(
+ new Promise<typeof BaileysLib>(resolve => {
+ finishLibrary = resolve;
+ }),
+ );
+ jest.mocked(useAtomicMultiFileAuthState).mockClear();
+ const init = engine.initialize();
+ await engine.destroy();
+ finishLibrary(BaileysLib);
+ await init;
+
+ expect(useAtomicMultiFileAuthState).not.toHaveBeenCalled();
+ expect(engine.sock).toBeNull();
+ });
+
// The teardown closed the only dispatcher there was; a lookup after it would build one nothing
// closes and send a stopped session's requests through its proxy.
it('skips the version lookup when the session stops while its auth state loads', async () => {
diff --git a/src/engine/adapters/baileys-lifecycle.ts b/src/engine/adapters/baileys-lifecycle.ts
index efcbff818..30c43790a 100644
--- a/src/engine/adapters/baileys-lifecycle.ts
+++ b/src/engine/adapters/baileys-lifecycle.ts
@@ -328,6 +328,10 @@ export class BaileysLifecycle {
}
}
const b = await this.loadLib();
+ // The first load of the library takes a while; a stop meanwhile must not have the auth dir created for it.
+ if (this.intentionalClose) {
+ return;
+ }
const { state, saveCreds } = await useAtomicMultiFileAuthState(this.host.authPath, b, this.host.logger);
// A teardown during those loads already closed the dispatcher: the lookup would build a new one
// that nothing closes and send the stopped session's requests through its proxy. One during the