Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(wwebjs): close a browser that finishes launching after a teardown

Yudhi Armyndharisc86b39a26780
diff --git a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
index 1d75050a5..a552645a8 100644
--- a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
+++ b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
@@ -383,6 +383,41 @@ describe('WhatsAppWebJsAdapter initialize() retry on a navigation-killed first i
expect(clientInitSpy).toHaveBeenCalledTimes(1);
});
+ // A stop, delete or logout that lands while Chromium is still launching runs Client.destroy()
+ // before whatsapp-web.js has assigned pupBrowser, so it closes nothing. The launch then finishes
+ // with a logged-in browser that nothing owns.
+ it('closes the browser a mid-launch teardown could not reach once the launch finishes', async () => {
+ const adapter = newAdapter();
+ const launch = { finished: false };
+ const destroyedAfterLaunch: boolean[] = [];
+ clientDestroySpy.mockImplementation(() => {
+ destroyedAfterLaunch.push(launch.finished);
+ return Promise.resolve();
+ });
+ clientInitSpy.mockImplementationOnce(async () => {
+ await adapter.disconnect();
+ launch.finished = true;
+ });
+
+ await expect(adapter.initialize({ onError: jest.fn() })).resolves.toBeUndefined();
+
+ // The stop's own destroy ran before the browser existed; only a second one can close it.
+ expect(destroyedAfterLaunch).toEqual([false, true]);
+ expect(adapter.getStatus()).toBe(EngineStatus.DISCONNECTED);
+ });
+
+ it('does not launch when a teardown lands during the pre-launch sweep', async () => {
+ const adapter = newAdapter();
+ clientInitSpy.mockResolvedValue(undefined);
+ rmSpy.mockImplementationOnce(async () => {
+ await adapter.disconnect();
+ });
+
+ await expect(adapter.initialize({ onError: jest.fn() })).resolves.toBeUndefined();
+
+ expect(clientInitSpy).not.toHaveBeenCalled();
+ });
+
it('clears the abandoned reconcile deadline from a first attempt that authenticated before dying', async () => {
jest.useFakeTimers();
clientInitSpy
diff --git a/src/engine/adapters/wwebjs-lifecycle.ts b/src/engine/adapters/wwebjs-lifecycle.ts
index 8f8a72a4e..cfcfc0b31 100644
--- a/src/engine/adapters/wwebjs-lifecycle.ts
+++ b/src/engine/adapters/wwebjs-lifecycle.ts
@@ -447,7 +447,18 @@ export class WwebjsLifecycle {
// removeStaleSingletonFiles. This runs after the orphan kill above and before this attempt's
// browser exists, so it cannot pull the files out from under a running Chromium.
await removeStaleSingletonFiles(this.host.config.sessionId, this.host.config.sessionDataPath, this.host.logger);
- await client.initialize();
+ if (this.tearingDown) return;
+ try {
+ await client.initialize();
+ } finally {
+ // A stop, delete or logout that landed mid-launch ran Client.destroy() before whatsapp-web.js
+ // assigned pupBrowser, so it closed nothing. The handle exists now: close the browser here, or
+ // it stays logged in with no owner until the process exits.
+ if (this.tearingDown || this.client !== client) {
+ await client.destroy().catch(() => undefined);
+ }
+ }
+ if (this.tearingDown || this.client !== client) return;
// whatsapp-web.js 1.34.x never observes the Chromium process/page it drives, so a crashed
// browser leaves the client looking READY forever ("silent death"). Attach death listeners
// to the puppeteer handles so a dead browser surfaces as a normal disconnect → reconnect.