Skip to content

socket: don't emit premature 'close' #20745

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

Closed
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
5 changes: 1 addition & 4 deletions doc/api/net.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,7 @@ endpoint, depending on what it [`connect()`][`socket.connect()`] to.
added: v0.1.90
-->

* `hadError` {boolean} `true` if the socket had a transmission error.

Emitted once the socket is fully closed. The argument `hadError` is a boolean
which says if the socket was closed due to a transmission error.
Emitted once the socket is fully closed.

### Event: 'connect'
<!-- YAML
Expand Down
36 changes: 17 additions & 19 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,6 @@ function Socket(options) {

// Prevent the "no-half-open enforcer" from being inherited from `Duplex`.
options.allowHalfOpen = true;
// For backwards compat do not emit close on destroy.
options.emitClose = false;
stream.Duplex.call(this, options);

// Default to *not* allowing half open sockets.
Expand Down Expand Up @@ -583,33 +581,33 @@ Socket.prototype._destroy = function(exception, cb) {
clearTimeout(s[kTimeout]);
}

debug('close');
const onClosed = () => {
Copy link
Member

Choose a reason for hiding this comment

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

I didn't test but I think this still does not prevent the 'error' event from being emitted multiple times. Assume that socket.destroy(err) is called twice.

First call will wait for this callback to be called and emit 'error' and 'close' when that happens.
Second call will find _readableState.destroyed set to true but _writableState.errorEmitted is not set to true yet as that will happen when this callback if invoked, so another 'error' event will be emitted.

Copy link
Member Author

Choose a reason for hiding this comment

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

But isn't that true for any stream? If you call destroy twice with error it will emit error twice... I would say the problem is in the destroy impl then?

Copy link
Member

Choose a reason for hiding this comment

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

No, it's emitted only once

const { Writable } = require('stream');

const w = new Writable();

w.on('error', console.log);
w.destroy(new Error('Oops'));
w.destroy(new Error('Oops'));

Copy link
Member Author

@ronag ronag May 20, 2018

Choose a reason for hiding this comment

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

Twice like this...

const { Writable } = require('stream');

const w = new Writable({
    destroy (err, cb) {
        process.nextTick(() => cb(err))
    }
});

w.on('error', console.log);
w.destroy(new Error('Oops'));
w.destroy(new Error('Oops'));

it's only once if you call cb synchronously. Which doesn't seem like the usual case or we wouldn't have a callback?

Copy link
Member

Choose a reason for hiding this comment

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

The callback on destroy is not a public api. It is used only internally.

Copy link
Member Author

@ronag ronag May 20, 2018

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

You are right, sorry, I was referring to https://nodejs.org/api/stream.html#stream_writable_destroy_error.

Copy link
Member

Choose a reason for hiding this comment

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

(We should fix that error being emitted more than once in streams imo.)

cb(exception);

if (this._server) {
COUNTER_NET_SERVER_CONNECTION_CLOSE(this);
debug('has server');
this._server._connections--;
if (this._server._emitCloseIfDrained) {
this._server._emitCloseIfDrained();
}
}
};

if (this._handle) {
debug('close');
if (this !== process.stderr)
debug('close handle');
var isException = exception ? true : false;
// `bytesRead` and `kBytesWritten` should be accessible after `.destroy()`
this[kBytesRead] = this._handle.bytesRead;
this[kBytesWritten] = this._handle.bytesWritten;

this._handle.close(() => {
debug('emit close');
this.emit('close', isException);
});
this._handle.close(onClosed);
this._handle.onread = noop;
this._handle = null;
this._sockname = null;
}

cb(exception);

if (this._server) {
COUNTER_NET_SERVER_CONNECTION_CLOSE(this);
debug('has server');
this._server._connections--;
if (this._server._emitCloseIfDrained) {
this._server._emitCloseIfDrained();
}
} else {
onClosed();
}
};

Expand Down