From 14622276bcd0a7d7aeafed42b3c9f0bc584b0b5d Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Wed, 2 Oct 2024 20:19:48 +0200 Subject: [PATCH] Fix `unref()` reentry (#3) --- index.js | 4 ++-- test/unref.js | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 test/unref.js diff --git a/index.js b/index.js index 57d8261..d50336f 100644 --- a/index.js +++ b/index.js @@ -26,7 +26,7 @@ class Timer { this._expiry = now + this._list.ms this._list.push(this) } else { - if (this._refed === true) decRef() + if (this._refed === true) this.unref() this._list = null } // apply at the bottom to avoid re-entries... @@ -36,7 +36,7 @@ class Timer { _clear () { if (this._list === null) return this._list.clear(this) - if (this._refed === true) decRef() + if (this._refed === true) this.unref() this._list = null maybeUpdateTimer() diff --git a/test/unref.js b/test/unref.js new file mode 100644 index 0000000..b389b8a --- /dev/null +++ b/test/unref.js @@ -0,0 +1,15 @@ +const test = require('brittle') +const timers = require('..') + +test('unref and a timer stays alive', async function (t) { + t.plan(1) + + const unreffed = timers.setTimeout(run, 10) + + function run () { + unreffed.unref() + timers.setTimeout(function () { + t.pass('timer triggered') + }, 50) + } +})