timers: fix unref() memory leak
authorTrevor Norris <trev.norris@gmail.com>
Wed, 26 Nov 2014 20:27:57 +0000 (12:27 -0800)
committerFedor Indutny <fedor@indutny.com>
Fri, 3 Apr 2015 23:29:57 +0000 (02:29 +0300)
The destructor isn't being called for timers that have been unref'd.

Fixes: https://github.com/joyent/node/issues/8364
PR-URL: https://github.com/iojs/io.js/pull/1330
Reviewed-By: Fedor Indutny <fedor@indutny.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
lib/timers.js

index 668d5536c8186cf3e73cf303c5c3f9d08bdf97cd..302a11e18b5624ddc5257efe3485d80bb76cec93 100644 (file)
@@ -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();