Skip to content

Commit

Permalink
Fix unref() reentry (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh authored Oct 2, 2024
1 parent 11e71b0 commit 1462227
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand All @@ -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()
Expand Down
15 changes: 15 additions & 0 deletions test/unref.js
Original file line number Diff line number Diff line change
@@ -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)
}
})

0 comments on commit 1462227

Please sign in to comment.