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
8 changes: 5 additions & 3 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,14 +574,16 @@ function onread(nread, buffer) {

debug('EOF');

// push a null to signal the end of data.
// Do it before `maybeDestroy` for correct order of events:
// `end` -> `close`
self.push(null);

if (self._readableState.length === 0) {
self.readable = false;
maybeDestroy(self);
}

// push a null to signal the end of data.
self.push(null);

// internal end event so that we know that the actual socket
// is no longer readable, and we can start the shutdown
// procedure. No need to wait for all the data to be consumed.
Expand Down
26 changes: 26 additions & 0 deletions test/parallel/test-net-end-close.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';
require('../common');
const assert = require('assert');
const net = require('net');

const uv = process.binding('uv');

const s = new net.Socket({
handle: {
readStart: function() {
process.nextTick(() => this.onread(uv.UV_EOF, null));
},
close: (cb) => process.nextTick(cb)
},
writable: false
});
s.resume();

const events = [];

s.on('end', () => events.push('end'));
s.on('close', () => events.push('close'));

process.on('exit', () => {
assert.deepStrictEqual(events, [ 'end', 'close' ]);
});
Copy link
Member

Choose a reason for hiding this comment

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

Would something like this work?

s.on('end', common.mustCall(() => {
  s.on('close', common.mustCall(() => {}));
}));

... so you can get rid of events and the exit listener.

Copy link
Member Author

Choose a reason for hiding this comment

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

On one hand - yes, on the other - if this test will ever error, the error message will be more informative.