-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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
net: replace _writableState with writableFinished #27974
Conversation
/ping @mcollina |
This comment has been minimized.
This comment has been minimized.
Since this already had a few approvals I'm not going to block it but in my opinion this:
I see only disadvantages. |
Commit message title and body should be updated as they are misleading. There is no |
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.
LGTM
@lpinca I find myself agreeing more than disagreeing with your points. If you were trying to adjust this code in the spirit of #445, what would you recommend? (I can think of a few things, but opted for this as it didn't involve adding new stuff to the |
I would add a getter to
but yes I think #445 is a bit misguided because it means exposing most (if not all) attributes of I think |
Hey, @nodejs/streams, what do you think?
|
(Is |
lib/net.js
Outdated
@@ -551,13 +551,9 @@ function onReadableStreamEnd() { | |||
|
|||
|
|||
Socket.prototype.destroySoon = function() { | |||
stream.finished(this, { error: false, readable: false }, this.destroy); |
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.
Note sure if the callback can be called synchronously? Just to be safe... put it after the end()
call? i.e. keep the order as it was
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.
eos()
unconditionally adds one listener for the 'finish'
event and one for 'close'
event (and actually another for 'end'
event even if the readable
option is false
).
If _writableState.finished
is already true
then the 'finish'
event won't be emitted and it will wait for 'close'
. Even if this is emitted synchronously it is not a problem because it means the socket is already destroyed and no longer writable.
If _writableState.finished
is false
then the 'finish'
event won't be emitted until socket.end()
is called and if this already happened then the socket is no longer writable.
So the order should not matter. However I can see a case where the behavior is different.
'use strict';
const net = require('net');
const server = net.createServer(function(socket) {
socket.write('foo');
});
server.listen(function() {
const socket = net.connect(this.address().port, function() {
socket.on('finish', function() {
socket.destroySoon();
});
socket.on('close', function() {
console.log('close');
});
setTimeout(function() {
socket.end();
}, 100);
});
});
Yes, it's a fabricated example that probably will never happen in reality but with this patch the socket is not destroyed ('close'
is not emitted) until the buffered data is actually read. This is not the case with the current implementation of destroySoon()
.
Accessing
I'm +1 to that approach (#27974 (comment)).
I'm +1 to this PR as well. However I concur that
@nodejs/streams is likely the team to target for this question. |
I think if someone messes with the internal buffers it means they either know what they are doing or want to break things intentionally. This is the reason for "use at your own risk". Take this data for example https://github.com/search?p=1&q=_readableState&type=Code, just from the first results we see use of
Yes, exposing all of that via getters is an option, but it does not seem the right solution to me. |
Marking this |
Used the |
(@addaleax Sorry, I didn't meant to request a re-review from you since you had already re-reviewed, but I don't see a way to undo my request in the GitHub interface.) |
Landed in 3832713 |
Replace usage of quasi-private _writableState.finished with public writableFinished property. PR-URL: #27974 Refs: #445 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
Replace use of quasi-private streams _writableState property in net.js with
use of the public streams finished() method.
Refs: #445
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes