Skip to content

Exception when calling fetchMessages method #2056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task done
agstrc opened this issue Mar 9, 2023 · 4 comments · May be fixed by #3531
Open
1 task done

Exception when calling fetchMessages method #2056

agstrc opened this issue Mar 9, 2023 · 4 comments · May be fixed by #3531
Labels
bug Something isn't working

Comments

@agstrc
Copy link

agstrc commented Mar 9, 2023

Is there an existing issue for this?

  • I have searched the existing issues

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.

@agstrc agstrc added the bug Something isn't working label Mar 9, 2023
@ankit-pn
Copy link

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

@ZhymabekRoman
Copy link

Same problem

@DiogoThomaz
Copy link

@agstrc @ankit-pn @ZhymabekRoman

I made a bit of a hack, it's not pretty, but it solved the problem.

Apparently, this error happens because Puppeteer is trying to access a resource from a screen that no longer exists.

This makes sense, since there are several asynchronous calls in the initialize method, right when the login screen transitions to the app screen.

function main() {

   // 1. Hack at the process level to ignore the error -- DESPERATION
    process.on('unhandledRejection', async (reason, promise) => {
        console.log('Unhandled Error:', reason);
        
        // Errors that the program should ignore
        const whiteListError = [
            'Execution context was destroyed',
        ];
        const errorMessage = reason instanceof Error ? reason.message : String(reason);

       if (!whiteListError.includes(errorMessage)) {
            // Code to stop your bot
           // Example:
          // console.log('KILL BOT');
          // setTimeout(() => process.exit(0), 3000);
        }
    });

    // 2. CODE TO START WEBJS BOT
   //....
   client.initialize();

}

Browser Type
Chromium

WhatsApp Account Type
Business

Does your WhatsApp account have multidevice enabled?
Yes, I am using Multi Device

Environment
OS: Kubernets (with image node:18)
Node.js version: v18.15.0
CPU reserved 250, limit 2000
MEMORY reserved 250 mb, limit 1024 mb

@DiogoThomaz
Copy link

@pedroslopez

Would it be feasible to add a coercion argument to handle this error at the process level?
Example:

const client = new Client({
  // custom configs
  coerce: ['Execution context was destroyed'] // ignore exception at process level
})

Otherwise, could you help us solve this issue?

@alechkos alechkos mentioned this issue Dec 26, 2024
2 tasks
@aleborile aleborile linked a pull request Apr 27, 2025 that will close this issue
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants