Skip to content

Commit

Permalink
net: unref timer in parent sockets
Browse files Browse the repository at this point in the history
`TLSSocket` wraps the original `net.Socket`, but writes/reads to/from
`TLSSocket` do not touch the timers of original `net.Socket`.

Introduce `socket._parent` property, and iterate through all parents
to unref timers and prevent timeout event on original `net.Socket`.

Fix: nodejs/node-v0.x-archive#9242
  • Loading branch information
indutny committed Feb 19, 2015
1 parent ed240f4 commit 2f471f2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
2 changes: 2 additions & 0 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ function TLSSocket(socket, options) {
readable: false,
writable: false
});
if (socket)
this._parent = socket;

// To prevent assertion in afterConnect()
if (socket)
Expand Down
22 changes: 15 additions & 7 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ function Socket(options) {
this._connecting = false;
this._hadError = false;
this._handle = null;
this._parent = null;
this._host = null;

if (typeof options === 'number')
Expand Down Expand Up @@ -445,7 +446,8 @@ Socket.prototype._destroy = function(exception, cb) {

this.readable = this.writable = false;

timers.unenroll(this);
for (var s = this; s !== null; s = s._parent)
timers.unenroll(s);

debug('close');
if (this._handle) {
Expand Down Expand Up @@ -490,7 +492,8 @@ function onread(nread, buffer) {
var self = handle.owner;
assert(handle === self._handle, 'handle != self._handle');

timers._unrefActive(self);
for (var s = self; s !== null; s = s._parent)
timers._unrefActive(s);

debug('onread', nread);

Expand Down Expand Up @@ -621,7 +624,8 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) {
this._pendingData = null;
this._pendingEncoding = '';

timers._unrefActive(this);
for (var s = this; s !== null; s = s._parent)
timers._unrefActive(s);

if (!this._handle) {
this._destroy(new Error('This socket is closed.'), cb);
Expand Down Expand Up @@ -749,7 +753,8 @@ function afterWrite(status, handle, req, err) {
return;
}

timers._unrefActive(self);
for (var s = self; s !== null; s = s._parent)
timers._unrefActive(s);

if (self !== process.stderr && self !== process.stdout)
debug('afterWrite call cb');
Expand Down Expand Up @@ -864,7 +869,8 @@ Socket.prototype.connect = function(options, cb) {
self.once('connect', cb);
}

timers._unrefActive(this);
for (var s = this; s !== null; s = s._parent)
timers._unrefActive(s);

self._connecting = true;
self.writable = true;
Expand Down Expand Up @@ -919,7 +925,8 @@ Socket.prototype.connect = function(options, cb) {
self._destroy();
});
} else {
timers._unrefActive(self);
for (var s = self; s !== null; s = s._parent)
timers._unrefActive(s);
connect(self,
ip,
port,
Expand Down Expand Up @@ -964,7 +971,8 @@ function afterConnect(status, handle, req, readable, writable) {
if (status == 0) {
self.readable = readable;
self.writable = writable;
timers._unrefActive(self);
for (var s = self; s !== null; s = s._parent)
timers._unrefActive(s);

self.emit('connect');

Expand Down
34 changes: 34 additions & 0 deletions test/parallel/test-tls-wrap-timeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
if (!process.versions.openssl) process.exit();

var common = require('../common');
var assert = require('assert');
var net = require('net');
var tls = require('tls');
var fs = require('fs');

var options = {
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem')
};

var server = tls.createServer(options, function(c) {
setTimeout(function() {
c.write('hello');
setTimeout(function() {
c.destroy();
server.close();
}, 75);
}, 75);
});

server.listen(common.PORT, function() {
var socket = net.connect(common.PORT, function() {
socket.setTimeout(120, assert.fail);

var tsocket = tls.connect({
socket: socket,
rejectUnauthorized: false
});
tsocket.resume();
});
});

0 comments on commit 2f471f2

Please sign in to comment.