Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

UNSUBSCRIBE and PUNSUBSCRIBE: always provide a reply.

UNSUBSCRIBE and PUNSUBSCRIBE commands are designed to mass-unsubscribe
the client respectively all the channels and patters if called without
arguments.

However when these functions are called without arguments, but there are
no channels or patters we are subscribed to, the old behavior was to
don't reply at all.

This behavior is broken, as every command should always reply.
Also it is possible that we are no longer subscribed to a channels but we
are subscribed to patters or the other way around, and the client should
be notified with the correct number of subscriptions.

Also it is not pretty that sometimes we did not receive a reply at all
in a redis-cli session from these commands, blocking redis-cli trying
to read the reply.

This fixes issue #714.
  • Loading branch information...
commit 2039f1a38ac23311abc71fc7d862d9e5b22598c6 1 parent 93f61bb
@antirez authored
Showing with 27 additions and 1 deletion.
  1. +16 −0 src/pubsub.c
  2. +11 −1 tests/unit/pubsub.tcl
View
16 src/pubsub.c
@@ -179,6 +179,14 @@ int pubsubUnsubscribeAllChannels(redisClient *c, int notify) {
count += pubsubUnsubscribeChannel(c,channel,notify);
}
+ /* We were subscribed to nothing? Still reply to the client. */
+ if (notify && count == 0) {
+ addReply(c,shared.mbulkhdr[3]);
+ addReply(c,shared.unsubscribebulk);
+ addReply(c,shared.nullbulk);
+ addReplyLongLong(c,dictSize(c->pubsub_channels)+
+ listLength(c->pubsub_patterns));
+ }
dictReleaseIterator(di);
return count;
}
@@ -196,6 +204,14 @@ int pubsubUnsubscribeAllPatterns(redisClient *c, int notify) {
count += pubsubUnsubscribePattern(c,pattern,notify);
}
+ if (notify && count == 0) {
+ /* We were subscribed to nothing? Still reply to the client. */
+ addReply(c,shared.mbulkhdr[3]);
+ addReply(c,shared.punsubscribebulk);
+ addReply(c,shared.nullbulk);
+ addReplyLongLong(c,dictSize(c->pubsub_channels)+
+ listLength(c->pubsub_patterns));
+ }
return count;
}
View
12 tests/unit/pubsub.tcl
@@ -192,4 +192,14 @@ start_server {tags {"pubsub"}} {
# clean up clients
$rd1 close
}
-}
+
+ test "PUNSUBSCRIBE and UNSUBSCRIBE should always reply." {
+ # Make sure we are not subscribed to any channel at all.
+ r punsubscribe
+ r unsubscribe
+ # Now check if the commands still reply correctly.
+ set reply1 [r punsubscribe]
+ set reply2 [r unsubscribe]
+ concat $reply1 $reply2
+ } {punsubscribe {} 0 unsubscribe {} 0}
+}
Please sign in to comment.
Something went wrong with that request. Please try again.