Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(clustered pubsub): check that client.isOpen before calling client.disconnect() when unsubscribing #2687

Merged
merged 4 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/client/lib/cluster/cluster-slots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ export default class RedisClusterSlots<
const client = await this.getPubSubClient();
await unsubscribe(client);

if (!client.isPubSubActive) {
if (!client.isPubSubActive && client.isOpen) {
await client.disconnect();
this.pubSubNode = undefined;
}
Expand Down Expand Up @@ -613,7 +613,7 @@ export default class RedisClusterSlots<
const client = await master.pubSubClient;
await unsubscribe(client);

if (!client.isPubSubActive) {
if (!client.isPubSubActive && client.isOpen) {
await client.disconnect();
master.pubSubClient = undefined;
}
Expand Down
27 changes: 27 additions & 0 deletions packages/client/lib/cluster/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,18 @@ describe('Cluster', () => {

assert.equal(cluster.pubSubNode, undefined);
}, GLOBAL.CLUSTERS.OPEN);

testUtils.testWithCluster('concurrent UNSUBSCRIBE does not throw an error (#2685)', async cluster => {
const listener = spy();
await Promise.all([
cluster.subscribe('1', listener),
cluster.subscribe('2', listener)
]);
await Promise.all([
cluster.unsubscribe('1', listener),
cluster.unsubscribe('2', listener)
]);
}, GLOBAL.CLUSTERS.OPEN);

testUtils.testWithCluster('psubscribe & punsubscribe', async cluster => {
const listener = spy();
Expand Down Expand Up @@ -323,6 +335,21 @@ describe('Cluster', () => {
minimumDockerVersion: [7]
});

testUtils.testWithCluster('concurrent SUNSUBCRIBE does not throw an error (#2685)', async cluster => {
const listener = spy();
await Promise.all([
await cluster.sSubscribe('1', listener),
await cluster.sSubscribe('2', listener)
]);
await Promise.all([
cluster.sUnsubscribe('1', listener),
cluster.sUnsubscribe('2', listener)
]);
}, {
...GLOBAL.CLUSTERS.OPEN,
minimumDockerVersion: [7]
});

testUtils.testWithCluster('should handle sharded-channel-moved events', async cluster => {
const SLOT = 10328,
migrating = cluster.slots[SLOT].master,
Expand Down