diff --git a/lib/timers.js b/lib/timers.js index 50ae477d59d30f..00b0773cfa6fb2 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -40,7 +40,6 @@ exports.active = function(item) { list = lists[msecs]; } else { list = new Timer(); - list.start(msecs, 0); L.init(list); @@ -48,6 +47,10 @@ exports.active = function(item) { list.msecs = msecs; list[kOnTimeout] = listOnTimeout; } + // Call start regardless whether list is new + // or not to prevent incorrect active_handles + // count. See https://github.com/nodejs/node-v0.x-archive/issues/25831. + list.start(msecs, 0); L.append(list, item); assert(!L.isEmpty(list)); // list is not empty diff --git a/test/addons/timers-active-handles/binding.cc b/test/addons/timers-active-handles/binding.cc new file mode 100644 index 00000000000000..9e32eaecd9a5c0 --- /dev/null +++ b/test/addons/timers-active-handles/binding.cc @@ -0,0 +1,17 @@ +#include +#include + +using namespace v8; + +void Method(const FunctionCallbackInfo& args) { + Isolate* isolate = args.GetIsolate(); + int hCnt = uv_default_loop()->active_handles; + uv_run(uv_default_loop(), UV_RUN_ONCE); + args.GetReturnValue().Set(Integer::New(isolate, hCnt)); +} + +void init(Handle exports) { + NODE_SET_METHOD(exports, "run", Method); +} + +NODE_MODULE(deasync, init) diff --git a/test/addons/timers-active-handles/binding.gyp b/test/addons/timers-active-handles/binding.gyp new file mode 100644 index 00000000000000..3bfb84493f3e87 --- /dev/null +++ b/test/addons/timers-active-handles/binding.gyp @@ -0,0 +1,8 @@ +{ + 'targets': [ + { + 'target_name': 'binding', + 'sources': [ 'binding.cc' ] + } + ] +} diff --git a/test/addons/timers-active-handles/test.js b/test/addons/timers-active-handles/test.js new file mode 100644 index 00000000000000..8ecf0484f136c3 --- /dev/null +++ b/test/addons/timers-active-handles/test.js @@ -0,0 +1,13 @@ +'use strict'; + +const assert = require('assert'); +var uvRunOnce = require('./build/Release/binding'); +setTimeout(function () { + var res; + setTimeout(function () { + res = true; + }, 2); + while (!res && uvRunOnce.run()) { + } + assert.equal(res, true); +}, 2); \ No newline at end of file