Skip to content

Commit 0363cf4

Browse files
dnakamurajasnell
authored andcommitted
tls: Closing parent socket also closes the tls sock
PR-URL: nodejs/node-v0.x-archive#25642 Reviewed-By: jasnell - James M Snell <jasnell@gmail.com>
1 parent a24db43 commit 0363cf4

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

lib/_tls_wrap.js

+4
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ function onocspresponse(resp) {
231231
function TLSSocket(socket, options) {
232232
// Disallow wrapping TLSSocket in TLSSocket
233233
assert(!(socket instanceof TLSSocket));
234+
var self = this;
234235

235236
net.Socket.call(this, {
236237
handle: socket && socket._handle,
@@ -241,6 +242,9 @@ function TLSSocket(socket, options) {
241242

242243
if (socket) {
243244
this._parent = socket;
245+
socket._destroy = function(exception) {
246+
self._destroy(exception);
247+
};
244248
// To prevent assertion in afterConnect()
245249
this._connecting = socket._connecting;
246250
}

lib/net.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -468,12 +468,10 @@ Socket.prototype._destroy = function(exception, cb) {
468468
return;
469469
}
470470

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

478476
debug('close');
479477
if (this._handle) {
@@ -482,16 +480,20 @@ Socket.prototype._destroy = function(exception, cb) {
482480
var isException = exception ? true : false;
483481
this._handle.close(function() {
484482
debug('emit close');
485-
self.emit('close', isException);
483+
for (var s = self; s !== null; s = s._parent)
484+
s.emit('close', isException);
486485
});
487486
this._handle.onread = noop;
488-
this._handle = null;
487+
for (var s = this; s !== null; s = s._parent)
488+
s._handle = null;
489489
}
490490

491491
// we set destroyed to true before firing error callbacks in order
492492
// to make it re-entrance safe in case Socket.prototype.destroy()
493493
// is called within callbacks
494-
this.destroyed = true;
494+
for (var s = this; s !== null; s = s._parent)
495+
s.destroyed = true;
496+
495497
fireErrorCallbacks();
496498

497499
if (this.server) {
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
var tlsCloseCount = 0;
19+
var sockCloseCount = 0;
20+
21+
var server = tls.createServer(options, function(secureSock) {
22+
secureSock.on('error', function() {
23+
hasError = true;
24+
});
25+
secureSock.on('close', function() {
26+
tlsCloseCount++;
27+
});
28+
normalSock.on('close', function() {
29+
sockCloseCount++;
30+
});
31+
32+
normalSock.destroy();
33+
secureSock.write('Test!', function(err) {
34+
assert(err);
35+
});
36+
});
37+
38+
server.on('connection', function(sock) {
39+
normalSock = sock;
40+
});
41+
42+
server.listen(common.PORT, function() {
43+
var c = tls.connect(common.PORT, {rejectUnauthorized: false});
44+
c.on('error', function() {}); // ignore socket hangup error
45+
46+
c.on('end', function() {
47+
server.close();
48+
});
49+
50+
process.on('exit', function() {
51+
assert(hasError);
52+
assert.equal(tlsCloseCount, 1);
53+
assert.equal(sockCloseCount, 1);
54+
});
55+
});

0 commit comments

Comments
 (0)