Skip to content

Commit bf02780

Browse files
apapirovskicjihrig
authored andcommitted
net: fix timeout with null handle
This commit handles the case where _onTimeout is called with a null handle. Refs: #15791 Fixes: #16484 PR-URL: #16489 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
1 parent d66c927 commit bf02780

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
@@ -396,13 +396,15 @@ Socket.prototype.setTimeout = function(msecs, callback) {
396396

397397

398398
Socket.prototype._onTimeout = function() {
399-
// `.prevWriteQueueSize` !== `.updateWriteQueueSize()` means there is
400-
// an active write in progress, so we suppress the timeout.
401-
const prevWriteQueueSize = this._handle.writeQueueSize;
402-
if (prevWriteQueueSize > 0 &&
403-
prevWriteQueueSize !== this._handle.updateWriteQueueSize()) {
404-
this._unrefTimer();
405-
return;
399+
if (this._handle) {
400+
// `.prevWriteQueueSize` !== `.updateWriteQueueSize()` means there is
401+
// an active write in progress, so we suppress the timeout.
402+
const prevWriteQueueSize = this._handle.writeQueueSize;
403+
if (prevWriteQueueSize > 0 &&
404+
prevWriteQueueSize !== this._handle.updateWriteQueueSize()) {
405+
this._unrefTimer();
406+
return;
407+
}
406408
}
407409
debug('_onTimeout');
408410
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)