diff --git a/lib/timers.js b/lib/timers.js index 668d5536c8186c..302a11e18b5624 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -301,6 +301,14 @@ const Timeout = function(after) { this._repeat = null; }; + +function unrefdHandle() { + this.owner._onTimeout(); + if (!this.owner.repeat) + this.owner.close(); +} + + Timeout.prototype.unref = function() { if (this._handle) { this._handle.unref(); @@ -315,7 +323,8 @@ Timeout.prototype.unref = function() { if (this._called && !this._repeat) return; this._handle = new Timer(); - this._handle[kOnTimeout] = this._onTimeout; + this._handle.owner = this; + this._handle[kOnTimeout] = unrefdHandle; this._handle.start(delay, 0); this._handle.domain = this.domain; this._handle.unref();