Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit c4dd56b

Browse files
committed
tls: Closing parent socket also closes the tls sock
1 parent 6b489e6 commit c4dd56b

File tree

3 files changed

+58
-11
lines changed

3 files changed

+58
-11
lines changed

lib/_tls_wrap.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ function onocspresponse(resp) {
228228
function TLSSocket(socket, options) {
229229
// Disallow wrapping TLSSocket in TLSSocket
230230
assert(!(socket instanceof TLSSocket));
231-
231+
var self = this;
232232
net.Socket.call(this, {
233233
handle: socket && socket._handle,
234234
allowHalfOpen: socket && socket.allowHalfOpen,
@@ -238,6 +238,7 @@ function TLSSocket(socket, options) {
238238

239239
if (socket) {
240240
this._parent = socket;
241+
socket._destroy = function(exception){self._destroy(exception)};
241242
// To prevent assertion in afterConnect()
242243
this._connecting = socket._connecting;
243244
}

lib/net.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,8 @@ Socket.prototype._destroy = function(exception, cb) {
456456
if (cb) cb(exception);
457457
if (exception && !self._writableState.errorEmitted) {
458458
process.nextTick(function() {
459-
self.emit('error', exception);
459+
for(var s = self; s !== null; s = s._parent)
460+
s.emit('error', exception);
460461
});
461462
self._writableState.errorEmitted = true;
462463
}
@@ -468,12 +469,11 @@ Socket.prototype._destroy = function(exception, cb) {
468469
return;
469470
}
470471

471-
self._connecting = false;
472-
473-
this.readable = this.writable = false;
474-
475-
for (var s = this; s !== null; s = s._parent)
476-
timers.unenroll(s);
472+
for (var s = this; s !== null; s = s._parent){
473+
timers.unenroll(s);
474+
s._connecting = false;
475+
s.readable = s.writable = false;
476+
}
477477

478478
debug('close');
479479
if (this._handle) {
@@ -482,16 +482,22 @@ Socket.prototype._destroy = function(exception, cb) {
482482
var isException = exception ? true : false;
483483
this._handle.close(function() {
484484
debug('emit close');
485-
self.emit('close', isException);
485+
for(var s = self; s !== null; s = s._parent)
486+
s.emit('close', isException);
486487
});
487488
this._handle.onread = noop;
488-
this._handle = null;
489+
for(var s = this; s !== null; s = s._parent){
490+
s._handle = null;
491+
}
489492
}
490493

491494
// we set destroyed to true before firing error callbacks in order
492495
// to make it re-entrance safe in case Socket.prototype.destroy()
493496
// is called within callbacks
494-
this.destroyed = true;
497+
for(var s = this; s !== null; s = s._parent){
498+
s.destroyed = true;
499+
}
500+
495501
fireErrorCallbacks();
496502

497503
if (this.server) {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
if (!process.versions.openssl) {
2+
console.error('Skipping because node compiled without OpenSSL.');
3+
process.exit(0);
4+
}
5+
6+
var common = require('../common');
7+
var assert = require('assert');
8+
var tls = require('tls');
9+
var fs = require('fs');
10+
11+
var options = {
12+
key: fs.readFileSync(common.fixturesDir + '/keys/agent2-key.pem'),
13+
cert: fs.readFileSync(common.fixturesDir + '/keys/agent2-cert.pem')
14+
};
15+
16+
var normalSock = null;
17+
var hasError = false;
18+
19+
var server = tls.createServer(options, function(secureSock) {
20+
secureSock.on('error', function(){hasError = true});
21+
normalSock.destroy();
22+
setTimeout(function(){
23+
secureSock.write('Test!', function(err){ assert.ok(err);});
24+
}, 200);
25+
});
26+
27+
server.on('connection', function(sock){
28+
normalSock = sock;
29+
});
30+
31+
server.listen(common.PORT, function() {
32+
var c = tls.connect(common.PORT, {rejectUnauthorized: false});
33+
c.on('error',function(){}); // ignore client errors
34+
35+
c.on('end', function() {
36+
server.close();
37+
});
38+
39+
process.on('exit', function(){assert(hasError)});
40+
});

0 commit comments

Comments
 (0)