Repository navigation
fix: keep subscriber mode while other channel kinds remain subscribed #2196
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
Merged
PavelPashov
merged 10 commits into
redis:main
from
sprayberry-code:fix/pubsub-keep-subscriber-mode-across-channel-kinds
Oct 6, 2026
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
66265b3
fix: keep subscriber mode while other channel kinds remain subscribed
askalf 7efed45
fix: unsubscribe the empty channel name out of the subscription set
askalf 21fb05d
test: scope the subscriber-mode assertions to RESP2
askalf 1628c32
fix: keep subscriber mode when a same-kind subscription remains
askalf 73708ad
Merge branch 'main' into fix/pubsub-keep-subscriber-mode-across-chann…
askalf 81a8859
fix: track subscribed channels in a null-prototype object
askalf e503279
fix: key subscribed channels by their bytes and test emptiness in O(1)
askalf 6cce37e
fix: keep tracking channels when a subscribe reply is not an array
askalf 00de788
test: cover patterns and the count-and-set decision for subscriber mode
askalf 8f949b4
test: cut the subscriber mode tests down to the cases the fix needs
askalf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import { expect } from "chai"; | ||
| import MockServer, { pubSubReply } from "../helpers/mock_server"; | ||
| import Redis from "../../lib/Redis"; | ||
|
|
||
| // Redis counts shard channels separately from channels and patterns, so the | ||
| // count carried by an (S)UNSUBSCRIBE reply only describes channels of the same | ||
| // kind. Dropping the subscriber state on that count alone loses subscriptions | ||
| // of the other kinds that are still active on the connection. | ||
| const PROTOCOLS = [3, 2] as const; | ||
|
|
||
| PROTOCOLS.forEach((protocol, index) => { | ||
| describe(`subscriber mode across channel kinds (RESP${protocol})`, () => { | ||
| const port = 17940 + index; | ||
| let server: MockServer; | ||
| let redis: Redis; | ||
|
|
||
| beforeEach(() => { | ||
| server = new MockServer(port, (argv) => { | ||
| const name = String(argv[0]).toLowerCase(); | ||
| if (name === "info") { | ||
| return "# Server\r\nredis_version:7.0.0\r\n"; | ||
| } | ||
| // Each kind reports the remaining count for that kind only. | ||
| if (name === "subscribe" || name === "ssubscribe") { | ||
| return pubSubReply(protocol, name, argv[1], 1); | ||
| } | ||
| if (name === "sunsubscribe") { | ||
| return pubSubReply(protocol, "sunsubscribe", argv[1], 0); | ||
| } | ||
| if (name === "unsubscribe") { | ||
| return pubSubReply(protocol, "unsubscribe", argv[1], 0); | ||
| } | ||
| return "OK"; | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| if (redis && redis.status !== "end") { | ||
| redis.disconnect(); | ||
| } | ||
| server.disconnect(); | ||
| }); | ||
|
|
||
| it("keeps the regular subscription after sunsubscribe", async () => { | ||
| redis = new Redis({ port, protocol }); | ||
| await redis.subscribe("regular"); | ||
| await redis.ssubscribe("shard"); | ||
| await redis.sunsubscribe("shard"); | ||
|
|
||
| const { subscriber } = redis.condition; | ||
| expect(subscriber).to.not.equal(false); | ||
| expect((subscriber as any).channels("subscribe")).to.eql(["regular"]); | ||
| expect((subscriber as any).channels("ssubscribe")).to.eql([]); | ||
| }); | ||
|
|
||
| it("delivers messages on a channel still subscribed after sunsubscribe", (done) => { | ||
| redis = new Redis({ port, protocol }); | ||
| redis.on("message", (channel, message) => { | ||
| expect(channel).to.eql("regular"); | ||
| expect(message).to.eql("hi"); | ||
| done(); | ||
| }); | ||
| (async () => { | ||
| await redis.subscribe("regular"); | ||
| await redis.ssubscribe("shard"); | ||
| await redis.sunsubscribe("shard"); | ||
| server.broadcast(pubSubReply(protocol, "message", "regular", "hi")); | ||
| })(); | ||
| }); | ||
|
|
||
| it("keeps the shard subscription after unsubscribe", async () => { | ||
| redis = new Redis({ port, protocol }); | ||
| await redis.ssubscribe("shard"); | ||
| await redis.subscribe("regular"); | ||
| await redis.unsubscribe("regular"); | ||
|
|
||
| const { subscriber } = redis.condition; | ||
| expect(subscriber).to.not.equal(false); | ||
| expect((subscriber as any).channels("ssubscribe")).to.eql(["shard"]); | ||
| expect((subscriber as any).channels("subscribe")).to.eql([]); | ||
| }); | ||
|
|
||
| it("leaves subscriber mode once every kind is unsubscribed", async () => { | ||
| redis = new Redis({ port, protocol }); | ||
| await redis.subscribe("regular"); | ||
| await redis.ssubscribe("shard"); | ||
| await redis.unsubscribe("regular"); | ||
| await redis.sunsubscribe("shard"); | ||
|
|
||
| expect(redis.condition.subscriber).to.equal(false); | ||
| expect(redis.mode).to.equal("normal"); | ||
| }); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.