Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit a846d93

Browse files
committed
net: use timers._unrefActive for internal timeouts
1 parent f46ad01 commit a846d93

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

lib/net.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ Socket.prototype.listen = function() {
307307
Socket.prototype.setTimeout = function(msecs, callback) {
308308
if (msecs > 0 && !isNaN(msecs) && isFinite(msecs)) {
309309
timers.enroll(this, msecs);
310-
timers.active(this);
310+
timers._unrefActive(this);
311311
if (callback) {
312312
this.once('timeout', callback);
313313
}
@@ -481,7 +481,7 @@ function onread(buffer, offset, length) {
481481
var self = handle.owner;
482482
assert(handle === self._handle, 'handle != self._handle');
483483

484-
timers.active(self);
484+
timers._unrefActive(self);
485485

486486
var end = offset + length;
487487
debug('onread', process._errno, offset, length, end);
@@ -612,7 +612,7 @@ Socket.prototype._write = function(data, encoding, cb) {
612612
this._pendingData = null;
613613
this._pendingEncoding = '';
614614

615-
timers.active(this);
615+
timers._unrefActive(this);
616616

617617
if (!this._handle) {
618618
this._destroy(new Error('This socket is closed.'), cb);
@@ -702,7 +702,7 @@ function afterWrite(status, handle, req) {
702702
return;
703703
}
704704

705-
timers.active(self);
705+
timers._unrefActive(self);
706706

707707
if (self !== process.stderr && self !== process.stdout)
708708
debug('afterWrite call cb');
@@ -783,7 +783,7 @@ Socket.prototype.connect = function(options, cb) {
783783
self.once('connect', cb);
784784
}
785785

786-
timers.active(this);
786+
timers._unrefActive(this);
787787

788788
self._connecting = true;
789789
self.writable = true;
@@ -814,7 +814,7 @@ Socket.prototype.connect = function(options, cb) {
814814
self._destroy();
815815
});
816816
} else {
817-
timers.active(self);
817+
timers._unrefActive(self);
818818

819819
addressType = addressType || 4;
820820

@@ -861,7 +861,7 @@ function afterConnect(status, handle, req, readable, writable) {
861861
if (status == 0) {
862862
self.readable = readable;
863863
self.writable = writable;
864-
timers.active(self);
864+
timers._unrefActive(self);
865865

866866
self.emit('connect');
867867

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var net = require('net');
25+
26+
var server = net.createServer(function (c) {
27+
c.write('hello');
28+
c.unref();
29+
});
30+
server.listen(common.PORT);
31+
server.unref();
32+
33+
var timedout = false;
34+
35+
[8, 5, 3, 6, 2, 4].forEach(function (T) {
36+
var socket = net.createConnection(common.PORT, 'localhost');
37+
socket.setTimeout(T * 1000, function () {
38+
console.log(process._getActiveHandles());
39+
timedout = true;
40+
socket.destroy();
41+
});
42+
socket.unref();
43+
});
44+
45+
process.on('exit', function () {
46+
assert.strictEqual(timedout, false, 'Socket timeout should not hold loop open');
47+
});

0 commit comments

Comments
 (0)