From bc4524b54410379be0455c19d0bd1668a3cb3c3b Mon Sep 17 00:00:00 2001 From: Emily Stark Date: Thu, 27 Feb 2014 07:56:36 -0800 Subject: [PATCH] Set up onReconnect after initial sub on the connection to log-reader. If we set it up before `subscribeAndWait` returns, then we'll end up with two subscriptions; we don't have the log-reader sub yet, so we can't stop it when `onReconnect` runs the first time, so we end up with a redundant subscription. This means that if a real reconnect happens later, we'll stop the sub that we set up inside `onReconnect`, but not the initial sub, so we've leaked a sub and end up with duplicate messages after reconnect. --- tools/deploy-galaxy.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/tools/deploy-galaxy.js b/tools/deploy-galaxy.js index d79ef34a31..d879bb6871 100644 --- a/tools/deploy-galaxy.js +++ b/tools/deploy-galaxy.js @@ -457,7 +457,21 @@ exports.logs = function (options) { throw new Error("Can't listen to messages on the logs collection"); var logsSubscription = null; - // In case of reconnect recover the state so user sees only new logs + try { + logsSubscription = + logReader.subscribeAndWait("logsForApp", options.app, + { streaming: options.streaming }); + } catch (e) { + return handleError(e, galaxy, { + "no-such-app": "No such app: " + options.app + }); + } + + // In case of reconnect recover the state so user sees only new logs. + // Only set up the onReconnect handler after the subscribe and wait + // has returned; if we set it up before, then we'll end up with two + // subscriptions, because the onReconnect handler will run for the + // first time before the subscribeAndWait returns. logReader.connection.onReconnect = function () { logsSubscription && logsSubscription.stop(); var opts = { streaming: options.streaming }; @@ -476,16 +490,6 @@ exports.logs = function (options) { ); }; - try { - logsSubscription = - logReader.subscribeAndWait("logsForApp", options.app, - { streaming: options.streaming }); - } catch (e) { - return handleError(e, galaxy, { - "no-such-app": "No such app: " + options.app - }); - } - return options.streaming ? null : 0; } finally { // If not streaming, close the connection to log-reader so that