Skip to content

Fix context destroyed on client initialize #3531

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
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

aleborile
Copy link

@aleborile aleborile commented Apr 27, 2025

PR Details

Catch errors during intialization ONLY if page reloading events occours

Description

in Client.initialize() function an handler of framenavigated event is created to track that a page reloading is ongoing and context can be destroyed in any moment. If some error happens due to framenavigated the flow can return normally because another Client.initialize() are going to be re-executed (Client.js:339)

Related Issue(s)

closes #2056

Motivation and Context

The change is required to handle errors from context destroyed correclty during initialization without killing the process if unpredictable (by Meta) refresh happens.

Error raised on reloading when initialization is in progress:
at rewriteError (/usr/src/app/node_modules/puppeteer-core/lib/cjs/puppeteer/common/ExecutionContext.js:284:15) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async ExecutionContext._ExecutionContext_evaluate (/usr/src/app/node_modules/puppeteer-core/lib/cjs/puppeteer/common/ExecutionContext.js:227:56) at async ExecutionContext.evaluate (/usr/src/app/node_modules/puppeteer-core/lib/cjs/puppeteer/common/ExecutionContext.js:107:16) at async Client.getWWebVersion (/usr/src/app/node_modules/whatsapp-web.js/src/Client.js:811:16) at async Client.inject (/usr/src/app/node_modules/whatsapp-web.js/src/Client.js:98:25) at async Client.initialize (/usr/src/app/node_modules/whatsapp-web.js/src/Client.js:329:9)

How Has This Been Tested

My clients in production never crash anymore during initialization and connect correctrly at first run.

Environment

  • Machine OS: Docker + Amazon Linux 2023
  • Phone OS: Android
  • Library Version: 1.27.0
  • WhatsApp Web Version: 2.3000.1022278655
  • Puppeteer Version: 18.2.1
  • Browser Type and Version: Chromium
  • Node Version: ^20

Types of changes

  • Dependency change
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • I have updated the documentation accordingly (index.d.ts).
  • I have updated the usage example accordingly (example.js)

volveezz referenced this pull request in volveezz/whatsapp-web.js May 13, 2025
@hkorhola
Copy link

have you @aleborile been able to run wa-app in a stable way with this version? Or only tested it a bit locally? we are suffering from similar "execution context destroyed" problems but I am not 100% sure at what point they exactly occur.

@aleborile
Copy link
Author

I tested locally and on production for couple of weeks without any problem since release.
NOTE that this fix cover only execution context errors during initialization, if your errors occurs in other steps, this will not fix your application.

@tuyuribr
Copy link
Collaborator

Please, only change the code you are supose to fix, revert all other changes.

@aleborile aleborile force-pushed the fix-context-destroyed-on-initialize branch 2 times, most recently from affcb0a to 8d5a60f Compare May 28, 2025 15:17
@aleborile
Copy link
Author

im sorry, now i kept only the main fix described by this pr. the second commit its just formatting

@tuyuribr tuyuribr added the AI PR Not necessary a bad pr, must review with extra caution label Jun 10, 2025
@elhumbertoz
Copy link
Contributor

I frequently encounter this issue (Fix context destroyed). Incorporating this PR would be highly beneficial, as it always occurs unexpectedly.

@aleborile aleborile force-pushed the fix-context-destroyed-on-initialize branch from d5d72ef to 8d5a60f Compare June 19, 2025 09:14
@aleborile aleborile force-pushed the fix-context-destroyed-on-initialize branch from 8d5a60f to 92a5010 Compare June 19, 2025 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AI PR Not necessary a bad pr, must review with extra caution bad pr
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exception when calling fetchMessages method
4 participants