Skip to content

Commit ebf9f29

Browse files
committed
lib: fix guard expression in timer.unref()
Fixes the following assertion on slow systems, like our ARM buildbot: $ out/Debug/node test/simple/test-timers-unref.js node: ../src/async-wrap-inl.h:101: v8::Handle<v8::Value> node::AsyncWrap::MakeCallback(uint32_t, int, v8::Handle<v8::Value>*): Assertion `cb_v->IsFunction()' failed. Aborted The reason it only manifests on slow systems is that the test starts a 1 ms interval timer, then defers timer.unref.bind({}) to the next tick. On fast systems, the test completes in under a millisecond, before the callback is called. This commit makes timer.unref() check that the receiver actually has a timeout callback property. Fixes #13. PR-URL: #165 Reviewed-By: Rod Vagg <[email protected]>
1 parent b7d2478 commit ebf9f29

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

lib/timers.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,9 @@ var Timeout = function(after) {
287287
};
288288

289289
Timeout.prototype.unref = function() {
290-
if (!this._handle) {
290+
if (this._handle) {
291+
this._handle.unref();
292+
} else if (typeof(this._onTimeout) === 'function') {
291293
var now = Timer.now();
292294
if (!this._idleStart) this._idleStart = now;
293295
var delay = this._idleStart + this._idleTimeout - now;
@@ -298,8 +300,6 @@ Timeout.prototype.unref = function() {
298300
this._handle.start(delay, 0);
299301
this._handle.domain = this.domain;
300302
this._handle.unref();
301-
} else {
302-
this._handle.unref();
303303
}
304304
};
305305

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) 2014, StrongLoop Inc.
2+
//
3+
// Permission to use, copy, modify, and/or distribute this software for any
4+
// purpose with or without fee is hereby granted, provided that the above
5+
// copyright notice and this permission notice appear in all copies.
6+
//
7+
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8+
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10+
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14+
15+
var common = require('../common');
16+
17+
var Timer = process.binding('timer_wrap').Timer;
18+
Timer.now = function() { return ++Timer.now.ticks; };
19+
Timer.now.ticks = 0;
20+
21+
var t = setInterval(function() {}, 1);
22+
var o = { _idleStart: 0, _idleTimeout: 1 };
23+
t.unref.call(o);
24+
25+
setTimeout(clearInterval.bind(null, t), 2);

0 commit comments

Comments
 (0)