From 5f3464cf4ecb6a2db66486bdd53a3698d88f0d29 Mon Sep 17 00:00:00 2001 From: Michael W Date: Tue, 30 Nov 2010 22:20:25 -0700 Subject: [PATCH] fixed timers, whoops. Fixes issue https://github.com/ry/node/issues/issue/481 --- lib/timers.js | 3 +- test/simple/test-net-can-reset-timeout.js | 41 +++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 test/simple/test-net-can-reset-timeout.js diff --git a/lib/timers.js b/lib/timers.js index 5419c038704..272cad95f7c 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -43,7 +43,6 @@ function shift (list) { function remove (item) { item._idleNext._idlePrev = item._idlePrev; item._idlePrev._idleNext = item._idleNext; - item._idleNext = null; } @@ -209,7 +208,7 @@ exports.setTimeout = function (callback, after) { exports.clearTimeout = function (timer) { - if (timer) { + if (timer && (timer.callback || timer._onTimeout)) { timer.callback = timer._onTimeout = null; exports.unenroll(timer); if (timer instanceof Timer) timer.stop(); // for after === 0 diff --git a/test/simple/test-net-can-reset-timeout.js b/test/simple/test-net-can-reset-timeout.js new file mode 100644 index 00000000000..db0d68a4b48 --- /dev/null +++ b/test/simple/test-net-can-reset-timeout.js @@ -0,0 +1,41 @@ +var net = require('net'); +var common = require('../common'); +var assert = require('assert'); + +var timeoutCount = 0; + +var server = net.createServer(function(stream){ + stream.setTimeout(100); + + stream.on('timeout', function () { + console.log("timeout"); + // try to reset the timeout. + stream.write("WHAT."); + // don't worry, the socket didn't *really* time out, we're just thinking + // it did. + timeoutCount += 1; + }); + + stream.on('end', function () { + console.log("server side end"); + stream.end(); + }); +}); + +server.listen(common.PORT, function() { + var c = net.createConnection(common.PORT); + + c.on('data', function () { + c.end(); + }); + + c.on('end', function () { + console.log("client side end"); + server.close(); + }); +}); + + +process.on('exit', function () { + assert.equal(1, timeoutCount); +});