Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read property 'port' of null on cluster.js #42

Open
lagoasoft-lucasschmidt opened this issue May 4, 2013 · 10 comments

Comments

@lagoasoft-lucasschmidt
Copy link

Hey, I just started trying this out, and I had an error, described below. The thing is that I thought it had something to do with my code. But then I tried a simple application like the one below and still have this problem. The application works, but I just thought about posting here to see if anyone has any idea about this. Thank you!

var Cluster = require('cluster2');
var express = require('express');

var app = express();
app.get('/', function(req, res) {
res.send('hello');
});

var c = new Cluster({
port: 3000,
cluster: false
});

c.listen(function(cb) {
cb(app);
});

TypeError: Cannot read property 'port' of null
at Server. (cluster.js:569:30)
at Server.g (events.js:175:14)
at Server.EventEmitter.emit (events.js:117:20)
at net.js:1029:10
at process._tickCallback (node.js:415:13)

[email protected]
[email protected]
[email protected]

@jnaveeth
Copy link

I've also been seeing the same error on my console, although the application works properly.

@inexplicable
Copy link
Contributor

in fact, this is an issue caused by node's cluster module
https://github.com/joyent/node/blob/v0.8.23-release/lib/cluster.js
https://github.com/joyent/node/blob/v0.10.7-release/lib/cluster.js

compare the following:
// Send a listening message to the master
tcpSelf.once('listening', function() {
cluster.worker.state = 'listening';
sendInternalMessage(cluster.worker, {
cmd: 'listening',
address: address,
port: port,
addressType: addressType,
fd: fd
});
});

//0.10.7
tcpSelf.once('listening', function() {
cluster.worker.state = 'listening';
sendInternalMessage(cluster.worker, {
cmd: 'listening',
address: address,
port: tcpSelf.address().port || port,
addressType: addressType,
fd: fd
});
});

the reading of tcpSelf.address().port is causing the error, as the net.js uses process.nextTick to emit 'listening' event and address isn't made available yet.

in most cases, i don't see it blocking the application though, let us know if otherwise

@ijse
Copy link

ijse commented Sep 26, 2013

So, the solution is....

@lagoasoft-lucasschmidt
Copy link
Author

Well, @ijse, since I found another problem in cluster2 module, I decided to not use it. I am using cluster module from node.

@ijse
Copy link

ijse commented Sep 26, 2013

@lucastschmidt Thanks for your advice. What's the problem? I just found it suit my case except the bug. Do you have any other alternatives?

@inexplicable
Copy link
Contributor

i could try overwrite the Socket.prototype.address at best in net.js to return none null value to avoid the error message, but doesn't look safe at all. Also, looking at 0.11.x, they have eliminated such need: https://github.com/joyent/node/blob/v0.11.7-release/lib/cluster.js

obj.once('listening', function() {
cluster.worker.state = 'listening';
var address = obj.address();
message.act = 'listening';
message.port = address && address.port || port,
send(message);
});

@lagoasoft-lucasschmidt
Copy link
Author

@ijse I am not using cluster2 because of this bug #43

It was completely insane. Eventually I just used the vanilla http://nodejs.org/api/cluster.html

I didnt need anything special, so it was no problem for me.

Another thing you could take a look into it is https://github.com/Unitech/pm2

I dont know if this project is active, but good luck!

@ijse
Copy link

ijse commented Sep 27, 2013

@inexplicable
@lucastschmidt Thank you! I'll try pm2.

@adamziel
Copy link

adamziel commented Apr 2, 2014

In case anyone stumbles upon this issue, this monkey-patch at the beggining of worker file helps:

  cluster = require('cluster');


  $getServer = cluster._getServer;

  cluster._getServer = function(tcpSelf, address, port, addressType, fd, cb) {
    var $address;
    $address = tcpSelf.address;
    tcpSelf.address = function() {
      address = $address.apply(tcpSelf, arguments);
      if (!address) {
        address = {
          port: port
        };
      }
      return address;
    };
    return $getServer.apply($getServer, arguments);
  };

@aripalo
Copy link

aripalo commented May 11, 2014

Well this was annoying bug (in NodeJS cluster module that is, not this project)... couple hours wasted as I thought my code had a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants