Node child process, channel closed on process.send
Asked Answered
E

2

5

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?

Emaemaciate answered 8/6, 2014 at 20:23 Comment(0)
E
8

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.

Emaemaciate answered 9/6, 2014 at 20:10 Comment(0)
F
0

You need to specify the event type, since with process.send(data) you're just sending an object.

Frobisher answered 8/6, 2014 at 20:36 Comment(5)
What type? According to nodejs.org/api/… there does not need to be a typeEmaemaciate
Yes, but in your code you're registering an event listener every time a message is received. I think you want to call a function there. So instead of someLib... just call the function directly like process.send(data). Or you can do something what is just below where you've pointed me.Frobisher
Sry you misunderstood me, someLib.on is called once, my code was just a short version. I edited my question.Emaemaciate
Ok, now it makes sense. I guess the problem is in the someLib somewhere.Frobisher
Must be, would like to find out.Emaemaciate

© 2022 - 2024 — McMap. All rights reserved.