Search code examples
node.jsnode-red

Node red on-close event does not wait for async function to finish


According to the documentation https://nodered.org/docs/creating-nodes/node-js when Node-red (or the specific node in question) closes down,the "close" event is called and if a listener is registered with a parameter it should wait for done() before completely stopping.

this.on('close', function(done) {
    doSomethingWithACallback(function() {
        done();
    });
});

It doesn't work for me though. My mistake, I'm sure, but I don't see where. The following code displays the first "Closing" entry in the log, but not the second entry "Waited enough. Actually finishing now.":

node.on("close", function(done) {
    node.log('Closing.');
    setTimeout(function(){
        node.log('Waited enough.Actually finishing now.');
        done();
    },5000);
});

Can someone please give me a pointer ?

Using: Node-red 0.17.5 node.js 6.14.1

Edit: output log added below

pi@raspberrypi:~ $ node-red-start

Start Node-RED

Once Node-RED has started, point a browser at http://192.168.1.17:1880
On Pi Node-RED works better with the Firefox or Chrome browser

Use   node-red-stop                          to stop Node-RED
Use   node-red-start                         to start Node-RED again
Use   node-red-log                           to view the recent log output
Use   sudo systemctl enable nodered.service  to autostart Node-RED at every boot
Use   sudo systemctl disable nodered.service to disable autostart on boot

To find more nodes and example flows - go to http://flows.nodered.org

Starting as a systemd service.
Started Node-RED graphical event wiring tool..
16 Apr 10:11:27 - [info]
Welcome to Node-RED
===================
16 Apr 10:11:27 - [info] Node-RED version: v0.17.5
16 Apr 10:11:27 - [info] Node.js  version: v6.14.1
16 Apr 10:11:27 - [info] Linux 4.14.30-v7+ arm LE
16 Apr 10:11:30 - [info] Loading palette nodes
16 Apr 10:11:47 - [info] Dashboard version 2.7.0 started at /ui
16 Apr 10:11:50 - [info] Settings file  : /home/pi/.node-red/settings.js
16 Apr 10:11:50 - [info] User directory : /home/pi/.node-red
16 Apr 10:11:50 - [info] Flows file     : /home/pi/.node-red/flows_raspberrypi.json
16 Apr 10:11:50 - [info] Server now running at http://127.0.0.1:1880/
16 Apr 10:11:51 - [info] Starting flows
16 Apr 10:11:51 - [info] Started flows
Stopping Node-RED graphical event wiring tool....
16 Apr 10:12:06 - [info] Stopping flows
16 Apr 10:12:06 - [info] [simple-queue:queue1] Closing.
Stopped Node-RED graphical event wiring tool..

Solution

  • You are hitting a bug that was fixed in Node-RED 0.18.

    Prior to Node-RED 0.18, the code that handled the shutdown of the runtime did not wait for the all of the node close handlers to complete before the process was terminated.