Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(engine): keep a navigation stamped during a liveness probe

Yudhi Armyndharis8d0603b1ab62
diff --git a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
index 37353222e..84ff4f4f8 100644
--- a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
+++ b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
@@ -6802,6 +6802,53 @@ describe('WhatsAppWebJsAdapter navigation re-inject grace (#1081)', () => {
await expect(adapter.probeLiveness()).resolves.toBe(true);
});
+ it.each([false, true])('keeps a newer navigation after an older probe settles; previous=%s', async previous => {
+ jest.useFakeTimers();
+ const { adapter, client } = wireAdapter();
+ if (previous) client.pupPage.emit('framenavigated', navFrame());
+ let resolve!: (state: WAState) => void;
+ client.getState.mockReturnValueOnce(
+ new Promise<WAState>(done => {
+ resolve = done;
+ }),
+ );
+ const probe = adapter.probeLiveness();
+ client.pupPage.emit('framenavigated', navFrame());
+ resolve(WAState.CONNECTED);
+ await expect(probe).resolves.toBe(true);
+ await expect(adapter.getChats()).rejects.toBeInstanceOf(EngineNotReadyError);
+ });
+
+ it('keeps a newer recovery episode when overlapping probes settle out of order', async () => {
+ jest.useFakeTimers();
+ const { adapter, client } = wireAdapter();
+ let first!: (state: WAState) => void;
+ let second!: (state: WAState) => void;
+ client.getState
+ .mockReturnValueOnce(
+ new Promise<WAState>(done => {
+ first = done;
+ }),
+ )
+ .mockReturnValueOnce(
+ new Promise<WAState>(done => {
+ second = done;
+ }),
+ );
+ const older = adapter.probeLiveness();
+ client.pupPage.emit('framenavigated', navFrame());
+ const current = adapter.probeLiveness();
+ second(WAState.CONNECTED);
+ await expect(current).resolves.toBe(true);
+ const lifecycle = (adapter as unknown as { lifecycle: WwebjsLifecycle }).lifecycle;
+ expect(lifecycle.isInNavigationReinjectWindow()).toBe(false);
+ client.pupPage.emit('framenavigated', navFrame());
+ first(WAState.CONNECTED);
+ await expect(older).resolves.toBe(true);
+ expect(lifecycle.isInNavigationReinjectWindow()).toBe(true);
+ await expect(adapter.getChats()).rejects.toBeInstanceOf(EngineNotReadyError);
+ });
+
it('does not report a transport-matching error as death inside the window (in-flight race)', () => {
const { adapter, client } = wireAdapter();
const report = (adapter as unknown as { reportIfPageTransportError: (e: unknown, c: string) => void })
diff --git a/src/engine/adapters/wwebjs-lifecycle.ts b/src/engine/adapters/wwebjs-lifecycle.ts
index 41fde90a7..3c64dc991 100644
--- a/src/engine/adapters/wwebjs-lifecycle.ts
+++ b/src/engine/adapters/wwebjs-lifecycle.ts
@@ -244,6 +244,8 @@ export class WwebjsLifecycle {
// exact jest timer counts, and a timer would also outlive the single-use adapter.
private lastMainFrameNavigationAt = 0;
private navigationEpisodeStartedAt = 0;
+ /** Counts the navigations stamped, so a probe can tell that one landed while it was waiting. */
+ private navigationCount = 0;
/** The WhatsApp Web build this init asked whatsapp-web.js to pin, undefined when it asked for none.
* Compared against the build the page reports at READY (./wwebjs-running-build). */
private requestedWebVersion: string | undefined;
@@ -760,6 +762,7 @@ export class WwebjsLifecycle {
// lastLoggedOut is a runtime field the wwjs typings do not declare (same loose-cast pattern as
// the pupBrowser/pupPage handles above).
if ((this.client as unknown as { lastLoggedOut?: boolean } | null)?.lastLoggedOut) return;
+ this.navigationCount++;
const now = Date.now();
if (this.navigationEpisodeStartedAt === 0) this.navigationEpisodeStartedAt = now;
this.lastMainFrameNavigationAt = now;
@@ -1162,6 +1165,7 @@ export class WwebjsLifecycle {
// dead on the first tick, and hid a page that really had died.
const probeable = this.status === EngineStatus.READY || this.status === EngineStatus.ACTION_REQUIRED;
if (!probeable || !this.client) return false;
+ const navigationsBefore = this.navigationCount;
let timeout: ReturnType<typeof setTimeout> | undefined;
try {
@@ -1175,8 +1179,9 @@ export class WwebjsLifecycle {
if (state === WAState.CONNECTED) {
// Observed recovery closes the navigation episode. Without this, an episode whose re-inject
// died silently (no 'ready' re-emit, but WA Web's socket back up) would keep a stale episode
- // anchor forever, denying the grace to every LATER navigation via the episode cap.
- this.clearNavigationReinjectWindow();
+ // anchor forever, denying the grace to every LATER navigation via the episode cap. Only an episode
+ // this answer can speak for: a navigation stamped while getState() was pending is a newer one.
+ if (navigationsBefore === this.navigationCount) this.clearNavigationReinjectWindow();
return true;
}
return this.isInNavigationReinjectWindow();