From d4a50e9501e50131e0f89db3cbb41bf5058b5027 Mon Sep 17 00:00:00 2001 From: Brad Harris Date: Wed, 5 Oct 2016 10:02:29 -0600 Subject: [PATCH] tracks destroyed state to avoid an in-flight reconnect if destroy has been called --- lib/Slackbot_worker.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/Slackbot_worker.js b/lib/Slackbot_worker.js index e404b1d5b..c95b3dff3 100755 --- a/lib/Slackbot_worker.js +++ b/lib/Slackbot_worker.js @@ -17,6 +17,9 @@ module.exports = function(botkit, config) { } }; + // Set when destroy() is called - prevents a reconnect from completing + // if it was fired off prior to destroy being called + var destroyed = false; var pingIntervalId = null; var lastPong = 0; var retryBackoff = null; @@ -97,7 +100,7 @@ module.exports = function(botkit, config) { botkit.log.notice('** BOT ID:', bot.identity.name, '...reconnect attempt #' + back.settings.attempt + ' of ' + options.retries + ' being made after ' + back.settings.timeout + 'ms'); bot.startRTM(function(err) { - if (err) { + if (err && !destroyed) { return reconnect(err); } retryBackoff = null; @@ -109,6 +112,9 @@ module.exports = function(botkit, config) { * Shutdown and cleanup the spawned worker */ bot.destroy = function() { + // this prevents a startRTM from completing if it was fired off + // prior to destroy being called + destroyed = true; if (retryBackoff) { retryBackoff.close(); retryBackoff = null; @@ -133,6 +139,12 @@ module.exports = function(botkit, config) { bot.identity = res.self; bot.team_info = res.team; + // Bail out if destroy() was called + if (destroyed) { + botkit.log.notice('Ignoring rtm.start response, bot was destroyed'); + return cb('Ignoring rtm.start response, bot was destroyed'); + } + /** * Also available: * res.users, res.channels, res.groups, res.ims,