particle-iot / node-red-contrib-particle-official

Official Node-RED Particle nodes
MIT License
12 stars 7 forks source link

Cannot destructure property 'onEvent' of 'undefined' as it is undefined. #3

Open TheCranston opened 2 years ago

TheCranston commented 2 years ago

I've had the particle integration via node-red working for quite a while now and at some point a guess a version change happened and now every so many hours I get this cycle of "Reauthenticating" then "exception" and finally node red restarts.

Not quite sure what's happening. Below is the log of one of these events.

Reauthenticating...
Reauthenticating...
Reauthenticating...
Reauthenticating...
Authenticated with Particle
13 Jan 10:49:13 - [red] Uncaught Exception:
13 Jan 10:49:13 - [error] TypeError: Cannot destructure property 'onEvent' of 'undefined' as it is undefined.
    at Api.listenToEventStream (/config/node-red/node_modules/@particle/node-red-contrib-particle-official/src/api.js:53:24)
    at login.then.retryIn (/config/node-red/node_modules/@particle/node-red-contrib-particle-official/src/api.js:169:29)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
[10:49:13] INFO: Starting Node-RED...
Welcome to Node-RED
===================
13 Jan 10:49:18 - [info] Node-RED version: v2.1.5
13 Jan 10:49:18 - [info] Node.js  version: v16.13.2
13 Jan 10:49:18 - [info] Linux 5.10.63-v8 arm64 LE
13 Jan 10:49:19 - [info] Loading palette nodes
13 Jan 10:49:26 - [info] Dashboard version 3.1.3 started at /endpoint/ui
13 Jan 10:49:27 - [info] Settings file  : /etc/node-red/config.js
13 Jan 10:49:27 - [info] Context store  : 'default' [module=memory]
13 Jan 10:49:27 - [info] User directory : /config/node-red/
13 Jan 10:49:27 - [warn] Projects disabled : editorTheme.projects.enabled=false
13 Jan 10:49:27 - [info] Flows file     : /config/node-red/flows.json
13 Jan 10:49:27 - [info] Server now running at http://127.0.0.1:46836/
13 Jan 10:49:27 - [info] Starting flows
13 Jan 10:49:27 - [info] Started flows
13 Jan 10:49:27 - [info] [server:Home Assistant] Connecting to http://x.x.x.x:8123
13 Jan 10:49:27 - [info] [server:Home Assistant] Connected to http://x.x.x.x:8123
Authenticated with Particle
TheCranston commented 2 years ago

@rickkas7 Good morning. The uncaught exception restart of nodered is happening several times a day. Is there a deeper debug trace I can capture that will help? I wouldn't have known this is happening if I did have an event notification push to my phone on state change in the flow. Anyway, please let me know best how to proceed. thanks!

TheCranston commented 2 years ago

More findings. So it appears that the issue is around the re-authentication timeout. I found res.body.expires_in: 7776000 which works out to be about 2 hours in ms. The same amount of time between faults and restarts. I over rode the value and set it to the max and it's been "holding" just fine. That's a work around and the core issue of a fault in the re-auth code persists. I'm not a javascript coder so I'm outta my depth on how to fix it or I'd have submitted a PR. Any and all help is appreciated. thanks!

TheCranston commented 2 years ago

Additional testing outside of homeasistant on a plain desktop install of node-red yielded the same results. However I was able to scope it down to the function nodes. the subscribe nodes do not exhibit this issue causing node-red to throw an uncaught exception. the desktop was running node-red v2.1.5 with node.js v17.3.0 on x64 linux

FranGarVal commented 2 years ago

I have the same problem when a migrate to v3 so I revert to 2.2.2 and is working again (node.js 16). I also tried another node from other author "node-red-contrib-particle", without success (similar problem?). Probably changed authentication particle's answer and newer versions are more unstable to the change.

TheCranston commented 2 years ago

@FranGarVal That has been my workaround as well. However it's not a long term option. Given https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=nodejs I'd like to keep reasonably updated to mitigate security issues.

FranGarVal commented 2 years ago

I installed in Raspberry Pi (Node v18.7.0) and exactly same problem :frowning:

Reauthenticating... Authenticated with Particle 20 Aug 15:21:23 - [red] Uncaught Exception: 20 Aug 15:21:23 - [error] TypeError: Cannot destructure property 'onEvent' of 'undefined' as it is undefined. at Api.listenToEventStream (/home/fran/.node-red/node_modules/@particle/node-red-contrib-particle-official/src/api.js:53:24) at login.then.retryIn (/home/fran/.node-red/node_modules/@particle/node-red-contrib-particle-official/src/api.js:169:29) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) nodered.service: Main process exited, code=exited, status=1/FAILURE nodered.service: Failed with result 'exit-code'.

sberkovitz commented 1 year ago

I've fixed this in a fork. Until it is merged in, you can install direct with

npm install git+https://github.com/sberkovitz/node-red-contrib-particle-official.git

mjscoiot commented 10 months ago

More findings. So it appears that the issue is around the re-authentication timeout. I found res.body.expires_in: 7776000 which works out to be about 2 hours in ms. The same amount of time between faults and restarts. I over rode the value and set it to the max and it's been "holding" just fine. That's a work around and the core issue of a fault in the re-auth code persists. I'm not a javascript coder so I'm outta my depth on how to fix it or I'd have submitted a PR. Any and all help is appreciated. thanks!

How did you override this setting?

TheCranston commented 10 months ago

@mjscoiot in api.js I simply used the max value rather than lower of max and the expires_in value received. nasty hack. I'm not a java programmer for certain. A far better approach is what @sberkovitz did in his fork. I'd consider that the correct path as it actually addresses the exception. Let's hope that his PR is accepted.

mjscoiot commented 10 months ago

I ended up just commenting out everthing in the _reauthenticate function after .login such that basically it doesn't retry the login within that section of code in @.***/node-red-contrib-particle-official/src/api.js`.

Seems to work flawlessly since login rarely fails.

On Sat, Dec 23, 2023 at 7:21 AM TheCranston @.***> wrote:

@mjscoiot https://github.com/mjscoiot in api.js I simply used the max value rather than lower of max and the expires_in value received. nasty hack. I'm not a java programmer for certain. A far better approach is what @sberkovitz https://github.com/sberkovitz did in his fork. I'd consider that the correct path as it actually addresses the exception. Let's hope that his PR is accepted.

— Reply to this email directly, view it on GitHub https://github.com/particle-iot/node-red-contrib-particle-official/issues/3#issuecomment-1868293714, or unsubscribe https://github.com/notifications/unsubscribe-auth/BE3367HLOBIAIRAWNLSRH33YK3LEBAVCNFSM5L4LWV7KU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOBWHAZDSMZXGE2A . You are receiving this because you were mentioned.Message ID: <particle-iot/node-red-contrib-particle-official/issues/3/1868293714@ github.com>