Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(wwebjs): treat a launch a stop kills as a teardown, not a failure

Yudhi Armyndharisa57322cdf658
diff --git a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
index d674226ab..b5c5dee55 100644
--- a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
+++ b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
@@ -381,9 +381,28 @@ describe('WhatsAppWebJsAdapter initialize() retry on a navigation-killed first i
});
const onError = jest.fn();
- await expect(adapter.initialize({ onError })).rejects.toThrow(EXEC_CTX);
+ await expect(adapter.initialize({ onError })).resolves.toBeUndefined();
expect(clientInitSpy).toHaveBeenCalledTimes(1);
+ expect(onError).not.toHaveBeenCalled();
+ });
+
+ // The stop's destroy closes the browser, which rejects the pending launch. That rejection is the
+ // stop landing, not a start failure: no FAILED status, no onError, like the other teardown exits.
+ it('settles DISCONNECTED without onError when a stop closes the launching browser', async () => {
+ const adapter = newAdapter();
+ const states: EngineStatus[] = [];
+ clientInitSpy.mockImplementationOnce(async () => {
+ await adapter.disconnect();
+ throw new Error('Protocol error (Runtime.callFunctionOn): Target closed');
+ });
+ const onError = jest.fn();
+
+ await expect(adapter.initialize({ onError, onStateChanged: state => states.push(state) })).resolves.toBeUndefined();
+
+ expect(onError).not.toHaveBeenCalled();
+ expect(states).not.toContain(EngineStatus.FAILED);
+ expect(adapter.getStatus()).toBe(EngineStatus.DISCONNECTED);
});
// A stop, delete or logout that lands while Chromium is still launching runs Client.destroy()
diff --git a/src/engine/adapters/wwebjs-lifecycle.ts b/src/engine/adapters/wwebjs-lifecycle.ts
index dbb490ad0..c1a1dbcfc 100644
--- a/src/engine/adapters/wwebjs-lifecycle.ts
+++ b/src/engine/adapters/wwebjs-lifecycle.ts
@@ -372,6 +372,12 @@ export class WwebjsLifecycle {
await this.runInitAttempt(puppeteerArgs, authTimeoutMs, proxyAuthentication, versionPin);
}
} catch (error) {
+ // A stop, delete or force-kill closed the browser under the launch, which is what rejected it:
+ // settle like the other teardown exits instead of reporting the stop as a failed start.
+ if (this.tearingDown) {
+ if (this.status !== EngineStatus.DISCONNECTED) this.setStatus(EngineStatus.DISCONNECTED);
+ return;
+ }
this.setStatus(EngineStatus.FAILED);
const reason = error instanceof Error ? error.message : String(error);
// What the dashboard renders as `lastError` is exactly this string and nothing else — the log