GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — net: handle undefined parent in _unrefTimer and _destroy · nodejs/node@05cc430

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib
    • net.js
  • test/parallel
    • test-net-socket-unref-timer-parent-chain.js
    • test-net-unref-timer-parent-undefined.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/net.js‎

Copy file name to clipboardExpand all lines: lib/net.js
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,9 @@ ObjectSetPrototypeOf(Socket, stream.Duplex);
736736

737737
// Refresh existing timeouts.
738738
Socket.prototype._unrefTimer = function _unrefTimer() {
739-
for (let s = this; s !== null; s = s._parent) {
739+
// `_parent` may be null; we use a loose `!= null` check in case external
740+
// code sets it to undefined.
741+
for (let s = this; s != null; s = s._parent) {
740742
if (s[kTimeout])
741743
s[kTimeout].refresh();
742744
}
@@ -1097,7 +1099,9 @@ Socket.prototype._destroy = function(exception, cb) {
10971099

10981100
this.connecting = false;
10991101

1100-
for (let s = this; s !== null; s = s._parent) {
1102+
// `_parent` may be null; we use a loose `!= null` check in case external
1103+
// code sets it to undefined.
1104+
for (let s = this; s != null; s = s._parent) {
11011105
clearTimeout(s[kTimeout]);
11021106
}
11031107

Collapse file

‎test/parallel/test-net-socket-unref-timer-parent-chain.js‎

Copy file name to clipboard
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
// Walking the `_parent` chain must stop on a nullish link, not only strict
5+
// `null`. During connection teardown a socket's `_parent` can be left
6+
// `undefined`, which previously caused `_unrefTimer()` and `_destroy()` to read
7+
// a property off `undefined` and throw.
8+
// Refs: https://github.com/nodejs/node/issues/64490
9+
10+
const assert = require('assert');
11+
const net = require('net');
12+
13+
{
14+
const socket = new net.Socket();
15+
socket._parent = undefined;
16+
socket._unrefTimer();
17+
}
18+
19+
{
20+
const socket = new net.Socket();
21+
socket._parent = undefined;
22+
socket.on('error', common.mustNotCall());
23+
socket.destroy();
24+
assert.strictEqual(socket.destroyed, true);
25+
}
Collapse file

‎test/parallel/test-net-unref-timer-parent-undefined.js‎

Copy file name to clipboard
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
if (!common.hasCrypto)
6+
common.skip('missing crypto');
7+
8+
const tls = require('tls');
9+
const fixtures = require('../common/fixtures');
10+
11+
// A TLS socket whose `_parent` is left `undefined` during teardown must not
12+
// crash when reads land on it (`onStreamRead` -> `_unrefTimer`) or when it is
13+
// destroyed (`_destroy`). Both walk the `_parent` chain.
14+
// Refs: https://github.com/nodejs/node/issues/64490
15+
16+
const options = {
17+
key: fixtures.readKey('agent1-key.pem'),
18+
cert: fixtures.readKey('agent1-cert.pem'),
19+
};
20+
21+
const server = tls.createServer(options, common.mustCall((conn) => {
22+
setTimeout(() => conn.write('x'), 50);
23+
}));
24+
25+
server.listen(0, common.mustCall(() => {
26+
const client = tls.connect({
27+
port: server.address().port,
28+
rejectUnauthorized: false,
29+
}, common.mustCall(() => {
30+
client._parent = undefined;
31+
}));
32+
33+
client.on('data', common.mustCall(() => {
34+
server.close();
35+
client.destroy();
36+
}));
37+
38+
client.on('error', common.mustNotCall());
39+
}));

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.