Unsubscribe only once on Subscription.close().

We now unsubscribe from Redis only once when closing a Subscription object to avoid duplicate traffic and unexpected Redis responses.

Jedis does not have a mechanism to consume the additional unsubscribe response which leaves protocol frames on the InputStream leading to a corrupt state.

Closes #2355
This commit is contained in:
Mark Paluch
2022-07-21 11:28:49 +02:00
parent dce01ca16f
commit cd1260ed5c
4 changed files with 26 additions and 6 deletions

View File

@@ -38,6 +38,7 @@ class JedisSubscription extends AbstractSubscription {
@Override
protected void doClose() {
if (!getChannels().isEmpty()) {
jedisPubSub.unsubscribe();
}

View File

@@ -76,10 +76,6 @@ public class LettuceSubscription extends AbstractSubscription {
@Override
protected void doClose() {
if (!isAlive()) {
return;
}
List<CompletableFuture<?>> futures = new ArrayList<>();
if (!getChannels().isEmpty()) {

View File

@@ -99,8 +99,19 @@ public abstract class AbstractSubscription implements Subscription {
@Override
public void close() {
doClose();
alive.set(false);
if (alive.compareAndSet(true, false)) {
doClose();
synchronized (channels) {
channels.clear();
}
synchronized (patterns) {
patterns.clear();
}
}
}
/**