Skip to content

Commit efefdd6

Browse files
committed
net: autoDestroy Socket
Refactors net.Socket into using autoDestroy functionality of streams. PR-URL: #31806 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 882b61a commit efefdd6

5 files changed

+22
-38
lines changed

lib/net.js

+8-28
Original file line numberDiff line numberDiff line change
@@ -285,20 +285,15 @@ function Socket(options) {
285285
else
286286
options = { ...options };
287287

288-
const { allowHalfOpen } = options;
289-
290-
// Prevent the "no-half-open enforcer" from being inherited from `Duplex`.
291-
options.allowHalfOpen = true;
288+
// Default to *not* allowing half open sockets.
289+
options.allowHalfOpen = Boolean(options.allowHalfOpen);
292290
// For backwards compat do not emit close on destroy.
293291
options.emitClose = false;
294-
options.autoDestroy = false;
292+
options.autoDestroy = true;
295293
// Handle strings directly.
296294
options.decodeStrings = false;
297295
stream.Duplex.call(this, options);
298296

299-
// Default to *not* allowing half open sockets.
300-
this.allowHalfOpen = Boolean(allowHalfOpen);
301-
302297
if (options.handle) {
303298
this._handle = options.handle; // private
304299
this[async_id_symbol] = getNewAsyncId(this._handle);
@@ -416,28 +411,18 @@ Socket.prototype._final = function(cb) {
416411
const err = this._handle.shutdown(req);
417412

418413
if (err === 1 || err === UV_ENOTCONN) // synchronous finish
419-
return afterShutdown.call(req, 0);
414+
return cb();
420415
else if (err !== 0)
421-
return this.destroy(errnoException(err, 'shutdown'));
416+
return cb(errnoException(err, 'shutdown'));
422417
};
423418

424-
425-
function afterShutdown(status) {
419+
function afterShutdown() {
426420
const self = this.handle[owner_symbol];
427421

428422
debug('afterShutdown destroyed=%j', self.destroyed,
429423
self._readableState);
430424

431425
this.callback();
432-
433-
// Callback may come after call to destroy.
434-
if (self.destroyed)
435-
return;
436-
437-
if (!self.readable || self.readableEnded) {
438-
debug('readableState ended, destroying');
439-
self.destroy();
440-
}
441426
}
442427

443428
// Provide a better error message when we call end() as a result
@@ -452,10 +437,10 @@ function writeAfterFIN(chunk, encoding, cb) {
452437
// eslint-disable-next-line no-restricted-syntax
453438
const er = new Error('This socket has been ended by the other party');
454439
er.code = 'EPIPE';
455-
process.nextTick(emitErrorNT, this, er);
456440
if (typeof cb === 'function') {
457441
defaultTriggerAsyncIdScope(this[async_id_symbol], process.nextTick, cb, er);
458442
}
443+
this.destroy(er);
459444

460445
return false;
461446
}
@@ -628,12 +613,7 @@ Socket.prototype.read = function(n) {
628613
function onReadableStreamEnd() {
629614
if (!this.allowHalfOpen) {
630615
this.write = writeAfterFIN;
631-
if (this.writable)
632-
this.end();
633-
else if (!this.writableLength)
634-
this.destroy();
635-
} else if (!this.destroyed && !this.writable && !this.writableLength)
636-
this.destroy();
616+
}
637617
}
638618

639619

test/parallel/test-http2-max-invalid-frames.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ server.on('stream', (stream) => {
2323

2424
server.listen(0, () => {
2525
const h2header = Buffer.alloc(9);
26-
const conn = net.connect(server.address().port);
26+
const conn = net.connect({
27+
port: server.address().port,
28+
allowHalfOpen: true
29+
});
2730

2831
conn.write('PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n');
2932

test/parallel/test-net-write-after-close.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
'use strict';
2323
const common = require('../common');
24+
const assert = require('assert');
2425

2526
const net = require('net');
2627

@@ -31,10 +32,7 @@ const server = net.createServer(common.mustCall(function(socket) {
3132

3233
socket.resume();
3334

34-
socket.on('error', common.mustCall(function(error) {
35-
console.error('received error as expected, closing server', error);
36-
server.close();
37-
}));
35+
socket.on('error', common.mustNotCall());
3836
}));
3937

4038
server.listen(0, function() {
@@ -44,7 +42,10 @@ server.listen(0, function() {
4442
// Then 'end' will be emitted when it receives a FIN packet from
4543
// the other side.
4644
client.on('end', common.mustCall(() => {
47-
serverSocket.write('test', common.mustCall());
45+
serverSocket.write('test', common.mustCall((err) => {
46+
assert(err);
47+
server.close();
48+
}));
4849
}));
4950
client.end();
5051
});

test/parallel/test-tls-getprotocol.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ const server = tls.createServer(serverConfig, common.mustCall(function() {
3434
secureProtocol: v.secureProtocol
3535
}, common.mustCall(function() {
3636
assert.strictEqual(this.getProtocol(), v.version);
37-
this.on('end', common.mustCall(function() {
37+
this.on('end', common.mustCall());
38+
this.on('close', common.mustCall(function() {
3839
assert.strictEqual(this.getProtocol(), null);
3940
})).end();
4041
if (++connected === clientConfigs.length)

test/parallel/test-tls-streamwrap-buffersize.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ const net = require('net');
5959
assert.strictEqual(client.bufferSize, i + 1);
6060
}
6161

62-
// It seems that tlsSockets created from sockets of `Duplex` emit no
63-
// "finish" events. We use "end" event instead.
64-
client.on('end', common.mustCall(() => {
62+
client.on('end', common.mustCall());
63+
client.on('close', common.mustCall(() => {
6564
assert.strictEqual(client.bufferSize, undefined);
6665
}));
6766

0 commit comments

Comments
 (0)