Skip to content

fix(client): settle argument-less unsubscribe while patterns remain - #3407

Merged
PavelPashov merged 2 commits into
redis:masterfrom
kyungseopk1m:fix/unsubscribe-all-hang
Aug 21, 2026
Merged

PavelPashov merged 2 commits into
redis:masterfrom
kyungseopk1m:fix/unsubscribe-all-hang

Conversation

@kyungseopk1m

@kyungseopk1m kyungseopk1m commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

Description

An argument-less unsubscribe() or pUnsubscribe() hangs when a subscription of the other type is still active, and it misaligns later replies.

The completion check for an argument-less unsubscribe waited for the reply count to reach 0. Redis reports the remaining channel plus pattern count on each UNSUBSCRIBE or PUNSUBSCRIBE reply, and sharded subscriptions are tracked separately, so with a pattern still subscribed the count never reaches 0. The command hangs, and when the server confirmation finally arrives it is matched to the next queued command, so a following PING receives the unsubscribe reply.

This settles the command when the reported count reaches the number of remaining subscriptions of the opposite type instead of 0. Argument-specified unsubscribe and sharded paths keep their previous behavior.

Reproduction, against a real server on both RESP2 and RESP3:

await client.subscribe('ch', () => {});
await client.pSubscribe('pat*', () => {});
await client.unsubscribe();      // never resolves before this change
await client.ping('a');          // returns the unsubscribe reply before this change

Added tests for both directions (channels left after pUnsubscribe(), patterns left after unsubscribe()) on RESP2 and RESP3. They use an asymmetric channel and pattern count so that using the wrong subscription type's count is caught as well.

Checklist

  • Does npm test pass with this change (including linting)? Type check and lint pass. The full mocha suite spins its own Redis test container per case, which did not come up within the harness timeout on my machine, so I verified the change against a local server with the reproduction above rather than the full suite.
  • Is the new or changed code fully tested?
  • Is a documentation update included? N/A, this is a bug fix with no API change.

Note

Medium Risk
Touches Pub/Sub reply matching in the command queue; a wrong residual would hang unsubscribes or steal later command replies. Scope is small and covered by new tests.

Overview
Fixes argument-less unsubscribe() / pUnsubscribe() hanging (and misaligning later replies) when the other subscription type is still active.

Redis reports remaining channel + pattern count on those replies, so the old === 0 completion check never fired. The queue now treats the command as complete when the reported count equals remaining subscriptions of the other type (residualAfterUnsubscribeAll). Sharded unsubscribes still complete at 0.

Tests cover both directions on RESP2 and RESP3, with asymmetric channel vs pattern counts so the wrong residual would fail.

Reviewed by Cursor Bugbot for commit a9f3c31. Bugbot is set up for automated code reviews on this repo. Configure here.

An argument-less UNSUBSCRIBE or PUNSUBSCRIBE settled only when the reply
count reached 0, but Redis reports the remaining channel plus pattern total,
with sharded subscriptions counted separately. While a pattern stayed
subscribed an argument-less unsubscribe never reached 0, so it hung and its
later confirmation was matched to the following command reply.

Settle when the reported count reaches the residual count of the opposite
subscription type instead of 0. Argument-specified and sharded paths keep
their previous behavior.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 21ef8ae31c

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".


// Following commands must receive their own replies (no off-by-one misalignment).
assert.equal(await subscriber.ping('AAA'), 'AAA');
assert.equal(await subscriber.ping('BBB'), 'BBB');

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Leave RESP2 Pub/Sub mode before test teardown

In every new RESP2 case, the callback returns while the opposite subscription is still active. testWithClient then runs await client.flushAll() in its finally block (packages/test-utils/lib/index.ts:451-457), but RESP2 only permits Pub/Sub-safe commands while subscribed, so Redis rejects FLUSHALL and the test fails during teardown before client.destroy() is reached. Explicitly unsubscribe the remaining pattern or channel before returning.

Useful? React with 👍 / 👎.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 21ef8ae. Configure here.

The new RESP2 cases returned while the opposite subscription was still
active, so the harness teardown flushAll ran while the client was in
subscriber mode and was rejected. Unsubscribe the remaining side before
returning. Also drop a duplicated pUnsubscribe case and clear the
pre-existing lint problems in pub-sub.ts, which the changed-files lint
surfaced once the file was touched.

@PavelPashov PavelPashov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution. Looks good!

@PavelPashov
PavelPashov merged commit 2a92b2e into redis:master Aug 21, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants