Repository navigation
fix(client): settle argument-less unsubscribe while patterns remain - #3407
Conversation
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.
There was a problem hiding this comment.
💡 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'); |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
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
left a comment
There was a problem hiding this comment.
Thank you for the contribution. Looks good!

Description
An argument-less
unsubscribe()orpUnsubscribe()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:
Added tests for both directions (channels left after
pUnsubscribe(), patterns left afterunsubscribe()) 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
npm testpass 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.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
=== 0completion 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.