Skip to content

Commit 9b6b055

Browse files
committed
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: nodejs/node-v0.x-archive#9242 PR-URL: #891 Reviewed-By: Colin Ihrig <[email protected]>
1 parent ecef871 commit 9b6b055

File tree

3 files changed

+52
-9
lines changed

3 files changed

+52
-9
lines changed

‎lib/_tls_wrap.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,12 @@ function TLSSocket(socket, options){
213213
readable: false,
214214
writable: false
215215
});
216+
if(socket){
217+
this._parent=socket;
216218

217-
// To prevent assertion in afterConnect()
218-
if(socket)
219+
// To prevent assertion in afterConnect()
219220
this._connecting=socket._connecting;
221+
}
220222

221223
this._tlsOptions=options;
222224
this._secureEstablished=false;

‎lib/net.js‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ function Socket(options){
120120
this._connecting=false;
121121
this._hadError=false;
122122
this._handle=null;
123+
this._parent=null;
123124
this._host=null;
124125

125126
if(typeofoptions==='number')
@@ -179,6 +180,11 @@ function Socket(options){
179180
}
180181
util.inherits(Socket,stream.Duplex);
181182

183+
Socket.prototype._unrefTimer=functionunrefTimer(){
184+
for(vars=this;s!==null;s=s._parent)
185+
timers._unrefActive(s);
186+
};
187+
182188
// the user has called .end(), and all the bytes have been
183189
// sent out to the other side.
184190
// If allowHalfOpen is false, or if the readable side has
@@ -445,7 +451,8 @@ Socket.prototype._destroy = function(exception, cb){
445451

446452
this.readable=this.writable=false;
447453

448-
timers.unenroll(this);
454+
for(vars=this;s!==null;s=s._parent)
455+
timers.unenroll(s);
449456

450457
debug('close');
451458
if(this._handle){
@@ -490,7 +497,7 @@ function onread(nread, buffer){
490497
varself=handle.owner;
491498
assert(handle===self._handle,'handle != self._handle');
492499

493-
timers._unrefActive(self);
500+
self._unrefTimer();
494501

495502
debug('onread',nread);
496503

@@ -621,7 +628,7 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb){
621628
this._pendingData=null;
622629
this._pendingEncoding='';
623630

624-
timers._unrefActive(this);
631+
this._unrefTimer();
625632

626633
if(!this._handle){
627634
this._destroy(newError('This socket is closed.'),cb);
@@ -749,7 +756,7 @@ function afterWrite(status, handle, req, err){
749756
return;
750757
}
751758

752-
timers._unrefActive(self);
759+
self._unrefTimer();
753760

754761
if(self!==process.stderr&&self!==process.stdout)
755762
debug('afterWrite call cb');
@@ -864,7 +871,7 @@ Socket.prototype.connect = function(options, cb){
864871
self.once('connect',cb);
865872
}
866873

867-
timers._unrefActive(this);
874+
this._unrefTimer();
868875

869876
self._connecting=true;
870877
self.writable=true;
@@ -919,7 +926,7 @@ Socket.prototype.connect = function(options, cb){
919926
self._destroy();
920927
});
921928
}else{
922-
timers._unrefActive(self);
929+
self._unrefTimer();
923930
connect(self,
924931
ip,
925932
port,
@@ -964,7 +971,7 @@ function afterConnect(status, handle, req, readable, writable){
964971
if(status==0){
965972
self.readable=readable;
966973
self.writable=writable;
967-
timers._unrefActive(self);
974+
self._unrefTimer();
968975

969976
self.emit('connect');
970977

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
if(!process.versions.openssl)process.exit();
2+
3+
varcommon=require('../common');
4+
varassert=require('assert');
5+
varnet=require('net');
6+
vartls=require('tls');
7+
varfs=require('fs');
8+
9+
varoptions={
10+
key: fs.readFileSync(common.fixturesDir+'/keys/agent1-key.pem'),
11+
cert: fs.readFileSync(common.fixturesDir+'/keys/agent1-cert.pem')
12+
};
13+
14+
varserver=tls.createServer(options,function(c){
15+
setTimeout(function(){
16+
c.write('hello');
17+
setTimeout(function(){
18+
c.destroy();
19+
server.close();
20+
},75);
21+
},75);
22+
});
23+
24+
server.listen(common.PORT,function(){
25+
varsocket=net.connect(common.PORT,function(){
26+
socket.setTimeout(120,assert.fail);
27+
28+
vartsocket=tls.connect({
29+
socket: socket,
30+
rejectUnauthorized: false
31+
});
32+
tsocket.resume();
33+
});
34+
});

0 commit comments

Comments
(0)