Node-RED crashing at startup due to bad flow context (reproducible)

Environment

MacOS 10.13.6
Node-RED version: v1.0.2
Node.js version: v10.15.3
Dashboard version 2.17.1

Issue

Node-red is crashing at startup

Pauls-mini-4:~ Paul$ node-red
31 Oct 08:47:54 - [info] 

Welcome to Node-RED
===================

31 Oct 08:47:54 - [info] Node-RED version: v1.0.2
31 Oct 08:47:54 - [info] Node.js  version: v10.15.3
31 Oct 08:47:54 - [info] Darwin 17.7.0 x64 LE
31 Oct 08:47:55 - [info] Loading palette nodes
31 Oct 08:47:57 - [warn] rpi-srf : rpi-gpio.errors.ignorenode
31 Oct 08:47:58 - [info] Worldmap version 2.1.4
31 Oct 08:47:58 - [info] Dashboard version 2.17.1 started at /ui
31 Oct 08:47:58 - [info] Settings file  : /Users/Paul/.node-red/settings.js
31 Oct 08:47:58 - [info] Context store  : 'default' [module=localfilesystem]
31 Oct 08:47:58 - [info] Context store  : 'memoryOnly' [module=memory]
31 Oct 08:47:58 - [error] Failed to start server:
31 Oct 08:47:58 - [error] Error: Error loading context store: SyntaxError: Unexpected token u in JSON at position 0
    at /usr/local/lib/node_modules/node-red/node_modules/@node-red/runtime/lib/nodes/context/index.js:177:15
Pauls-mini-4:~ Paul$ 

This is being caused because one of the flow context variables just contains the word undefined.

19%20AM

This occurs if I am using one project, then switch to a second project "The Project', then shutdown NR and start it up again.

It seems that when using the project feature, when you switch between projects, the context folders/files are remembered but the data is not. A flows.json' in a context folder can end up containing just the word undefined` which cause NR to crash on startup.

Note: I'll can recreate this pretty much at will. I'll try to create it with smaller flows but I'm going out for several hours at this point.

1 Like

So I have reproduced this with two projects with one tab each.

  1. Start Node-Red from the command line
  2. create 'project A', insert this flow and deploy
