node icon indicating copy to clipboard operation
node copied to clipboard

child_process fork creates error: Error: listen EADDRINUSE

Open TitaneBoy opened this issue 10 years ago • 4 comments

  • Node Version: 6.1.0:
  • Platform: Windows 7, 64-bit:
  • Subsystem:Child Process:

Creating fork from child_process creates an error. This is what I get:

Error: listen EADDRINUSE :::4925 at Object.exports._errnoException (util.js:949:11) at exports._exceptionWithHostPort (util.js:972:20) at Agent.Server._listen2 (net.js:1253:14) at listen (net.js:1289:10) at Agent.Server.listen (net.js:1385:5) at Object.start (_debug_agent.js:21:9) at startup (node.js:86:44) at node.js:445:3

This is my code:

var fork = require('child_process').fork;
var child = fork(__dirname + '/my_child_file.js');

From there, the error mentioned above occurs

Thank you for helping me to resolve that.

TitaneBoy avatar May 20 '16 18:05 TitaneBoy

It looks like you're forking a child process that is trying to listen on a port that something else is already listening on.

cjihrig avatar May 20 '16 18:05 cjihrig

And What I can do to resolve that ? As I can see in the node js documentation, it should be simple to create child process fork , as simple as the two lines of code I gave in my first comment. As I can see, there is no needs to specify some port.

Is it a bug from node.js or Am I doing something wrong ?

Thank you.

TitaneBoy avatar May 20 '16 18:05 TitaneBoy

@TitaneBoy you cannot have two processes listening to the same port. Inside of my_child_file you will likely want to use an argument to pass the port.

Closing this as it is not a bug in node... @TitaneBoy please post in http://github.com/nodejs/help if you would like more help with this problem. Feel free to @ me and I'll help you out there

Here is a gist of what I am talking about https://gist.github.com/TheAlphaNerd/d2a82b014d3cfe9b871bb401d11fccd4

MylesBorins avatar May 20 '16 19:05 MylesBorins

Well... it's a node issue, any child/forked node instance will fail with Error: listen EADDRINUSE if you launch it from debugger. Sample code below:

console.log('fork'!=process.argv[2] ? 'parent started' : 'forked started');
if ('fork'!=process.argv[2])
{
    var child = require('child_process').fork(__filename, ['fork'], {silent: false});
    setTimeout(function() { console.log('ending parent process'); }, 2000);
}
else
    setTimeout(function() { console.log('ending forked process'); }, 1000);

if you run it as node test.js then everything is OK, if you run it as node debug test.js then it will fail with Error: listen EADDRINUSE :::5858. IMO, forked nodes should be aware of debugging connection an optionally attach to running debugger.

pps83 avatar Sep 12 '16 03:09 pps83