Skip to content

Commit 3c8bf90

Browse files
ronagtargos
authored andcommitted
net: wait for shutdown to complete before closing
When not allowing half open, handle.close would be invoked before shutdown has been called and completed causing a potential data race. Fixes: #32486 (comment) PR-URL: #32491 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 9ee2afa commit 3c8bf90

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

lib/internal/stream_base_commons.js

+10
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,16 @@ function onStreamRead(arrayBuffer) {
213213
if (stream[kMaybeDestroy])
214214
stream.on('end', stream[kMaybeDestroy]);
215215

216+
// TODO(ronag): Without this `readStop`, `onStreamRead`
217+
// will be called once more (i.e. after Readable.ended)
218+
// on Windows causing a ECONNRESET, failing the
219+
// test-https-truncate test.
220+
if (handle.readStop) {
221+
const err = handle.readStop();
222+
if (err)
223+
return stream.destroy(errnoException(err, 'read'));
224+
}
225+
216226
// Push a null to signal the end of data.
217227
// Do it before `maybeDestroy` for correct order of events:
218228
// `end` -> `close`

lib/net.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -631,9 +631,9 @@ function onReadableStreamEnd() {
631631
this.write = writeAfterFIN;
632632
if (this.writable)
633633
this.end();
634-
}
635-
636-
if (!this.destroyed && !this.writable && !this.writableLength)
634+
else if (!this.writableLength)
635+
this.destroy();
636+
} else if (!this.destroyed && !this.writable && !this.writableLength)
637637
this.destroy();
638638
}
639639

test/async-hooks/test-graph.tls-write.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ function onexit() {
6565
{ type: 'TCPCONNECTWRAP',
6666
id: 'tcpconnect:1', triggerAsyncId: 'tcp:1' },
6767
{ type: 'TCPWRAP', id: 'tcp:2', triggerAsyncId: 'tcpserver:1' },
68-
{ type: 'TLSWRAP', id: 'tls:2', triggerAsyncId: 'tcpserver:1' },
69-
{ type: 'Immediate', id: 'immediate:1', triggerAsyncId: 'tcp:2' },
70-
{ type: 'Immediate', id: 'immediate:2', triggerAsyncId: 'tcp:1' },
68+
{ type: 'TLSWRAP', id: 'tls:2', triggerAsyncId: 'tcpserver:1' }
7169
]
7270
);
7371
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const net = require('net');
6+
7+
const server = net.createServer(common.mustCall((socket) => {
8+
socket.end(Buffer.alloc(1024));
9+
})).listen(0, common.mustCall(() => {
10+
const socket = net.connect(server.address().port);
11+
assert.strictEqual(socket.allowHalfOpen, false);
12+
socket.resume();
13+
socket.on('end', common.mustCall(() => {
14+
process.nextTick(() => {
15+
// Ensure socket is not destroyed straight away
16+
// without proper shutdown.
17+
assert(!socket.destroyed);
18+
server.close();
19+
});
20+
}));
21+
socket.on('finish', common.mustCall(() => {
22+
assert(!socket.destroyed);
23+
}));
24+
socket.on('close', common.mustCall());
25+
}));

0 commit comments

Comments
 (0)