[{"id":"cc0489ce.3b992","type":"tab","label":"Flow A ","disabled":false,"info":""},{"id":"a6585a7b.1c95b8","type":"ui_base","theme":{"name":"theme-light","lightTheme":{"default":"#0094CE","baseColor":"#0094CE","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif","edited":true,"reset":false},"darkTheme":{"default":"#097479","baseColor":"#097479","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif","edited":false},"customTheme":{"name":"Untitled Theme 1","default":"#4B7930","baseColor":"#4B7930","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif"},"themeState":{"base-color":{"default":"#0094CE","value":"#0094CE","edited":false},"page-titlebar-backgroundColor":{"value":"#0094CE","edited":false},"page-backgroundColor":{"value":"#fafafa","edited":false},"page-sidebar-backgroundColor":{"value":"#ffffff","edited":false},"group-textColor":{"value":"#1bbfff","edited":false},"group-borderColor":{"value":"#ffffff","edited":false},"group-backgroundColor":{"value":"#ffffff","edited":false},"widget-textColor":{"value":"#111111","edited":false},"widget-backgroundColor":{"value":"#0094ce","edited":false},"widget-borderColor":{"value":"#ffffff","edited":false},"base-font":{"value":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif"}},"angularTheme":{"primary":"indigo","accents":"blue","warn":"red","background":"grey"}},"site":{"name":"Node-RED Dashboard","hideToolbar":"false","allowSwipe":"false","lockMenu":"false","allowTempTheme":"true","dateFormat":"DD/MM/YYYY","sizes":{"sx":48,"sy":48,"gx":6,"gy":6,"cx":6,"cy":6,"px":0,"py":0}}},{"id":"25c6c8ec.665608","type":"inject","z":"cc0489ce.3b992","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":150,"y":40,"wires":[["8b4bb50c.ada018"]]},{"id":"8b4bb50c.ada018","type":"change","z":"cc0489ce.3b992","name":"Set 'flow.movefile' to true (testing)","rules":[{"t":"set","p":"movefile","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":220,"y":120,"wires":[["f784e40f.7728f"]]},{"id":"f784e40f.7728f","type":"debug","z":"cc0489ce.3b992","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":150,"y":200,"wires":[]}]
  1. create 'project B', insert this flow and deploy
[{"id":"7208c0c6.105408","type":"tab","label":"Project B","disabled":false,"info":""},{"id":"1ab4cfdc.a4ac5","type":"ui_base","theme":{"name":"theme-light","lightTheme":{"default":"#0094CE","baseColor":"#0094CE","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif","edited":true,"reset":false},"darkTheme":{"default":"#097479","baseColor":"#097479","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif","edited":true,"reset":false},"customTheme":{"name":"Untitled Theme 1","default":"#4B7930","baseColor":"#4B7930","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif"},"themeState":{"base-color":{"default":"#0094CE","value":"#0094CE","edited":false},"page-titlebar-backgroundColor":{"value":"#0094CE","edited":false},"page-backgroundColor":{"value":"#fafafa","edited":false},"page-sidebar-backgroundColor":{"value":"#ffffff","edited":false},"group-textColor":{"value":"#1bbfff","edited":false},"group-borderColor":{"value":"#ffffff","edited":false},"group-backgroundColor":{"value":"#ffffff","edited":false},"widget-textColor":{"value":"#111111","edited":false},"widget-backgroundColor":{"value":"#0094ce","edited":false},"widget-borderColor":{"value":"#ffffff","edited":false},"base-font":{"value":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif"}},"angularTheme":{"primary":"indigo","accents":"blue","warn":"red","background":"grey"}},"site":{"name":"Node-RED Dashboard","hideToolbar":"true","allowSwipe":"false","lockMenu":"false","allowTempTheme":"true","dateFormat":"DD/MM/YYYY","sizes":{"sx":48,"sy":48,"gx":6,"gy":6,"cx":6,"cy":6,"px":0,"py":0}}},{"id":"8c60a36f.c85da8","type":"ui_group","z":"","name":"LED ","tab":"e7eda5d6.a835a8","order":1,"disp":true,"width":"8","collapse":false},{"id":"e7eda5d6.a835a8","type":"ui_tab","z":"","name":"LEDs","icon":"dashboard","order":6,"disabled":false,"hidden":false},{"id":"caed8c62.99b478","type":"function","z":"7208c0c6.105408","name":"Turn selected LED ON or OFF","func":"// var groupID = 6;\n\n// var nodeID = 90;\n\nvar groupID = flow.get(\"groupID\")||0;\nvar nodeID  = flow.get(\"nodeID\")||0;\n\nvar logic_state = msg.payload.state;\n\nvar gpioPin = msg.payload.pin;\nmsg.payload=\"event,sendAction=\"+groupID+\",\"+nodeID+\",\"+logic_state+\",\"+gpioPin;\n\nvar date = new Date(); // Or the date you'd like converted.\nisoDate = new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString();\nmsg.date = isoDate.slice(0,10);\nmsg.time = isoDate.slice(11,19);\n   \nnode.status({fill:\"green\",shape:\"dot\",text:\"Last request to node\"+ nodeID + \"  was made at: \" + msg.time + \" on \" + msg.date}); \n\nreturn msg;","outputs":1,"noerr":0,"x":230,"y":320,"wires":[[]]},{"id":"5892fb98.c071cc","type":"ui_button","z":"7208c0c6.105408","name":"RED led","group":"8c60a36f.c85da8","order":7,"width":"1","height":"1","passthru":false,"label":"","tooltip":"","color":"white","bgcolor":"{{msg.background}}","icon":"highlight","payload":"1","payloadType":"str","topic":"","x":160,"y":240,"wires":[["a9033ce.3d8d24"]]},{"id":"424d6907.a869c","type":"function","z":"7208c0c6.105408","name":"","func":"var red_status = flow.get(\"red_status\")||\"off\";\n\nif (red_status == \"off\")\n{\n    msg.background=\"grey\";\n}\nelse if (red_status == \"on\")\n{\n    msg.background=\"red\";\n}\nreturn msg;","outputs":1,"noerr":0,"x":150,"y":180,"wires":[["5892fb98.c071cc"]]},{"id":"d1b66fb5.25f898","type":"inject","z":"7208c0c6.105408","name":"Initial conditions","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":true,"onceDelay":"1","x":160,"y":60,"wires":[["8b782467.f57d68"]]},{"id":"a9033ce.3d8d24","type":"function","z":"7208c0c6.105408","name":"","func":"//var red_status = \"off\"; \n//red_status = flow.get(\"red_status\")||\"off\";\nvar red_status = flow.get(\"red_status\")||\"off\";\n\nif (red_status == \"off\")\n{\n    red_status = \"on\"\n    msg.background=\"red\";\n    msg.payload = {\"pin\":15,\"state\":1}\n}\nelse if (red_status == \"on\")\n{\n    red_status=\"off\"\n    msg.background=\"grey\";\n    msg.payload = {\"pin\":15,\"state\":0}\n}\nflow.set(\"red_status\",red_status);\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":240,"wires":[["5892fb98.c071cc","caed8c62.99b478"]]},{"id":"8b782467.f57d68","type":"change","z":"7208c0c6.105408","name":"Set inital conditions","rules":[{"t":"set","p":"red_status","pt":"flow","to":"off","tot":"str"},{"t":"set","p":"yellow_status","pt":"flow","to":"off","tot":"str"},{"t":"set","p":"green_status","pt":"flow","to":"off","tot":"str"},{"t":"set","p":"nodeID","pt":"flow","to":"25","tot":"num"},{"t":"set","p":"groupID","pt":"flow","to":"2","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":170,"y":120,"wires":[["424d6907.a869c"]]}]
  1. Switch back to project A

  2. from the command line ctrl-c out of NR

  3. open a os window so you can watch the .node-red directory contents. If there is a context folder, delete it. Sort the view by date show you can watch the context folders appear.

  4. start NR again

  5. in NR click the inject button for the first flow then quickly go to the projects option and switch to the other project, Press the inject button and swap back to the terminal and ctrl-c out of NR.

  6. at this point you if you look at the contect folder, you should see two sub folders. If you open them both one should have a flow.json that is 9 bytes long. If you open it it will contain the word undefined.

If you try to start NR it will crash with the above information.

If you delete the context folder that contains that 9 byte flow.json file, NR will start up fine.

My guess is that since the first project's context wasn't written out when swapped to the second project, something is hanging around and when NR is shut down, the contect fiiles are written, but the original one's data is gon and that is why it is undefined (of course I could very very wrong too :slightly_smiling_face:)

1 Like

Issue opened: https://github.com/node-red/node-red/issues/2369