Skip to content

Commit cd44ee1

Browse files
committed
net: fix timeout with null handle
This commit handles the case where _onTimeout is called with a null handle. Refs: nodejs#15791 Fixes: nodejs#16484 PR-URL: nodejs#16489 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent 96f7df8 commit cd44ee1

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

lib/net.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -332,13 +332,15 @@ Socket.prototype.setTimeout = function(msecs, callback) {
332332

333333

334334
Socket.prototype._onTimeout = function() {
335-
// `.prevWriteQueueSize` !== `.updateWriteQueueSize()` means there is
336-
// an active write in progress, so we suppress the timeout.
337-
const prevWriteQueueSize = this._handle.writeQueueSize;
338-
if (prevWriteQueueSize > 0 &&
339-
prevWriteQueueSize !== this._handle.updateWriteQueueSize()) {
340-
this._unrefTimer();
341-
return;
335+
if (this._handle) {
336+
// `.prevWriteQueueSize` !== `.updateWriteQueueSize()` means there is
337+
// an active write in progress, so we suppress the timeout.
338+
const prevWriteQueueSize = this._handle.writeQueueSize;
339+
if (prevWriteQueueSize > 0 &&
340+
prevWriteQueueSize !== this._handle.updateWriteQueueSize()) {
341+
this._unrefTimer();
342+
return;
343+
}
342344
}
343345
debug('_onTimeout');
344346
this.emit('timeout');
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const net = require('net');
5+
const assert = require('assert');
6+
7+
const socket = new net.Socket();
8+
socket.setTimeout(common.platformTimeout(50));
9+
10+
socket.on('timeout', common.mustCall(() => {
11+
assert.strictEqual(socket._handle, null);
12+
}));
13+
14+
socket.on('connect', common.mustNotCall());
15+
16+
// since the timeout is unrefed, the code will exit without this
17+
setTimeout(() => {}, common.platformTimeout(200));

0 commit comments

Comments
 (0)