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.64k stars 3.73k forks source link

When i disconnect a client from the whatsapp app, puppeteer throws an error that damages any client i've created #2581

Open luisdbarboza opened 1 year ago

luisdbarboza commented 1 year ago

Is there an existing issue for this?

Describe the bug

Well, like the title says, if i unlink the whatsapp client using my phone's whatsapp app, the expected behaviour is for the client to emit the disconnected event, just 1 client, the one that got disconnected, but instead, what happens is that i get a log like this on my console: uncaughtException: Protocol error (Network.getResponseBody): Session closed. Most likely the page has been closed.

Now, this exception damages any other client i may have open...

Meaning, if i use the localAuth strategy, and use multiple clients in my app, if only 1 of let's say, 10 clients disconnects from their phone, 10 clients are affected, because of the puppeteer exception that gets thrown.

I'm using manjaro os for testing, an arch linux distro btw.

Expected behavior

I guess just 1 client should get disconnected and every other one should work normally.

Steps to Reproduce the Bug or Issue

Use the localAuth strategy, and connect multiple clients.

Connect a client scanning the qr code.

Disconnect the client from the whatsapp mobile app.

An error gets thrown.

Now my webserver throws 500 internal server error on any occasion.

Relevant Code

No response

Browser Type

Chromium

WhatsApp Account Type

Standard

Does your WhatsApp account have multidevice enabled?

Yes, I am using Multi Device

Environment

Manjaro os, an arch linux distro. Whatsapp web version https://github.com/Julzk/whatsapp-web.js/tarball/jkr_hotfix_7

nodejs version v18.18.0

Additional context

No response

a20000110 commented 1 year ago

You can use the following code in your entry file, which will catch exceptions and will not cause your application to crash. `process.on('uncaughtException', (err) => {

})`

luisdbarboza commented 1 year ago

@a20000110 do you mean attaching an event handler to the Client object?

luisdbarboza commented 1 year ago

For additional info, i created a client with the localAuth strategy, i sent a message with the client, and after i closed the session from the whatsapp app, the library throws this error:

uncaughtException: Protocol error (Networ.getResponseBody): Session closed. Most likely the page has been closed at CDPSession.send (...restOfThePath/node_modules/puppeteer/lib/cjs/puppeteer/common/HTTPResponse.js:128:57

luiscuriel commented 1 year ago

How are you closing the app? You need to call client.destroy()

luisdbarboza commented 1 year ago

Hmm, yes, i'm destroying the client every time, but you see, even if i delete the .wwebjs_auth, or even if i don't do anything in the client.on('disconnected'. (callback)) event handler, it stills throws the error i described above

sgbtz commented 1 year ago

Same issue here. I tried to add a try catch in the entire Cliente intializing function but puppeteer error is still crashing the server. @a20000110 solution is a workaround for ANY not handled exception so be careful using it. The best solution is to handle the exception directly.

luisdbarboza commented 1 year ago

@a20000110 solution isn't working for me, because well, the app itself is not crashing completely, if i put an event handler for the uncaughtException i can log the error in the console, the process doesn't stop, but, i get the uncaughtException error logged in the console, and also my endpoints start sending responses with status 500 internal sever error

aldhinya commented 1 year ago

Same issue here.

venturion commented 1 year ago

any updates?

jpdev01 commented 11 months ago

Same issue here: Protocol error (Runtime.callFunctionOn): Session closed. Most likely the page has been closed.

ekapasha17 commented 10 months ago

same here, I am facing the same issue

alechkos commented 10 months ago

@ekapasha17

same here, I am facing the same issue

Check #2661

ekapasha17 commented 10 months ago

@ekapasha17

same here, I am facing the same issue

Check #2661

not solving the problem yet

AndisKush commented 10 months ago

I've noticed that when Chromium is closed (can test in task manager), the event client.on('disconected'... is not fired And when try to send any message the error occurs: Error: Protocol error (Runtime.callFunctionOn): Session closed. Most likely the page has been closed.

Anyone facing this issue?

DukazzCruz commented 7 months ago

Work for me install independent "puppeteer": "^22.6.4". Try it.

Shahista96 commented 1 month ago

Is this issue resolved? If I unlink my device from whatsapp mobile app, then i am not able to re-scan the QRs generated. Can someone please help. I am quite new to this.