Skip to content

Commit ef4714c

Browse files
addaleaxMylesBorins
authored andcommitted
net: inline and simplify onSocketEnd
Backport-PR-URL: #19194 PR-URL: #18607 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 1fadb2e commit ef4714c

File tree

2 files changed

+13
-31
lines changed

2 files changed

+13
-31
lines changed

lib/net.js

+11-30
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ function Socket(options) {
245245

246246
// shut down the socket when we're finished with it.
247247
this.on('finish', onSocketFinish);
248-
this.on('_socketEnd', onSocketEnd);
248+
this.on('end', onReadableStreamEnd);
249249

250250
initSocketHandle(this);
251251

@@ -341,32 +341,6 @@ function afterShutdown(status, handle) {
341341
}
342342
}
343343

344-
// the EOF has been received, and no more bytes are coming.
345-
// if the writable side has ended already, then clean everything
346-
// up.
347-
function onSocketEnd() {
348-
// XXX Should not have to do as much in this function.
349-
// ended should already be true, since this is called *after*
350-
// the EOF errno and onread has eof'ed
351-
debug('onSocketEnd', this._readableState);
352-
this._readableState.ended = true;
353-
if (this._readableState.endEmitted) {
354-
this.readable = false;
355-
maybeDestroy(this);
356-
} else {
357-
this.once('end', function end() {
358-
this.readable = false;
359-
maybeDestroy(this);
360-
});
361-
this.read(0);
362-
}
363-
364-
if (!this.allowHalfOpen) {
365-
this.write = writeAfterFIN;
366-
this.destroySoon();
367-
}
368-
}
369-
370344
// Provide a better error message when we call end() as a result
371345
// of the other side sending a FIN. The standard 'write after end'
372346
// is overly vague, and makes it seem like the user's code is to blame.
@@ -512,6 +486,12 @@ Socket.prototype.end = function(data, encoding, callback) {
512486
};
513487

514488

489+
// Called when the 'end' event is emitted.
490+
function onReadableStreamEnd() {
491+
maybeDestroy(this);
492+
}
493+
494+
515495
// Call whenever we set writable=false or readable=false
516496
function maybeDestroy(socket) {
517497
if (!socket.readable &&
@@ -625,10 +605,11 @@ function onread(nread, buffer) {
625605
// Do it before `maybeDestroy` for correct order of events:
626606
// `end` -> `close`
627607
self.push(null);
608+
self.read(0);
628609

629-
if (self.readableLength === 0) {
630-
self.readable = false;
631-
maybeDestroy(self);
610+
if (!self.allowHalfOpen) {
611+
self.write = writeAfterFIN;
612+
self.destroySoon();
632613
}
633614

634615
// internal end event so that we know that the actual socket

test/parallel/test-http-connect.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ server.listen(0, common.mustCall(() => {
7171

7272
// the stream.Duplex onend listener
7373
// allow 0 here, so that i can run the same test on streams1 impl
74-
assert(socket.listeners('end').length <= 1);
74+
assert(socket.listenerCount('end') <= 2,
75+
`Found ${socket.listenerCount('end')} end listeners`);
7576

7677
assert.strictEqual(socket.listeners('free').length, 0);
7778
assert.strictEqual(socket.listeners('close').length, 0);

0 commit comments

Comments
 (0)