-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
stream_base,tls_wrap: notify on destruct #11947
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
if (!common.hasCrypto) { | ||
common.skip('missing crypto'); | ||
return; | ||
} | ||
const tls = require('tls'); | ||
const fs = require('fs'); | ||
const util = require('util'); | ||
|
||
const sent = 'hello world'; | ||
const serverOptions = { | ||
isServer: true, | ||
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'), | ||
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem') | ||
}; | ||
|
||
let ssl = null; | ||
|
||
process.on('exit', function() { | ||
assert.ok(ssl !== null); | ||
// If the internal pointer to stream_ isn't cleared properly then this | ||
// will abort. | ||
util.inspect(ssl); | ||
}); | ||
|
||
const server = tls.createServer(serverOptions, function(s) { | ||
s.on('data', function() { }); | ||
s.on('end', function() { | ||
server.close(); | ||
s.destroy(); | ||
}); | ||
}).listen(0, function() { | ||
const c = new tls.TLSSocket(); | ||
ssl = c.ssl; | ||
c.connect(this.address().port, function() { | ||
c.end(sent); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If TLSWrap is the only user of this, wouldn't it make more sense to set
stream_ = nullptr
in ~TLSWrap()?(Also, that inheritance chain... TLSWrap -> StreamWrap -> StreamBase -> StreamResource, with multiple inheritance, CRTP and ad hoc function pointers thrown in for good measure. Barf!)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bnoordhuis
settingstream_ = nullptr
in~TLSWrap()
actually isn't enough. And actually, just managed to create a new test that still segfaults. So I'm revisiting the patch.EDIT: The failure I mentioned just happened to occur under similar circumstances, but unrelated to this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bnoordhuis okay. so this took me far longer than I'd have liked but I now remember why I made the change this way to begin with.
TLSWrap::stream_
points to theStreamBase*
instance that's passed toTLSWrap::TLSWrap()
. Each of these pointers are assigned to their ownFunctionTemplate
instance as an internal aligned pointer, and the lifetime of each is independent from the other. Meaningstream_
may be deleted even though theTLSWrap
instance remains alive. In JS they're just attached as properties to each other.So if the
StreamBase
instance is closed and released thenstream_
will point to invalid memory, even though theTLSWrap
instance is still alive. Which means the necessary step is to zero out thestream_
field in theTLSWrap
instance, and also addstream != nullptr
checks to the native methods likeTLSWrap::IsAlive()
, so it is no longer pointing to an invalid memory address.To clarify (if that's possible)
TLSWrap
inherits fromStreamBase
and it holds a pointer to anotherStreamBase
instance.I'll update the commit message with all this information.
FYI all that class template multiple inheritance stuff going on is what lead me to this solution in c0e6c66:
In order to handle unwrapping in methods like
template<class Base, ...> StreamBase::JSMethod()
that need to convert avoid*
of a class instance in a template method to its parent class, that also has multiple inheritance.