Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node child process, channel closed on process.send

Inside my worker file I listen for a data callback. someLib is node-serialport.

process.on('message', function(msg) {
    someLib.on('data', function(data){
        console.log('some data');
        process.send(data);
    });
});

This prints

some data
Error: channel closed

But

process.on('message', function(msg) {
    process.send('foobar');
});

works fine. It is strange but sometimes the first code example works, so the channel closed error appears randomly.

From http://nodejs.org/api/child_process.html#child_process_event_error I get the info that the error is triggered when

Sending a message to the child process failed for whatever reason.

What is "whatever reason"? Any ideas?

like image 775
UpCat Avatar asked Jun 08 '14 20:06

UpCat


People also ask

What is child_process spawn?

spawn returns an object with stdout and stderr streams. You can tap on the stdout stream to read data that the child process sends back to Node. stdout being a stream has the "data", "end", and other events that streams have.

How does one leave a spawned process running after the application itself closes?

if you want to relaunch app again then you should just close or hide all windows and activate setInterval to keep checking if child process is alive or not with help of pid ... after that you can create new window again or show hidden window... and if you want fresh process then you should relaunch app after you get ...

What is spawnSync?

spawnSync() function provides equivalent functionality in a synchronous manner that blocks the event loop until the spawned process either exits or is terminated. For convenience, the node:child_process module provides a handful of synchronous and asynchronous alternatives to child_process. spawn() and child_process.


1 Answers

The problem was that forked child processes were not closed correctly when parent was killed. This resulted in multiple ghost processes that caused the channel closed error.

I hooked into the SIGHUP and killed them gracefully. Now everything works.

like image 76
UpCat Avatar answered Oct 27 '22 09:10

UpCat