Skip to content
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

state node seems to crash node red server #18

Open
SirYaro opened this issue Sep 30, 2024 · 3 comments
Open

state node seems to crash node red server #18

SirYaro opened this issue Sep 30, 2024 · 3 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@SirYaro
Copy link

SirYaro commented Sep 30, 2024

On my NR deployment as below:

1 Oct 00:34:49 - [info] Node-RED version: v3.1.5
1 Oct 00:34:49 - [info] Node.js  version: v18.19.0
1 Oct 00:34:49 - [info] Linux 6.6.16-haos x64 LE
1 Oct 00:34:51 - [info] Loading palette nodes
1 Oct 00:34:52 - [info] Node-RED Contrib Theme Collection version: v3.1.9
1 Oct 00:34:59 - [info] Dashboard version 3.6.2 started at /endpoint/ui

I set up a simple flow:

[{"id":"aa81c317a725a8f3","type":"set-shared-state","z":"8e8f5a68f5eb9b9d","g":"b01913e064169ca1","state":"7ea309fdc0ceb6d8","name":"yaro_swiatlo_lewy","triggerOnInit":true,"triggerOnChange":true,"provideOutput":false,"outputs":0,"x":470,"y":4500,"wires":[]},{"id":"3b86614cbf559f9d","type":"mqtt in","z":"8e8f5a68f5eb9b9d","g":"b01913e064169ca1","name":"przełacznik ","topic":"zigbee2mqtt/wlacznik-2klik-C-warsztat","qos":"2","datatype":"auto-detect","broker":"224853ed.7c89fc","nl":false,"rap":true,"rh":0,"inputs":0,"x":100,"y":4500,"wires":[["c66f5b9c7ee8bcbe"]]},{"id":"67c2c43140b7ee2a","type":"debug","z":"8e8f5a68f5eb9b9d","g":"b01913e064169ca1","name":"debug 49","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":460,"y":4560,"wires":[]},{"id":"c66f5b9c7ee8bcbe","type":"function","z":"8e8f5a68f5eb9b9d","g":"b01913e064169ca1","name":" ","func":"\nreturn msg.payload;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":270,"y":4500,"wires":[["aa81c317a725a8f3","67c2c43140b7ee2a"]]},{"id":"7ea309fdc0ceb6d8","type":"shared-state","name":"yaro_swiatlo_lewy","lbl":"","tags":"yaro,włacznik,lewy","historyCount":"2","dataType":"obj","boolType":"bool","boolStrTrue":"","boolStrFalse":"","precision":"","numMin":"","numMax":"","unit":"","saveInterval":"0"},{"id":"224853ed.7c89fc","type":"mqtt-broker","name":"HA MQTT","broker":"localhost","port":"1883","clientid":"node-red","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]
obraz

When "mqtt in" node sends a message similar to this:

{"topic":"zigbee2mqtt/wlacznik-2klik-C-warsztat","payload":{"action":null,"device_temperature":23,"flip_indicator_light":"ON","flip_indicator_light_left":"ON","linkquality":232,"mode_switch":null,"operation_mode_left":null,"operation_mode_right":null,"power_outage_count":4,"power_outage_memory":null,"state_left":"OFF","state_right":"OFF","update":{"installed_version":-1,"latest_version":-1,"state":null},"update_available":null},"qos":0,"retain":false,"_topic":"zigbee2mqtt/wlacznik-2klik-C-warsztat","_msgid":"4b9575a56a9036e3"}

It always crashed my NR server with below exception.
It also crash it during restart attempt but approximately only about 5 times out of 6 attempts.

1 Oct 00:30:14 - [red] Uncaught Exception:
1 Oct 00:30:14 - [error] SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
    at stateNode.update (/config/node_modules/node-red-contrib-state/lib/state.js:137:23)
    at setStateNode._inputCallback (/config/node_modules/node-red-contrib-state/lib/setState.js:47:24)
    at /opt/node_modules/@node-red/runtime/lib/nodes/Node.js:214:26
    at Object.trigger (/opt/node_modules/@node-red/util/lib/hooks.js:166:13)
    at setStateNode.Node._emitInput (/opt/node_modules/@node-red/runtime/lib/nodes/Node.js:206:11)
    at setStateNode.Node.emit (/opt/node_modules/@node-red/runtime/lib/nodes/Node.js:190:25)
    at setStateNode.Node.receive (/opt/node_modules/@node-red/runtime/lib/nodes/Node.js:499:10)
    at deliverMessageToDestination (/opt/node_modules/@node-red/runtime/lib/flows/Flow.js:800:40)
    at Immediate.<anonymous> (/opt/node_modules/@node-red/runtime/lib/flows/Flow.js:816:21)
[00:30:14] INFO: Service Node-RED exited with code 1 (by signal 0)
@lorenwest
Copy link
Owner

The problem is your state file on disk is corrupt. You will need to either manually fix the file or remove it completely.

The offending line is this one:

await writeFile(node.stateFile, JSON.stringify(node.exposedState()));

If the process fails during the write of that file, it can leave it in a partially saved (corrupt) state.

If anyone is willing to make an improvement to this library, a good FS save change would be to write the state to a temporary file, then move that file into place. The move is autonomous - it either works 100% or fails 100%, so it won't leave a file in a partially saved state.

Leaving open for discussion if anyone wants to make this improvement.

@lorenwest lorenwest added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels Oct 1, 2024
@lorenwest
Copy link
Owner

Another improvement would be to add error handling on the FS.readFile side so it doesn't crash Node-RED if it tries to load a corrupt file. It wouldn't read the file, but at least it wouldn't crash Node-RED.

@SirYaro
Copy link
Author

SirYaro commented Oct 1, 2024

Is it possible that the incoming message was somehow "wrong", and could not be parsed and saved? Because that's how it looked from my side.
I was tinkering with the function node and when I put there a certain return message everything went to hell. :)
When I changed function code, it fixed the problem immediately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants