Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
tls: nullify .ssl on handle close
This is an intermediate fix for an issue of accessing `TLSWrap` fields
after the parent handle was destroyed. While `close` listener cleans up
this field automatically, it can be done even earlier at the
`TLSWrap.close` call.

Proper fix is going to be submitted and landed after this one.

Fix: #5108
  • Loading branch information
indutny committed Feb 9, 2016
commit 809bb0a373e82f728f03fa792122128741b821ea
3 changes: 3 additions & 0 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ proxiedMethods.forEach(function(name) {
});

tls_wrap.TLSWrap.prototype.close = function closeProxy(cb) {
if (this.owner)
this.owner.ssl = null;

if (this._parentWrap && this._parentWrap._handle === this._parent) {
this._parentWrap.once('close', cb);
return this._parentWrap.destroy();
Expand Down
45 changes: 45 additions & 0 deletions test/parallel/test-tls-regr-gh-5108.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const common = require('../common');

if (!common.hasCrypto) {
console.log('1..0 # Skipped: node compiled without crypto.');
return;
}

const assert = require('assert');
const tls = require('tls');
const fs = require('fs');
const path = require('path');

const keyDir = path.join(common.fixturesDir, 'keys');
const key = fs.readFileSync(path.join(keyDir, 'agent1-key.pem'));
const cert = fs.readFileSync(path.join(keyDir, 'agent1-cert.pem'));

const server = tls.createServer({
key: key,
cert: cert
}, function(c) {
c.end();
}).listen(common.PORT, function() {
var client = tls.connect({
port: common.PORT,
rejectUnauthorized: false
}, common.mustCall(function() {
client.destroy();
if (!client._events.close)
client._events.close = [];
else if (!Array.isArray(client._events.close))
client._events.close = [ client._events.close ];

client._events.close.unshift(common.mustCall(function() {
setImmediate(function() {
// Make it crash on unpatched node.js
var fd = client.ssl && client.ssl.fd;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the intention is to fail the test here, why not assert it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because assert below will fail, and it won't crash.

assert(client.ssl === null);
assert(!fd);
});
}));
server.close();
}));
});