From ebdfad69dce1b3a1a48960328e5f6c3ed8fafa41 Mon Sep 17 00:00:00 2001 From: antirez Date: Mon, 16 Jan 2012 11:27:22 +0100 Subject: [PATCH] Don't change the replication state if SLAVE OF is called with arguments specifying the same master we are already connected with. This fixes issues #290. --- src/replication.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/replication.c b/src/replication.c index 479dcf43..dd42fcf1 100644 --- a/src/replication.c +++ b/src/replication.c @@ -517,9 +517,23 @@ void slaveofCommand(redisClient *c) { redisLog(REDIS_NOTICE,"MASTER MODE enabled (user request)"); } } else { + long port; + + if ((getLongFromObjectOrReply(c, c->argv[2], &port, NULL) != REDIS_OK)) + return; + + /* Check if we are already attached to the specified slave */ + if (server.masterhost && !strcasecmp(server.masterhost,c->argv[1]->ptr) + && server.masterport == port) { + redisLog(REDIS_NOTICE,"SLAVE OF would result into synchronization with the master we are already connected with. No operation performed."); + addReplySds(c,sdsnew("+OK Already connected to specified master\r\n")); + return; + } + /* There was no previous master or the user specified a different one, + * we can continue. */ sdsfree(server.masterhost); server.masterhost = sdsdup(c->argv[1]->ptr); - server.masterport = atoi(c->argv[2]->ptr); + server.masterport = port; if (server.master) freeClient(server.master); if (server.repl_state == REDIS_REPL_TRANSFER) replicationAbortSyncTransfer(); -- 2.49.0