mirror of
https://github.com/nodejs/node.git
synced 2026-11-09 15:11:11 -05:00
net: unref timer in parent sockets
`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: https://github.com/joyent/node/issues/9242 PR-URL: https://github.com/iojs/io.js/pull/891 Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
1 parent
b3aa876f08
commit
bada87bd66
3 files changed
+75
-9
No files matched your search
+4
-2
@@ -236,9 +236,11 @@ function TLSSocket(socket, options) {
|
||||
writable: false
|
||||
});
|
||||
|
||||
// To prevent assertion in afterConnect()
|
||||
if (socket)
|
||||
if (socket) {
|
||||
this._parent = socket;
|
||||
// To prevent assertion in afterConnect()
|
||||
this._connecting = socket._connecting;
|
||||
}
|
||||
|
||||
this._tlsOptions = options;
|
||||
this._secureEstablished = false;
|
||||
|
||||
+16
-7
@@ -140,6 +140,7 @@ function Socket(options) {
|
||||
this._connecting = false;
|
||||
this._hadError = false;
|
||||
this._handle = null;
|
||||
this._parent = null;
|
||||
this._host = null;
|
||||
|
||||
if (util.isNumber(options))
|
||||
@@ -199,6 +200,13 @@ function Socket(options) {
|
||||
}
|
||||
util.inherits(Socket, stream.Duplex);
|
||||
|
||||
|
||||
Socket.prototype._unrefTimer = function unrefTimer() {
|
||||
for (var s = this; s !== null; s = s._parent)
|
||||
timers._unrefActive(s);
|
||||
};
|
||||
|
||||
|
||||
// the user has called .end(), and all the bytes have been
|
||||
// sent out to the other side.
|
||||
// If allowHalfOpen is false, or if the readable side has
|
||||
@@ -464,7 +472,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) {
|
||||
@@ -509,7 +518,7 @@ function onread(nread, buffer) {
|
||||
var self = handle.owner;
|
||||
assert(handle === self._handle, 'handle != self._handle');
|
||||
|
||||
timers._unrefActive(self);
|
||||
self._unrefTimer();
|
||||
|
||||
debug('onread', nread);
|
||||
|
||||
@@ -641,7 +650,7 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) {
|
||||
this._pendingData = null;
|
||||
this._pendingEncoding = '';
|
||||
|
||||
timers._unrefActive(this);
|
||||
this._unrefTimer();
|
||||
|
||||
if (!this._handle) {
|
||||
this._destroy(new Error('This socket is closed.'), cb);
|
||||
@@ -769,7 +778,7 @@ function afterWrite(status, handle, req, err) {
|
||||
return;
|
||||
}
|
||||
|
||||
timers._unrefActive(self);
|
||||
self._unrefTimer();
|
||||
|
||||
if (self !== process.stderr && self !== process.stdout)
|
||||
debug('afterWrite call cb');
|
||||
@@ -872,7 +881,7 @@ Socket.prototype.connect = function(options, cb) {
|
||||
self.once('connect', cb);
|
||||
}
|
||||
|
||||
timers._unrefActive(this);
|
||||
this._unrefTimer();
|
||||
|
||||
self._connecting = true;
|
||||
self.writable = true;
|
||||
@@ -924,7 +933,7 @@ Socket.prototype.connect = function(options, cb) {
|
||||
self._destroy();
|
||||
});
|
||||
} else {
|
||||
timers._unrefActive(self);
|
||||
self._unrefTimer();
|
||||
connect(self,
|
||||
ip,
|
||||
port,
|
||||
@@ -969,7 +978,7 @@ function afterConnect(status, handle, req, readable, writable) {
|
||||
if (status == 0) {
|
||||
self.readable = readable;
|
||||
self.writable = writable;
|
||||
timers._unrefActive(self);
|
||||
self._unrefTimer();
|
||||
|
||||
self.emit('connect');
|
||||
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
// Copyright Joyent, Inc. and other Node contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
||||
// persons to whom the Software is furnished to do so, subject to the
|
||||
// following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included
|
||||
// in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
||||
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
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();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user