pedroslopez / whatsapp-web.js

A WhatsApp client library for NodeJS that connects through the WhatsApp Web browser app
https://wwebjs.dev
Apache License 2.0
15.21k stars 3.63k forks source link

Exception when calling fetchMessages method #2056

Open agstrc opened 1 year ago

agstrc commented 1 year ago

Is there an existing issue for this?

Describe the bug

Occasionally, the fetchMessages method will throw the following exception:

/whatsapp-worker/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:265
                throw new Error('Execution context was destroyed, most likely because of a navigation.');
                      ^
Error: Execution context was destroyed, most likely because of a navigation.
    at rewriteError (/whatsapp-worker/node_modules/puppeteer/src/common/ExecutionContext.ts:322:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at ExecutionContext._evaluateInternal (/whatsapp-worker/node_modules/puppeteer/src/common/ExecutionContext.ts:271:7)
    at ExecutionContext.evaluate (/whatsapp-worker/node_modules/puppeteer/src/common/ExecutionContext.ts:140:12)
    at async PrivateChat.fetchMessages (/whatsapp-worker/node_modules/whatsapp-web.js/src/structures/Chat.js:179:24)

I have observed the issue when, after initializing the client, loading all chat instances and attempting to fetch the messages on each of them. It does not happen everytime, but often enough to be observed, as I have multiple clients (in separate containers) that will often restart the wwebjs client and attemp to fetch the messages.

Expected behavior

As a user, I expected the messages to be returned, instead of getting an exception.

Steps to Reproduce the Bug or Issue

  1. Initialize the client
  2. Wait 10 seconds (used mainly to wait for new messages to load in)
  3. Load all chat instances
  4. Loop over the chat instances and attempt to fetch each instance's messages

Relevant Code

await client.initialize()
await new Promise((r) => setTimeout(r, 10_000));
const chats = await client.getChats();
for (const chat of chats.slice(0, 50)) {
  const messages = await chat.fetchMessages({ limit: 256 });
  // do something with the messages
}

Browser Type

Chromium

WhatsApp Account Type

Standard

Does your WhatsApp account have multidevice enabled?

Yes, I am using Multi Device

Environment

OS: Docker (with image node:18) WhatsApp Web version: 2.2311.5 Node.js version: v18.15.0

Additional context

This happens mainly on accounts that receive a large number of messages. These accounts tend to load in older messages after the account has initialized. That is to say, new messages show up after initialization, but these messages were sent before the client started up.

ankit-pn commented 3 months ago

Getting the same bug. Any update on this. How you solved this @agstrc