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

socket.server is not defined #5083

Closed
unusualbob opened this issue Feb 4, 2016 · 7 comments
Closed

socket.server is not defined #5083

unusualbob opened this issue Feb 4, 2016 · 7 comments
Labels
https Issues or PRs related to the https subsystem. net Issues and PRs related to the net subsystem.

Comments

@unusualbob
Copy link

Recently myself and @braydonf were upgrading a project from 0.10.28 to 4.x and found something our connection tracking logic broke and was causing our server to never shutdown. When investigating I found that the server would never emit the close event even if all sockets had been destroyed.

When looking at net.js I noticed that the server._emitCloseIfDrained won't be called if the connection is missing its server property:

https://github.com/nodejs/node/blob/v4.2.6/lib/net.js#L482

Socket.prototype._destroy = function(exception, cb) {
  ...
  if (this.server) {
    COUNTER_NET_SERVER_CONNECTION_CLOSE(this);
    debug('has server');
    this.server._connections--;
    if (this.server._emitCloseIfDrained) {
      this.server._emitCloseIfDrained();
    }
  }

I decided to check for this property when creating my server

server.on('connection', function(socket) {
  console.log(socket.server);
});

I found that in 0.10.x socket.server was defined, but in 4.x it was undefined. I tried to make a test case to replicate it but found I was unable to do so. Luckily @braydonf took another look at it and has managed to replicate it, and is currently tracking down exactly when breakage occurred and said he would follow up on this issue when found. Looks like so far he's narrowed it down to io.js 2.2 -> 2.3.

@mscdex mscdex added the net Issues and PRs related to the net subsystem. label Feb 4, 2016
@braydonf
Copy link

braydonf commented Feb 4, 2016

Okay so I've narrowed it further down and the behavior changed between the releases 2.3.1 and 2.3.2.

Here is code to reproduce:

var https = require('https');
var fs = require('fs');

var options = {
  key: fs.readFileSync('./server.key'),
  cert: fs.readFileSync('./server.crt')
};

var server = https.createServer(options, function(req, res) {
  res.writeHead(200);
  res.end('hello world\n');
}).listen(8111);


server.on('connection', function(connection) {
  console.log('connection.server', connection.server);
  // undefined in 2.3.2, and defined in 2.3.1
});

Looks like the behavior changed between commit 6c61ca5 and 9180140 .

@braydonf
Copy link

braydonf commented Feb 4, 2016

And specifically here: 9180140#diff-04c3a6bcf355f2e05e7700c1b253d475R377

@braydonf
Copy link

braydonf commented Feb 4, 2016

Should also be noted that the with "http" connection.server is defined, so this only affects "https".

@mscdex
Copy link
Contributor

mscdex commented Feb 5, 2016

/cc @indutny

@braydonf
Copy link

braydonf commented Feb 5, 2016

Okay @unusualbob and I have worked out a test case for this here: #5106

@evanlucas evanlucas added the https Issues or PRs related to the https subsystem. label Feb 9, 2016
@indutny
Copy link
Member

indutny commented Feb 16, 2016

Looking.

indutny added a commit to indutny/io.js that referenced this issue Feb 16, 2016
The role of `this.server` is now split between `this._server` and
`this.server`. Where the first one is used for counting active
connections of `net.Server`, and the latter one is just a public API for
users' consumption.

The reasoning for this is simple, `TLSSocket` instances wrap
`net.Socket` instances, thus both refer to the `net.Server` through the
`this.server` property. However, only one of them should be used for
`net.Server` connection count book-keeping, otherwise double-decrement
will happen on socket destruction.

Fix: nodejs#5083
@indutny
Copy link
Member

indutny commented Feb 16, 2016

Should be fixed by #5262, thank you!

rvagg pushed a commit that referenced this issue Feb 18, 2016
The role of `this.server` is now split between `this._server` and
`this.server`. Where the first one is used for counting active
connections of `net.Server`, and the latter one is just a public API for
users' consumption.

The reasoning for this is simple, `TLSSocket` instances wrap
`net.Socket` instances, thus both refer to the `net.Server` through the
`this.server` property. However, only one of them should be used for
`net.Server` connection count book-keeping, otherwise double-decrement
will happen on socket destruction.

Fix: #5083
PR-URL: #5262
Reviewed-By: James M Snell <[email protected]>
MylesBorins pushed a commit that referenced this issue Mar 1, 2016
The role of `this.server` is now split between `this._server` and
`this.server`. Where the first one is used for counting active
connections of `net.Server`, and the latter one is just a public API for
users' consumption.

The reasoning for this is simple, `TLSSocket` instances wrap
`net.Socket` instances, thus both refer to the `net.Server` through the
`this.server` property. However, only one of them should be used for
`net.Server` connection count book-keeping, otherwise double-decrement
will happen on socket destruction.

Fix: #5083
PR-URL: #5262
Reviewed-By: James M Snell <[email protected]>
MylesBorins pushed a commit that referenced this issue Mar 1, 2016
The role of `this.server` is now split between `this._server` and
`this.server`. Where the first one is used for counting active
connections of `net.Server`, and the latter one is just a public API for
users' consumption.

The reasoning for this is simple, `TLSSocket` instances wrap
`net.Socket` instances, thus both refer to the `net.Server` through the
`this.server` property. However, only one of them should be used for
`net.Server` connection count book-keeping, otherwise double-decrement
will happen on socket destruction.

Fix: #5083
PR-URL: #5262
Reviewed-By: James M Snell <[email protected]>
MylesBorins pushed a commit that referenced this issue Mar 2, 2016
The role of `this.server` is now split between `this._server` and
`this.server`. Where the first one is used for counting active
connections of `net.Server`, and the latter one is just a public API for
users' consumption.

The reasoning for this is simple, `TLSSocket` instances wrap
`net.Socket` instances, thus both refer to the `net.Server` through the
`this.server` property. However, only one of them should be used for
`net.Server` connection count book-keeping, otherwise double-decrement
will happen on socket destruction.

Fix: #5083
PR-URL: #5262
Reviewed-By: James M Snell <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
https Issues or PRs related to the https subsystem. net Issues and PRs related to the net subsystem.
Projects
None yet
Development

No branches or pull requests

5 participants