From 6974d4d438d8876f7656e9f896143b6efd686b6e Mon Sep 17 00:00:00 2001 From: Vitaly Aminev Date: Fri, 22 Jan 2016 11:55:10 +0300 Subject: [PATCH] fix: "MOVED" err not crashing process when slot was not assigned If slot was not previously discovered by ioredis - it would cause a crash as it would try to rewrite a masterNode on a missing object --- lib/cluster.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/cluster.js b/lib/cluster.js index 13f56164..4227674f 100644 --- a/lib/cluster.js +++ b/lib/cluster.js @@ -411,10 +411,17 @@ Cluster.prototype.sendCommand = function (command, stream, node) { var _this = this; if (!node) { command.reject = function (err) { + var partialTry = _.partial(tryConnection, true); + _this.handleError(err, ttl, { moved: function (node, slot, hostPort) { debug('command %s is moved to %s:%s', command.name, hostPort[0], hostPort[1]); - _this.slots[slot].masterNode = node; + var coveredSlot = _this.slots[slot]; + if (!coveredSlot) { + _this.slots[slot] = { masterNode: node, allNodes: [node] }; + } else { + coveredSlot.masterNode = node; + } tryConnection(); _this.refreshSlotsCache(); }, @@ -422,8 +429,8 @@ Cluster.prototype.sendCommand = function (command, stream, node) { debug('command %s is required to ask %s:%s', command.name, hostPort[0], hostPort[1]); tryConnection(false, node); }, - clusterDown: tryConnection.bind(null, true), - connectionClosed: tryConnection.bind(null, true), + clusterDown: partialTry, + connectionClosed: partialTry, maxRedirections: function (redirectionError) { reject.call(command, redirectionError); },