Skip to content

Commit 9d73910

Browse files
committed
net: inline and simplify onSocketEnd
PR-URL: nodejs#18607 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent dfe0bc1 commit 9d73910

2 files changed

Lines changed: 13 additions & 31 deletions

File tree

lib/net.js

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ function Socket(options) {
236236
}
237237

238238
// shut down the socket when we're finished with it.
239-
this.on('_socketEnd', onSocketEnd);
239+
this.on('end', onReadableStreamEnd);
240240

241241
initSocketHandle(this);
242242

@@ -337,32 +337,6 @@ function afterShutdown(status, handle, req) {
337337
}
338338
}
339339

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

510484

485+
// Called when the 'end' event is emitted.
486+
function onReadableStreamEnd() {
487+
maybeDestroy(this);
488+
}
489+
490+
511491
// Call whenever we set writable=false or readable=false
512492
function maybeDestroy(socket) {
513493
if (!socket.readable &&
@@ -621,10 +601,11 @@ function onread(nread, buffer) {
621601
// Do it before `maybeDestroy` for correct order of events:
622602
// `end` -> `close`
623603
self.push(null);
604+
self.read(0);
624605

625-
if (self.readableLength === 0) {
626-
self.readable = false;
627-
maybeDestroy(self);
606+
if (!self.allowHalfOpen) {
607+
self.write = writeAfterFIN;
608+
self.destroySoon();
628609
}
629610

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

test/parallel/test-http-connect.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ server.listen(0, common.mustCall(() => {
6565

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

7071
assert.strictEqual(socket.listeners('free').length, 0);
7172
assert.strictEqual(socket.listeners('close').length, 0);

0 commit comments

Comments
 (0)