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

Problem Client.destroy() #525

Closed matheushsouto closed 3 years ago

matheushsouto commented 3 years ago

(node:39965) UnhandledPromiseRejectionWarning: Error: Protocol error (Runtime.callFunctionOn): Target closed. at Promise (/home/shooting4/shooting/shchat-node/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:208:63) at new Promise () at CDPSession.send (/home/shooting4/shooting/shchat-node/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:207:16) at ExecutionContext._evaluateInternal (/home/shooting4/shooting/shchat-node/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:200:50) at ExecutionContext.evaluateHandle (/home/shooting4/shooting/shchat-node/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:151:21) at WaitTask.rerun (/home/shooting4/shooting/shchat-node/node_modules/puppeteer/lib/cjs/puppeteer/common/DOMWorld.js:528:37) at process._tickCallback (internal/process/next_tick.js:68:7) (node:39965) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:39965) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Environment (please complete the following information):

WhatsApp

Library

Additional context

Add any other context about the problem here.

Basit-qc commented 3 years ago

Experiencing same issue. Is there any way arond this?

indhio commented 3 years ago

I have same issue here. There is another way to restart the client without stop and run the server?

Basit-qc commented 3 years ago

Observation client.destroy() works fine if client is connected/authenticated.

UPDATE It kills the browser if you have authenticated or not, but still error comes from puppeteer code which I think can be neglected.

a4smanjorg5 commented 3 years ago

try replace method .initialize() to .initialize.catch(ex => {}) on client.initialize(); or try { await client.initialize() } catch {} to handle this promise rejection (this also will resolve the terminated of Node.js in future features because promise rejections)

matheushsouto commented 3 years ago

Thank you a4smanjorg5

venki1410 commented 2 years ago

it shows me error like this

/Users/venkat/project/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:189 throw new Error(${PROCESS_ERROR_EXPLANATION}\nError cause: ${error.stack}); ^

Error: Puppeteer was unable to kill the process which ran the browser binary. This means that, on future Puppeteer launches, Puppeteer might not be able to launch the browser. Please check your open processes and ensure that the browser processes that Puppeteer launched have been killed. If you think this is a bug, please report it on the Puppeteer issue tracker. Error cause: Error: kill EPERM at process.kill (node:internal/process/per_thread:220:13) at BrowserRunner.kill (/Users/venkat/project/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:185:29) at BrowserRunner.close (/Users/venkat/project/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:158:18) at Browser.close (/Users/venkat/project/node_modules/puppeteer/lib/cjs/puppeteer/common/Browser.js:394:35) at Client.destroy (/Users/venkat/project/node_modules/whatsapp-web.js/src/Client.js:478:31) at /Users/venkat/project/node_modules/whatsapp-web.js/src/Client.js:469:28 at runMicrotasks () at processTicksAndRejections (node:internal/process/task_queues:96:5) at BrowserRunner.kill (/Users/venkat/project/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:189:23) at BrowserRunner.close (/Users/venkat/project/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:158:18) at Browser.close (/Users/venkat/project/node_modules/puppeteer/lib/cjs/puppeteer/common/Browser.js:394:35) at Client.destroy (/Users/venkat/project/node_modules/whatsapp-web.js/src/Client.js:478:31) at /Users/venkat/project/node_modules/whatsapp-web.js/src/Client.js:469:28 at runMicrotasks () at processTicksAndRejections (node:internal/process/task_queues:96:5)

venki1410 commented 2 years ago

try catch not working.this is my code

`client.on('disconnected',async(reason)=>{ io.emit('message',{id:id,text:"Mobile Logged out !"}); io.emit('disconnected','disconnected'); fs.unlinkSync(SESSION_FILE_PATH,function(err){ if(err) return console.log(err); console.log('session file destroyed'); });

try {
    await client.destroy();

} catch (error) {
    console.log("catched");
}
 client.initialize(); 

});

` but again it show me the error

/Users/venkat/project/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:189 throw new Error(${PROCESS_ERROR_EXPLANATION}\nError cause: ${error.stack}`); ^

Error: Puppeteer was unable to kill the process which ran the browser binary. This means that, on future Puppeteer launches, Puppeteer might not be able to launch the browser. Please check your open processes and ensure that the browser processes that Puppeteer launched have been killed. If you think this is a bug, please report it on the Puppeteer issue tracker. Error cause: Error: kill EPERM at process.kill (node:internal/process/per_thread:220:13) at BrowserRunner.kill (/Users/venkat/project/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:185:29) at BrowserRunner.close (/Users/venkat/project/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:158:18) at Browser.close (/Users/venkat/project/node_modules/puppeteer/lib/cjs/puppeteer/common/Browser.js:394:35) at Client.destroy (/Users/venkat/project/node_modules/whatsapp-web.js/src/Client.js:478:31) at /Users/venkat/project/node_modules/whatsapp-web.js/src/Client.js:469:28 at runMicrotasks () at processTicksAndRejections (node:internal/process/task_queues:96:5) at BrowserRunner.kill (/Users/venkat/project/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:189:23) at BrowserRunner.close (/Users/venkat/project/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:158:18) at Browser.close (/Users/venkat/project/node_modules/puppeteer/lib/cjs/puppeteer/common/Browser.js:394:35) at Client.destroy (/Users/venkat/project/node_modules/whatsapp-web.js/src/Client.js:478:31) at /Users/venkat/project/node_modules/whatsapp-web.js/src/Client.js:469:28 at runMicrotasks () at processTicksAndRejections (node:internal/process/task_queues:96:5) [nodemon] app crashed - waiting for file changes before starting... `

Olinari commented 1 year ago

same for me try { await client.destroy(); catch (error) { console.log(error); }

...

[nodemon] app crashed - waiting for file changes before starting... `

Shivank12batra commented 1 year ago

same for me as well, the above solution proposed doesn't work as well

joresv commented 1 year ago

I have same issue here. There is another way to restart the client without stop and run the server?

Hi I don't know if can help anyone currently but I leave my solution here. in effect when the server is not restarted and you make a new call to the client it is not instantiated again. the solution to implement is to be able to find this instance and destroy it before making a new instantiation to do this you must:

every time you make a server call, your old instance will be destroyed and you'll have no more problems. I hope this helps someone and find me on YouTube @OnProgramme ;).