Isaksson / node-red-contrib-unifi

MIT License
50 stars 19 forks source link

unifiWS stability issue #128

Open jmorris644 opened 5 months ago

jmorris644 commented 5 months ago

if I deploy a nodeWS and then subsequently change the config node, unifiWS does not recognize the change and goes into a reconnect mode roughly every 2-3 seconds. Twice now this has crashed node-red for me.

Upon the node-red restart or if I delete the unifiWS node and drop a new one into the flow then everything seems to be ok.

jmorris644 commented 5 months ago

Here is the log output 23 Feb 07:31:22 - [red] Uncaught Exception: 23 Feb 07:31:22 - [error] SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at WebSocket.message (/home/joe/.node-red/node_modules/node-red-contrib-unif i/unifiws-helper.js:69:40) at WebSocket.emit (node:events:513:28) at Receiver.receiverOnMessage (/home/joe/.node-red/node_modules/ws/lib/webso cket.js:1184:20) at Receiver.emit (node:events:513:28) at Receiver.dataMessage (/home/joe/.node-red/node_modules/ws/lib/receiver.js :541:14) at Receiver.getData (/home/joe/.node-red/node_modules/ws/lib/receiver.js:459 :17) at Receiver.startLoop (/home/joe/.node-red/node_modules/ws/lib/receiver.js:1 58:22) at Receiver._write (/home/joe/.node-red/node_modules/ws/lib/receiver.js:84:1 0) at writeOrBuffer (node:internal/streams/writable:391:12) at _write (node:internal/streams/writable:332:10) at Receiver.Writable.write (node:internal/streams/writable:336:10) at TLSSocket.socketOnData (/home/joe/.node-red/node_modules/ws/lib/websocket .js:1278:35) at TLSSocket.emit (node:events:513:28) at addChunk (node:internal/streams/readable:315:12) at readableAddChunk (node:internal/streams/readable:289:9) at TLSSocket.Readable.push (node:internal/streams/readable:228:10) at TLSWrap.onStreamRead (node:internal/stream_base_commons:190:23) nodered.service: Main process exited, code=exited, status=1/FAILURE nodered.service: Failed with result 'exit-code'.

Isaksson commented 5 months ago

Thanks for the feedback and log, the WS is still in "beta" and this is the first input that I get from a user when it comes to WS, I will take a look at it.

Steve-Mcl commented 4 months ago

FYI, another user encountered a very similar crash reported here: https://discourse.nodered.org/t/random-restarts/86752?u=steve-mcl

26 Mar 06:43:06 - [red] Uncaught Exception:
26 Mar 06:43:06 - [error] SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at WebSocket.message (/home/pi/.node-red/node_modules/node-red-contrib-unifi/unifiws-helper.js:69:40)

A quick glimps at line 69 i see you are parsing data input without any guards or error handling: https://github.com/Isaksson/node-red-contrib-unifi/blob/c05f2857c962ca3c7e7aeb0734447bb20ef6d59b/unifiws-helper.js#L69

It would probably be worth first checking the data value before parsing (is it getting partial / split packets that you need to build up before attempting a parse?) or at least, wrap it in a try catch and forward the error in the callback?

Isaksson commented 4 months ago

Hello. Yes, I am aware of that and its fixed in the upcoming release.