From f2f30286bf5e5332b1e788f3c745c5231e1b098b Mon Sep 17 00:00:00 2001 From: Yoshihiro Kikuchi Date: Sun, 18 Dec 2011 09:22:22 +0900 Subject: [PATCH] timers: fix memory leak in setTimeout Closing handle is leaked when setTimeout called with arguments which are 1. a callback 2. zero delay (i.e. setTimeout(function(){}, 0); ) --- lib/timers.js | 5 ++++- test/simple/test-timers-zero-timeout.js | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/timers.js b/lib/timers.js index 39eb4d9161..52f357f57d 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -151,7 +151,10 @@ exports.setTimeout = function(callback, after) { timer = new Timer(); if (arguments.length <= 2) { - timer._onTimeout = callback; + timer._onTimeout = function() { + callback(); + timer.close(); + } } else { var args = Array.prototype.slice.call(arguments, 2); timer._onTimeout = function() { diff --git a/test/simple/test-timers-zero-timeout.js b/test/simple/test-timers-zero-timeout.js index 79ecd9d267..d5e6df0786 100644 --- a/test/simple/test-timers-zero-timeout.js +++ b/test/simple/test-timers-zero-timeout.js @@ -27,6 +27,7 @@ var assert = require('assert'); var ncalled = 0; setTimeout(f, 0, 'foo', 'bar', 'baz'); + var timer = setTimeout(function(){}, 0); function f(a, b, c) { assert.equal(a, 'foo'); @@ -37,6 +38,8 @@ var assert = require('assert'); process.on('exit', function() { assert.equal(ncalled, 1); + // timer should be already closed + assert.equal(timer.close(), -1); }); })();