brianc/node-forky

Error: channel closed

Opened this issue · 0 comments

Hey, I'm not sure, but I think there's a little bug in the disconnect function.

When a client repeatedly hits a route that throws an error (that results in disconnecting the worker), I get the following error:

Error: channel closed
18:09:12 web.1  |     at process.target.send (child_process.js:406:26)
18:09:12 web.1  |     at Worker.send (cluster.js:406:21)
18:09:12 web.1  |     at Function.forky.disconnect (./node_modules/forky/index.js:77:12)

When the client hits the route the first time, the worker will get disconnected but the connection to the client remains. If the client hits the route again the worker will try to send another disconnect message to the master. Since the channel is closed, the above error will occur.

In the disconnect function the following lines (I guess) should prevent this behavior:

if(worker.state == 'disconnecting') return;
  worker.state = 'disconnecting';

The worker however will set its state to disconnected (not disconnecting), which will lead the worker to send another message.

Correct me If I'm wrong!?

Maybe the above lines should be changed to:

if(worker.state == 'disconnecting' || worker.state == 'disconnected') return;
  worker.state = 'disconnecting';

What do you think?

Best regards!