Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(engine): report a dead page in the wwebjs forward read

Yudhi Armyndharisabc8f461a69b
diff --git a/src/engine/adapters/wwebjs-client-teardown.spec.ts b/src/engine/adapters/wwebjs-client-teardown.spec.ts
index f2d22a0ef..7e63b80c9 100644
--- a/src/engine/adapters/wwebjs-client-teardown.spec.ts
+++ b/src/engine/adapters/wwebjs-client-teardown.spec.ts
@@ -366,6 +366,26 @@ describe('whatsapp-web.js operations that race a session teardown', () => {
await finish();
});
+ it('lets a forward go on when the read of its destination chat merely fails', async () => {
+ const forward = jest.fn().mockResolvedValue(undefined);
+ const source = { fetchMessages: () => Promise.resolve([{ id: { _serialized: 'M1' }, forward }]) };
+ const destination = { fetchMessages: () => Promise.reject(new Error('Evaluation failed: boom')) };
+ const { adapter } = readyAdapter({
+ getNumberId: () => Promise.resolve(found(CHAT)),
+ getChatById: (id: string) => Promise.resolve(id === '[email protected]' ? source : destination),
+ });
+ const onDisconnected = jest.fn();
+ (adapter as unknown as { callbacks: unknown }).callbacks = { onDisconnected };
+
+ await expect(adapter.forwardMessage('[email protected]', CHAT, 'M1')).resolves.toEqual({
+ id: '',
+ timestamp: expect.any(Number) as unknown,
+ });
+ expect(forward).toHaveBeenCalledTimes(1);
+ expect(onDisconnected).not.toHaveBeenCalled();
+ expect(adapter.getStatus()).toBe(EngineStatus.READY);
+ });
+
it('keeps a send that was already on the wire an outcome-unknown failure', async () => {
const onWire = gate();
const sendMessage = jest.fn(() =>
@@ -444,6 +464,38 @@ const DEATH_CASES: DeathCase[] = [
},
run: adapter => adapter.forwardMessage('[email protected]', CHAT, 'M1'),
},
+ {
+ // A failed destination read never blocks a forward by itself, but a page that died under it does.
+ name: 'the destination chat lookup of a forward',
+ build: () => {
+ const forward = jest.fn();
+ const source = { fetchMessages: () => Promise.resolve([{ id: { _serialized: 'M1' }, forward }]) };
+ return {
+ client: {
+ getNumberId: () => Promise.resolve(found(CHAT)),
+ getChatById: (id: string) => (id === '[email protected]' ? Promise.resolve(source) : Promise.reject(closedTarget())),
+ },
+ unreached: forward,
+ };
+ },
+ run: adapter => adapter.forwardMessage('[email protected]', CHAT, 'M1'),
+ },
+ {
+ name: 'the destination history read of a forward',
+ build: () => {
+ const forward = jest.fn();
+ const source = { fetchMessages: () => Promise.resolve([{ id: { _serialized: 'M1' }, forward }]) };
+ const destination = { fetchMessages: () => Promise.reject(closedTarget()) };
+ return {
+ client: {
+ getNumberId: () => Promise.resolve(found(CHAT)),
+ getChatById: (id: string) => Promise.resolve(id === '[email protected]' ? source : destination),
+ },
+ unreached: forward,
+ };
+ },
+ run: adapter => adapter.forwardMessage('[email protected]', CHAT, 'M1'),
+ },
];
describe('whatsapp-web.js operations whose page dies on its own', () => {
diff --git a/src/engine/adapters/wwebjs-messaging.ts b/src/engine/adapters/wwebjs-messaging.ts
index 0d9631980..9a98c4da6 100644
--- a/src/engine/adapters/wwebjs-messaging.ts
+++ b/src/engine/adapters/wwebjs-messaging.ts
@@ -783,7 +783,7 @@ export class WwebjsMessaging {
// The ids already in that chat are read first, on every attempt, so the recovery below can tell
// the forwarded copy from an earlier send with the same whole-second timestamp. Only messages
// WhatsApp Web already holds are read, so this never pages the chat's history in ahead of the
- // send. A failed read never blocks the forward; it only leaves the copy unidentified.
+ // send. A read that merely fails never blocks the forward; it only leaves the copy unidentified.
// The newest timestamp it saw is kept too: a concurrent call on the destination chat can page
// older history in before the second read, and those messages are older than every one loaded.
let resolvedTo = toChatId;
@@ -798,9 +798,13 @@ export class WwebjsMessaging {
newest: snapshot.reduce((newest, m) => Math.max(newest, m.timestamp), 0),
};
} catch (error) {
+ // A page that died under the read is not a read that merely failed. Chromium stopped from
+ // outside rejects this call before any death listener runs, so report it for the check below.
+ this.host.reportIfPageTransportError(error, 'forwardMessage');
this.host.logger.warn(`Could not read the destination chat before forwarding: ${String(error)}`);
}
- // The read above is a page round trip a teardown can land in, and its failure is only logged.
+ // The read above is a page round trip a teardown can land in. Its failure only leaves the copy
+ // unidentified, unless it shows the page is gone, which this check then refuses.
this.host.ensureReady();
return msgToForward.forward(to);
});