From 66265b3b2b1cead74a49931fd26fd3d3f9c07c8b Mon Sep 17 00:00:00 2001 From: askalf <263217947+askalf@users.noreply.github.com> Date: Mon, 14 Sep 2026 02:08:08 +0000 Subject: [PATCH 1/9] fix: keep subscriber mode while other channel kinds remain subscribed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Redis counts shard channels separately from channels and patterns, so the count in an (S)UNSUBSCRIBE reply describes only channels of the same kind. DataHandler cleared `condition.subscriber` whenever that count reached zero, discarding subscriptions of the other kinds that were still active: SUNSUBSCRIBE of the last shard channel dropped live channel and pattern subscriptions, and UNSUBSCRIBE of the last channel dropped shard ones. Messages on the surviving subscriptions were then no longer delivered — silently under RESP3, and as "Command queue state error" under RESP2. Use SubscriptionSet#isEmpty() so the state is dropped only once every kind is empty. --- lib/DataHandler.ts | 18 +++++-- test/unit/subscriberMode.ts | 94 +++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 5 deletions(-) create mode 100644 test/unit/subscriberMode.ts diff --git a/lib/DataHandler.ts b/lib/DataHandler.ts index 75652dd45..b11f92418 100644 --- a/lib/DataHandler.ts +++ b/lib/DataHandler.ts @@ -211,12 +211,18 @@ export default class DataHandler { if (channel) { this.redis.condition.subscriber.del(replyType, channel); } + + // The reply count only covers channels of the same kind as the + // command (SUNSUBSCRIBE reports remaining shard channels, and + // UNSUBSCRIBE/PUNSUBSCRIBE ignore shard channels), so it reaching + // zero does not mean the connection left subscriber mode. Drop the + // state only once every kind is empty. + if (this.redis.condition.subscriber.isEmpty()) { + this.redis.condition.subscriber = false; + } } const count = reply[2] as string | Buffer | number; - if (Number(count) === 0) { - this.redis.condition.subscriber = false; - } if (this.handleUnsolicitedUnsubscribe(replyType)) { return; @@ -317,10 +323,12 @@ export default class DataHandler { if (channel) { this.redis.condition.subscriber.del(replyType, channel); } - const count = reply[2]; - if (Number(count) === 0) { + // See the matching comment in `returnPush`: the count is per channel + // kind, so only an empty set means subscriber mode is over. + if (this.redis.condition.subscriber.isEmpty()) { this.redis.condition.subscriber = false; } + const count = reply[2]; if (this.handleUnsolicitedUnsubscribe(replyType)) { break; } diff --git a/test/unit/subscriberMode.ts b/test/unit/subscriberMode.ts new file mode 100644 index 000000000..f7190ef4a --- /dev/null +++ b/test/unit/subscriberMode.ts @@ -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"); + }); + }); +}); From 7efed45e3d90416efe46c195257358ea0fe999a9 Mon Sep 17 00:00:00 2001 From: askalf <263217947+askalf@users.noreply.github.com> Date: Mon, 14 Sep 2026 03:04:47 +0000 Subject: [PATCH 2/9] fix: unsubscribe the empty channel name out of the subscription set `""` is a legal channel name, but the `(S|P)UNSUBSCRIBE` reply handlers guarded `del()` with `if (channel)`, so a reply naming the empty channel never removed it. `SubscriptionSet#isEmpty()` then stayed false and `condition.subscriber` was never cleared, leaving the connection stuck in subscriber mode after its last channel was unsubscribed. The earlier `Number(count) === 0` check did not have this gap. Test both guards for a missing reply element instead of truthiness, and cover subscribe/unsubscribe of an empty channel name for both regular and shard channels under RESP2 and RESP3. --- lib/DataHandler.ts | 12 ++++++++---- test/unit/subscriberMode.ts | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/DataHandler.ts b/lib/DataHandler.ts index b11f92418..c0d030d63 100644 --- a/lib/DataHandler.ts +++ b/lib/DataHandler.ts @@ -207,8 +207,11 @@ export default class DataHandler { case "unsubscribe": case "punsubscribe": { if (this.redis.condition.subscriber) { - const channel = reply[1] ? reply[1].toString() : null; - if (channel) { + // `""` is a valid channel name, so both guards test for a missing + // reply element rather than truthiness — skipping `del()` for it + // would keep the set non-empty and strand subscriber mode below. + const channel = reply[1] == null ? null : reply[1].toString(); + if (channel !== null) { this.redis.condition.subscriber.del(replyType, channel); } @@ -319,8 +322,9 @@ export default class DataHandler { case "sunsubscribe": case "unsubscribe": case "punsubscribe": { - const channel = reply[1] ? reply[1].toString() : null; - if (channel) { + // See the matching comment in `returnPush` about the empty channel name. + const channel = reply[1] == null ? null : reply[1].toString(); + if (channel !== null) { this.redis.condition.subscriber.del(replyType, channel); } // See the matching comment in `returnPush`: the count is per channel diff --git a/test/unit/subscriberMode.ts b/test/unit/subscriberMode.ts index f7190ef4a..6ef49a50a 100644 --- a/test/unit/subscriberMode.ts +++ b/test/unit/subscriberMode.ts @@ -90,5 +90,26 @@ PROTOCOLS.forEach((protocol, index) => { expect(redis.condition.subscriber).to.equal(false); expect(redis.mode).to.equal("normal"); }); + + // `""` is a legal channel name, and the reply naming it must still be + // removed from the subscription set — a truthiness check on the channel + // skips the removal and leaves the connection stuck in subscriber mode. + it("leaves subscriber mode after unsubscribing an empty channel name", async () => { + redis = new Redis({ port, protocol }); + await redis.subscribe(""); + await redis.unsubscribe(""); + + expect(redis.condition.subscriber).to.equal(false); + expect(redis.mode).to.equal("normal"); + }); + + it("leaves subscriber mode after sunsubscribing an empty shard channel name", async () => { + redis = new Redis({ port, protocol }); + await redis.ssubscribe(""); + await redis.sunsubscribe(""); + + expect(redis.condition.subscriber).to.equal(false); + expect(redis.mode).to.equal("normal"); + }); }); }); From 21fb05d9abd0c7a33f3d26865678db61a998add9 Mon Sep 17 00:00:00 2001 From: askalf <263217947+askalf@users.noreply.github.com> Date: Mon, 14 Sep 2026 12:00:01 +0000 Subject: [PATCH 3/9] test: scope the subscriber-mode assertions to RESP2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `Redis#mode` is derived from `isResp2SubscriberMode`, which is false for protocol 3, so under RESP3 it reads "normal" even while the connection is subscribed. Asserting on it there passes whether or not the subscription state was cleaned up. Assert `condition.subscriber` on both protocols — that is the check which discriminates — and keep the `mode` assertion for RESP2 only. Rename the affected tests to say they cover subscription-state cleanup. --- test/unit/subscriberMode.ts | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/test/unit/subscriberMode.ts b/test/unit/subscriberMode.ts index 6ef49a50a..6b66b320b 100644 --- a/test/unit/subscriberMode.ts +++ b/test/unit/subscriberMode.ts @@ -8,6 +8,20 @@ import Redis from "../../lib/Redis"; // of the other kinds that are still active on the connection. const PROTOCOLS = [3, 2] as const; +// `Redis#mode` only reports the subscriber state under RESP2. With RESP3 push +// messages share the connection, so it reads `"normal"` even while subscribed +// and asserting on it would pass whether or not the subscription state was +// cleaned up. `condition.subscriber` is the check that discriminates on both. +function expectSubscriptionStateCleared( + redis: Redis, + protocol: typeof PROTOCOLS[number] +) { + expect(redis.condition.subscriber).to.equal(false); + if (protocol === 2) { + expect(redis.mode).to.equal("normal"); + } +} + PROTOCOLS.forEach((protocol, index) => { describe(`subscriber mode across channel kinds (RESP${protocol})`, () => { const port = 17940 + index; @@ -80,36 +94,33 @@ PROTOCOLS.forEach((protocol, index) => { expect((subscriber as any).channels("subscribe")).to.eql([]); }); - it("leaves subscriber mode once every kind is unsubscribed", async () => { + it("clears the subscription state 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"); + expectSubscriptionStateCleared(redis, protocol); }); // `""` is a legal channel name, and the reply naming it must still be // removed from the subscription set — a truthiness check on the channel // skips the removal and leaves the connection stuck in subscriber mode. - it("leaves subscriber mode after unsubscribing an empty channel name", async () => { + it("clears the subscription state after unsubscribing an empty channel name", async () => { redis = new Redis({ port, protocol }); await redis.subscribe(""); await redis.unsubscribe(""); - expect(redis.condition.subscriber).to.equal(false); - expect(redis.mode).to.equal("normal"); + expectSubscriptionStateCleared(redis, protocol); }); - it("leaves subscriber mode after sunsubscribing an empty shard channel name", async () => { + it("clears the subscription state after sunsubscribing an empty shard channel name", async () => { redis = new Redis({ port, protocol }); await redis.ssubscribe(""); await redis.sunsubscribe(""); - expect(redis.condition.subscriber).to.equal(false); - expect(redis.mode).to.equal("normal"); + expectSubscriptionStateCleared(redis, protocol); }); }); }); From 1628c32b1f2fd9a5c5fadf342db9e0ba4c950005 Mon Sep 17 00:00:00 2001 From: askalf <263217947+askalf@users.noreply.github.com> Date: Mon, 14 Sep 2026 12:49:22 +0000 Subject: [PATCH 4/9] fix: keep subscriber mode when a same-kind subscription remains The subscription set keys channels by their utf8 rendering, so channel names that are distinct as bytes but decode alike (e.g. the invalid sequences <80> and <81>, which both render as U+FFFD) collapse onto one key. Unsubscribing either one empties the set while the server still reports a same-kind subscription, and clearing the state on the set alone then drops RESP3 messages and misroutes RESP2 ones to the command queue. Require both the set to be empty and the reply's count to be zero before leaving subscriber mode: the count rules out a same-kind subscription the set collapsed, and the set rules out the channel kinds the count ignores. --- lib/DataHandler.ts | 49 ++++++++++++++------ test/helpers/mock_server.ts | 32 +++++++++++++ test/unit/subscriberMode.ts | 91 ++++++++++++++++++++++++++++++++++++- 3 files changed, 156 insertions(+), 16 deletions(-) diff --git a/lib/DataHandler.ts b/lib/DataHandler.ts index c0d030d63..da39b77ff 100644 --- a/lib/DataHandler.ts +++ b/lib/DataHandler.ts @@ -206,6 +206,8 @@ export default class DataHandler { case "sunsubscribe": case "unsubscribe": case "punsubscribe": { + const count = reply[2] as string | Buffer | number; + if (this.redis.condition.subscriber) { // `""` is a valid channel name, so both guards test for a missing // reply element rather than truthiness — skipping `del()` for it @@ -215,18 +217,9 @@ export default class DataHandler { this.redis.condition.subscriber.del(replyType, channel); } - // The reply count only covers channels of the same kind as the - // command (SUNSUBSCRIBE reports remaining shard channels, and - // UNSUBSCRIBE/PUNSUBSCRIBE ignore shard channels), so it reaching - // zero does not mean the connection left subscriber mode. Drop the - // state only once every kind is empty. - if (this.redis.condition.subscriber.isEmpty()) { - this.redis.condition.subscriber = false; - } + leaveSubscriberModeIfDone(this.redis.condition, count); } - const count = reply[2] as string | Buffer | number; - if (this.handleUnsolicitedUnsubscribe(replyType)) { return; } @@ -327,12 +320,8 @@ export default class DataHandler { if (channel !== null) { this.redis.condition.subscriber.del(replyType, channel); } - // See the matching comment in `returnPush`: the count is per channel - // kind, so only an empty set means subscriber mode is over. - if (this.redis.condition.subscriber.isEmpty()) { - this.redis.condition.subscriber = false; - } const count = reply[2]; + leaveSubscriberModeIfDone(this.redis.condition, count); if (this.handleUnsolicitedUnsubscribe(replyType)) { break; } @@ -403,6 +392,36 @@ export default class DataHandler { } } +// Both the count carried by the reply and the subscription set are partial +// views of the connection's subscriptions, and neither one alone answers +// "is this connection still in subscriber mode?": +// +// - The count only covers channels of the same kind as the command +// (SUNSUBSCRIBE reports remaining shard channels, and UNSUBSCRIBE / +// PUNSUBSCRIBE ignore shard channels), so it reaching zero says nothing +// about the other kinds. +// - The set is keyed by the utf8 rendering of the channel name, so channel +// names that are distinct as bytes but decode alike (e.g. the invalid +// sequences `<80>` and `<81>`, which both render as U+FFFD) share one key. +// Unsubscribing either one empties that key, so the set can read empty +// while the server still holds a same-kind subscription. +// +// Requiring both covers each one's blind spot with the other: the count rules +// out a same-kind subscription the set collapsed, and the set rules out the +// kinds the count ignores. A reply carrying no count is not authoritative +// about anything, so it does not hold the connection in subscriber mode on its +// own. +function leaveSubscriberModeIfDone( + condition: Condition, + count: string | Buffer | number +) { + const remaining = Number(count); + const subscriber = condition.subscriber as SubscriptionSet; + if (subscriber.isEmpty() && !(remaining > 0)) { + condition.subscriber = false; + } +} + // All string RESP types (simple, blob, verbatim) decode to Buffer rather than // utf8 strings. This keeps the parser encoding-agnostic: ioredis decides // utf8-vs-Buffer per command afterwards via `replyEncoding`. Decoding to utf8 diff --git a/test/helpers/mock_server.ts b/test/helpers/mock_server.ts index ab1633b5e..1b1de6287 100644 --- a/test/helpers/mock_server.ts +++ b/test/helpers/mock_server.ts @@ -222,3 +222,35 @@ export function pubSubReply( } return [type, channel, value]; } + +// `pubSubReply` goes through the string-based writer, which cannot carry a +// channel name that is not valid utf8. This builds the same reply as raw bytes +// instead, for the binary-safe channel names, and returns a Buffer to write to +// the socket directly (RESP3 push frame or RESP2 array, as the protocol asks). +export function rawPubSubReply( + protocol: MockServerProtocol, + type: string, + channel: string | Buffer, + value: string | number = 1 +): Buffer { + const channelBytes = Buffer.isBuffer(channel) + ? channel + : Buffer.from(channel); + const parts = [ + Buffer.from(`${protocol === 3 ? ">" : "*"}3\r\n`), + blobString(Buffer.from(type)), + blobString(channelBytes), + typeof value === "number" + ? Buffer.from(`:${value}\r\n`) + : blobString(Buffer.from(value)), + ]; + return Buffer.concat(parts); +} + +function blobString(payload: Buffer): Buffer { + return Buffer.concat([ + Buffer.from(`$${payload.length}\r\n`), + payload, + Buffer.from("\r\n"), + ]); +} diff --git a/test/unit/subscriberMode.ts b/test/unit/subscriberMode.ts index 6b66b320b..2a2901ffd 100644 --- a/test/unit/subscriberMode.ts +++ b/test/unit/subscriberMode.ts @@ -1,5 +1,9 @@ import { expect } from "chai"; -import MockServer, { pubSubReply } from "../helpers/mock_server"; +import { Socket } from "net"; +import MockServer, { + pubSubReply, + rawPubSubReply, +} from "../helpers/mock_server"; import Redis from "../../lib/Redis"; // Redis counts shard channels separately from channels and patterns, so the @@ -122,5 +126,90 @@ PROTOCOLS.forEach((protocol, index) => { expectSubscriptionStateCleared(redis, protocol); }); + + // Channel names are binary safe, but the subscription set keys them by + // their utf8 rendering, so two distinct names can collapse onto one key: + // `<80>` and `<81>` are both invalid utf8 and both render as U+FFFD. + // Unsubscribing one empties the set while the server still reports the + // other as subscribed, so the set alone cannot decide this transition -- + // the count in the reply is what says a same-kind subscription remains. + describe("with binary channel names that share a utf8 rendering", () => { + const first = Buffer.from([0x80]); + const second = Buffer.from([0x81]); + + // Replies naming these channels have to keep the raw bytes, which the + // mock server's string-based writer cannot do, so they are written to + // the socket directly and the automatic reply is suppressed. + function replyWithBytes( + socket: Socket, + type: string, + channel: Buffer, + count: number + ) { + socket.write(rawPubSubReply(protocol, type, channel, count)); + } + + // Both SUBSCRIBEs are acknowledged with the channel the client asked + // for; the server counts them separately, so the second reports 2. + // UNSUBSCRIBE of the first then reports 1 remaining. + function serveBinaryChannels(server: MockServer) { + let subscribed = 0; + server.handler = (argv, socket, flags) => { + const name = String(argv[0]).toLowerCase(); + if (name === "info") { + return "# Server\r\nredis_version:7.0.0\r\n"; + } + if (name === "subscribe") { + subscribed += 1; + flags.hang = true; + replyWithBytes( + socket, + "subscribe", + subscribed === 1 ? first : second, + subscribed + ); + return undefined; + } + if (name === "unsubscribe") { + subscribed -= 1; + flags.hang = true; + replyWithBytes(socket, "unsubscribe", first, subscribed); + return undefined; + } + return "OK"; + }; + } + + it("keeps subscriber mode while the other one is still subscribed", async () => { + serveBinaryChannels(server); + redis = new Redis({ port, protocol }); + await redis.subscribe(first); + await redis.subscribe(second); + await redis.unsubscribe(first); + + expect(redis.condition.subscriber).to.not.equal(false); + if (protocol === 2) { + expect(redis.mode).to.equal("subscriber"); + } + }); + + it("still delivers messages on the one that is still subscribed", (done) => { + serveBinaryChannels(server); + redis = new Redis({ port, protocol }); + redis.on("messageBuffer", (channel, message) => { + expect(channel).to.eql(second); + expect(message.toString()).to.equal("hi"); + done(); + }); + (async () => { + await redis.subscribe(first); + await redis.subscribe(second); + await redis.unsubscribe(first); + server + .getAllClients()[0] + .write(rawPubSubReply(protocol, "message", second, "hi")); + })(); + }); + }); }); }); From 81a88594bc5826c83231b36a7da05490ae2aa093 Mon Sep 17 00:00:00 2001 From: askalf <263217947+askalf@users.noreply.github.com> Date: Mon, 14 Sep 2026 18:11:39 +0000 Subject: [PATCH 5/9] fix: track subscribed channels in a null-prototype object A channel named `__proto__` was never stored as an own key of the plain object backing the subscription set, so `channels()` did not list it and `isEmpty()` reported the set empty while the subscription was live. An (S)UNSUBSCRIBE of another kind then cleared the subscriber state and the connection dropped messages under RESP3 and misrouted them under RESP2. --- lib/SubscriptionSet.ts | 18 +++++++++++---- test/unit/subscriberMode.ts | 45 +++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/lib/SubscriptionSet.ts b/lib/SubscriptionSet.ts index 98a92c627..0131cc296 100644 --- a/lib/SubscriptionSet.ts +++ b/lib/SubscriptionSet.ts @@ -3,14 +3,24 @@ import { CommandNameFlags } from "./Command"; type AddSet = CommandNameFlags["ENTER_SUBSCRIBER_MODE"][number]; type DelSet = CommandNameFlags["EXIT_SUBSCRIBER_MODE"][number]; +type ChannelSet = { [channel: string]: boolean }; + +// Channel names come from the server, so they can collide with the names +// inherited from `Object.prototype`. Assigning `__proto__` on a plain object +// hits the prototype setter instead of creating an own key, which would hide +// the channel from `channels()` and from `isEmpty()`. +function createChannelSet(): ChannelSet { + return Object.create(null) as ChannelSet; +} + /** * Tiny class to simplify dealing with subscription set */ export default class SubscriptionSet { - private set: { [key: string]: { [channel: string]: boolean } } = { - subscribe: {}, - psubscribe: {}, - ssubscribe: {}, + private set: { [key: string]: ChannelSet } = { + subscribe: createChannelSet(), + psubscribe: createChannelSet(), + ssubscribe: createChannelSet(), }; add(set: AddSet, channel: string) { diff --git a/test/unit/subscriberMode.ts b/test/unit/subscriberMode.ts index 2a2901ffd..b5e26a4ee 100644 --- a/test/unit/subscriberMode.ts +++ b/test/unit/subscriberMode.ts @@ -127,6 +127,51 @@ PROTOCOLS.forEach((protocol, index) => { expectSubscriptionStateCleared(redis, protocol); }); + // Channel names are chosen by the application, so they can collide with + // the names inherited from `Object.prototype`. `__proto__` in particular + // is never stored as an own key on a plain object, so the subscription + // set would report itself empty while the channel is still subscribed. + it("keeps a channel named __proto__ subscribed after sunsubscribe", async () => { + redis = new Redis({ port, protocol }); + await redis.subscribe("__proto__"); + 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(["__proto__"]); + expect((subscriber as any).channels("ssubscribe")).to.eql([]); + }); + + it("delivers messages on a channel named __proto__ after sunsubscribe", (done) => { + redis = new Redis({ port, protocol }); + redis.on("message", (channel, message) => { + expect(channel).to.eql("__proto__"); + expect(message).to.eql("hi"); + done(); + }); + (async () => { + await redis.subscribe("__proto__"); + await redis.ssubscribe("shard"); + await redis.sunsubscribe("shard"); + server.broadcast(pubSubReply(protocol, "message", "__proto__", "hi")); + })(); + }); + + // The removal has to reach the stored key as well: the channel is tracked + // while subscribed, and unsubscribing it empties the set and leaves the + // connection out of subscriber mode. + it("tracks and then clears a subscription to __proto__", async () => { + redis = new Redis({ port, protocol }); + await redis.subscribe("__proto__"); + expect((redis.condition.subscriber as any).channels("subscribe")).to.eql([ + "__proto__", + ]); + + await redis.unsubscribe("__proto__"); + expectSubscriptionStateCleared(redis, protocol); + }); + // Channel names are binary safe, but the subscription set keys them by // their utf8 rendering, so two distinct names can collapse onto one key: // `<80>` and `<81>` are both invalid utf8 and both render as U+FFFD. From e503279ed363984bdb9c28995c1e35354f2ac3e8 Mon Sep 17 00:00:00 2001 From: askalf <263217947+askalf@users.noreply.github.com> Date: Mon, 14 Sep 2026 19:08:52 +0000 Subject: [PATCH 6/9] fix: key subscribed channels by their bytes and test emptiness in O(1) The subscription set keyed channels by their utf8 rendering, so names that are distinct as bytes but decode alike (e.g. the invalid sequences <80> and <81>, which both render as U+FFFD) shared one entry. Unsubscribing either one dropped both, leaving the set empty while the server still held a live subscription. The reply count only covers its own kind, so it hides this for the acknowledgement that carries it but not for a later zero count from another kind: SUBSCRIBE <80>, SUBSCRIBE <81>, UNSUBSCRIBE <80>, SSUBSCRIBE, SUNSUBSCRIBE leaves subscriber mode with <81> still subscribed. Its later messages are then dropped under RESP3 and misrouted to the command queue under RESP2. Key the set by latin1, which round-trips every byte, and keep the utf8 rendering as the value so channels() is unchanged for its callers. Storing the channels in a Map also makes isEmpty() a size check. It runs once per unsubscribe acknowledgement and the server sends one per channel, so materializing the keys made unsubscribing N channels O(N^2) work and could stall the event loop for large subscription sets. --- lib/DataHandler.ts | 26 +++++++------- lib/SubscriptionSet.ts | 49 ++++++++++++++++---------- test/unit/subscriberMode.ts | 70 +++++++++++++++++++++++++++++++++---- 3 files changed, 106 insertions(+), 39 deletions(-) diff --git a/lib/DataHandler.ts b/lib/DataHandler.ts index da39b77ff..49071f663 100644 --- a/lib/DataHandler.ts +++ b/lib/DataHandler.ts @@ -154,7 +154,7 @@ export default class DataHandler { this.redis.condition.subscriber = new SubscriptionSet(); this.redis.condition.subscriber.add( item.command.name, - reply[1].toString() + reply[1] as string | Buffer ); if (!fillSubCommand(item.command, reply[2])) { @@ -190,7 +190,7 @@ export default class DataHandler { this.redis.condition.subscriber = new SubscriptionSet(); } - const channel = reply[1].toString(); + const channel = reply[1] as string | Buffer; this.redis.condition.subscriber.add(replyType, channel); const item = this.shiftCommand(reply); if (!item) { @@ -212,7 +212,8 @@ export default class DataHandler { // `""` is a valid channel name, so both guards test for a missing // reply element rather than truthiness — skipping `del()` for it // would keep the set non-empty and strand subscriber mode below. - const channel = reply[1] == null ? null : reply[1].toString(); + const channel = + reply[1] == null ? null : (reply[1] as string | Buffer); if (channel !== null) { this.redis.condition.subscriber.del(replyType, channel); } @@ -301,7 +302,7 @@ export default class DataHandler { case "ssubscribe": case "subscribe": case "psubscribe": { - const channel = reply[1].toString(); + const channel = reply[1] as string | Buffer; this.redis.condition.subscriber.add(replyType, channel); const item = this.shiftCommand(reply); if (!item) { @@ -316,7 +317,7 @@ export default class DataHandler { case "unsubscribe": case "punsubscribe": { // See the matching comment in `returnPush` about the empty channel name. - const channel = reply[1] == null ? null : reply[1].toString(); + const channel = reply[1] == null ? null : (reply[1] as string | Buffer); if (channel !== null) { this.redis.condition.subscriber.del(replyType, channel); } @@ -400,17 +401,14 @@ export default class DataHandler { // (SUNSUBSCRIBE reports remaining shard channels, and UNSUBSCRIBE / // PUNSUBSCRIBE ignore shard channels), so it reaching zero says nothing // about the other kinds. -// - The set is keyed by the utf8 rendering of the channel name, so channel -// names that are distinct as bytes but decode alike (e.g. the invalid -// sequences `<80>` and `<81>`, which both render as U+FFFD) share one key. -// Unsubscribing either one empties that key, so the set can read empty -// while the server still holds a same-kind subscription. +// - The set only tracks what this connection subscribed to through ioredis, +// so it cannot see a subscription the server holds but never acknowledged +// to it. // // Requiring both covers each one's blind spot with the other: the count rules -// out a same-kind subscription the set collapsed, and the set rules out the -// kinds the count ignores. A reply carrying no count is not authoritative -// about anything, so it does not hold the connection in subscriber mode on its -// own. +// out a remaining same-kind subscription, and the set rules out the kinds the +// count ignores. A reply carrying no count is not authoritative about +// anything, so it does not hold the connection in subscriber mode on its own. function leaveSubscriberModeIfDone( condition: Condition, count: string | Buffer | number diff --git a/lib/SubscriptionSet.ts b/lib/SubscriptionSet.ts index 0131cc296..00138a1cd 100644 --- a/lib/SubscriptionSet.ts +++ b/lib/SubscriptionSet.ts @@ -3,14 +3,22 @@ import { CommandNameFlags } from "./Command"; type AddSet = CommandNameFlags["ENTER_SUBSCRIBER_MODE"][number]; type DelSet = CommandNameFlags["EXIT_SUBSCRIBER_MODE"][number]; -type ChannelSet = { [channel: string]: boolean }; - -// Channel names come from the server, so they can collide with the names -// inherited from `Object.prototype`. Assigning `__proto__` on a plain object -// hits the prototype setter instead of creating an own key, which would hide -// the channel from `channels()` and from `isEmpty()`. -function createChannelSet(): ChannelSet { - return Object.create(null) as ChannelSet; +// Channel names are binary safe, so they are keyed by their bytes rather than +// by their utf8 rendering: names that are distinct as bytes can render alike +// (e.g. the invalid sequences `<80>` and `<81>`, which both decode to U+FFFD), +// and sharing a key would drop one of them from the set. `latin1` maps every +// byte to one code unit, so the key round-trips the exact bytes. The value +// keeps the utf8 rendering, which is what `channels()` hands back. +type ChannelSet = Map; + +function channelKey(channel: string | Buffer): string { + return (Buffer.isBuffer(channel) ? channel : Buffer.from(channel)).toString( + "latin1" + ); +} + +function channelName(channel: string | Buffer): string { + return Buffer.isBuffer(channel) ? channel.toString() : channel; } /** @@ -18,28 +26,31 @@ function createChannelSet(): ChannelSet { */ export default class SubscriptionSet { private set: { [key: string]: ChannelSet } = { - subscribe: createChannelSet(), - psubscribe: createChannelSet(), - ssubscribe: createChannelSet(), + subscribe: new Map(), + psubscribe: new Map(), + ssubscribe: new Map(), }; - add(set: AddSet, channel: string) { - this.set[mapSet(set)][channel] = true; + add(set: AddSet, channel: string | Buffer) { + this.set[mapSet(set)].set(channelKey(channel), channelName(channel)); } - del(set: DelSet, channel: string) { - delete this.set[mapSet(set)][channel]; + del(set: DelSet, channel: string | Buffer) { + this.set[mapSet(set)].delete(channelKey(channel)); } channels(set: AddSet | DelSet): string[] { - return Object.keys(this.set[mapSet(set)]); + return Array.from(this.set[mapSet(set)].values()); } + // Called once per unsubscribe acknowledgement, and the server sends one per + // channel, so this stays O(1): counting the keys instead would make + // unsubscribing N channels Θ(N²) work and stall the event loop. isEmpty(): boolean { return ( - this.channels("subscribe").length === 0 && - this.channels("psubscribe").length === 0 && - this.channels("ssubscribe").length === 0 + this.set.subscribe.size === 0 && + this.set.psubscribe.size === 0 && + this.set.ssubscribe.size === 0 ); } } diff --git a/test/unit/subscriberMode.ts b/test/unit/subscriberMode.ts index b5e26a4ee..1b1ebd29a 100644 --- a/test/unit/subscriberMode.ts +++ b/test/unit/subscriberMode.ts @@ -172,12 +172,10 @@ PROTOCOLS.forEach((protocol, index) => { expectSubscriptionStateCleared(redis, protocol); }); - // Channel names are binary safe, but the subscription set keys them by - // their utf8 rendering, so two distinct names can collapse onto one key: - // `<80>` and `<81>` are both invalid utf8 and both render as U+FFFD. - // Unsubscribing one empties the set while the server still reports the - // other as subscribed, so the set alone cannot decide this transition -- - // the count in the reply is what says a same-kind subscription remains. + // Channel names are binary safe, so the subscription set has to key them + // by their bytes: `<80>` and `<81>` are both invalid utf8 and both render + // as U+FFFD, so keying by that rendering collapses the two onto one entry + // and unsubscribing either one drops both from the set. describe("with binary channel names that share a utf8 rendering", () => { const first = Buffer.from([0x80]); const second = Buffer.from([0x81]); @@ -221,6 +219,15 @@ PROTOCOLS.forEach((protocol, index) => { replyWithBytes(socket, "unsubscribe", first, subscribed); return undefined; } + // Shard channels are counted separately by the server, so the shard + // acknowledgements carry their own count and say nothing about the + // regular channels that are still subscribed. + if (name === "ssubscribe") { + return pubSubReply(protocol, "ssubscribe", argv[1], 1); + } + if (name === "sunsubscribe") { + return pubSubReply(protocol, "sunsubscribe", argv[1], 0); + } return "OK"; }; } @@ -238,6 +245,57 @@ PROTOCOLS.forEach((protocol, index) => { } }); + // The count in an acknowledgement only describes its own kind, so a + // shard count of 0 says nothing about the two regular channels. If the + // set has collapsed them onto one key it already reads empty by then, + // and the connection leaves subscriber mode with a channel still live. + it("keeps subscriber mode when a shard count of 0 arrives later", async () => { + serveBinaryChannels(server); + redis = new Redis({ port, protocol }); + await redis.subscribe(first); + await redis.subscribe(second); + await redis.unsubscribe(first); + await redis.ssubscribe("shard"); + await redis.sunsubscribe("shard"); + + expect(redis.condition.subscriber).to.not.equal(false); + if (protocol === 2) { + expect(redis.mode).to.equal("subscriber"); + } + }); + + it("still delivers messages after a later shard count of 0", (done) => { + serveBinaryChannels(server); + redis = new Redis({ port, protocol }); + redis.on("messageBuffer", (channel, message) => { + expect(channel).to.eql(second); + expect(message.toString()).to.equal("hi"); + done(); + }); + (async () => { + await redis.subscribe(first); + await redis.subscribe(second); + await redis.unsubscribe(first); + await redis.ssubscribe("shard"); + await redis.sunsubscribe("shard"); + server + .getAllClients()[0] + .write(rawPubSubReply(protocol, "message", second, "hi")); + })(); + }); + + // Both names have to stay individually tracked, or the reconnect path + // resubscribes to only one of them. + it("tracks both names as separate channels", async () => { + serveBinaryChannels(server); + redis = new Redis({ port, protocol }); + await redis.subscribe(first); + await redis.subscribe(second); + + const { subscriber } = redis.condition; + expect((subscriber as any).channels("subscribe")).to.have.lengthOf(2); + }); + it("still delivers messages on the one that is still subscribed", (done) => { serveBinaryChannels(server); redis = new Redis({ port, protocol }); From 6cce37ed73475206b9b3874ac8ebfe95f9e2772b Mon Sep 17 00:00:00 2001 From: askalf <263217947+askalf@users.noreply.github.com> Date: Tue, 15 Sep 2026 10:52:30 +0000 Subject: [PATCH 7/9] fix: keep tracking channels when a subscribe reply is not an array A server or proxy that answers SUBSCRIBE with a simple string leaves `reply[1]` as a byte of that string rather than a channel name. Keying channels by their bytes made `SubscriptionSet` reject it outright, and the throw escaped the decoder's reply dispatch as an uncaught error. Accept the value and key it by its string form, as the previous `toString()` did, so only the key derivation changed and not which replies the set tolerates. --- lib/DataHandler.ts | 13 ++++++------- lib/SubscriptionSet.ts | 23 +++++++++++++++-------- test/unit/subscriberMode.ts | 24 ++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/lib/DataHandler.ts b/lib/DataHandler.ts index 49071f663..b8c45c127 100644 --- a/lib/DataHandler.ts +++ b/lib/DataHandler.ts @@ -9,7 +9,7 @@ import Deque = require("denque"); import { EventEmitter } from "events"; import Command from "./Command"; import { Debug } from "./utils"; -import SubscriptionSet from "./SubscriptionSet"; +import SubscriptionSet, { ChannelName } from "./SubscriptionSet"; import { Decoder, RESP_TYPES } from "./resp/decoder"; import { TypeMapping } from "./resp/types"; @@ -154,7 +154,7 @@ export default class DataHandler { this.redis.condition.subscriber = new SubscriptionSet(); this.redis.condition.subscriber.add( item.command.name, - reply[1] as string | Buffer + reply[1] as ChannelName ); if (!fillSubCommand(item.command, reply[2])) { @@ -190,7 +190,7 @@ export default class DataHandler { this.redis.condition.subscriber = new SubscriptionSet(); } - const channel = reply[1] as string | Buffer; + const channel = reply[1] as ChannelName; this.redis.condition.subscriber.add(replyType, channel); const item = this.shiftCommand(reply); if (!item) { @@ -212,8 +212,7 @@ export default class DataHandler { // `""` is a valid channel name, so both guards test for a missing // reply element rather than truthiness — skipping `del()` for it // would keep the set non-empty and strand subscriber mode below. - const channel = - reply[1] == null ? null : (reply[1] as string | Buffer); + const channel = reply[1] == null ? null : (reply[1] as ChannelName); if (channel !== null) { this.redis.condition.subscriber.del(replyType, channel); } @@ -302,7 +301,7 @@ export default class DataHandler { case "ssubscribe": case "subscribe": case "psubscribe": { - const channel = reply[1] as string | Buffer; + const channel = reply[1] as ChannelName; this.redis.condition.subscriber.add(replyType, channel); const item = this.shiftCommand(reply); if (!item) { @@ -317,7 +316,7 @@ export default class DataHandler { case "unsubscribe": case "punsubscribe": { // See the matching comment in `returnPush` about the empty channel name. - const channel = reply[1] == null ? null : (reply[1] as string | Buffer); + const channel = reply[1] == null ? null : (reply[1] as ChannelName); if (channel !== null) { this.redis.condition.subscriber.del(replyType, channel); } diff --git a/lib/SubscriptionSet.ts b/lib/SubscriptionSet.ts index 00138a1cd..c63536fa5 100644 --- a/lib/SubscriptionSet.ts +++ b/lib/SubscriptionSet.ts @@ -3,6 +3,13 @@ import { CommandNameFlags } from "./Command"; type AddSet = CommandNameFlags["ENTER_SUBSCRIBER_MODE"][number]; type DelSet = CommandNameFlags["EXIT_SUBSCRIBER_MODE"][number]; +// The channel name is read out of a reply, and a reply is not guaranteed to be +// the array a subscribe acknowledgement normally is: a server or proxy that +// answers `SUBSCRIBE` with a simple string leaves the caller indexing a Buffer, +// which yields a byte. Such a value has no bytes of its own to preserve, so it +// is keyed by its string form rather than rejected. +export type ChannelName = string | Buffer | number; + // Channel names are binary safe, so they are keyed by their bytes rather than // by their utf8 rendering: names that are distinct as bytes can render alike // (e.g. the invalid sequences `<80>` and `<81>`, which both decode to U+FFFD), @@ -11,14 +18,14 @@ type DelSet = CommandNameFlags["EXIT_SUBSCRIBER_MODE"][number]; // keeps the utf8 rendering, which is what `channels()` hands back. type ChannelSet = Map; -function channelKey(channel: string | Buffer): string { - return (Buffer.isBuffer(channel) ? channel : Buffer.from(channel)).toString( - "latin1" - ); +function channelKey(channel: ChannelName): string { + return ( + Buffer.isBuffer(channel) ? channel : Buffer.from(String(channel)) + ).toString("latin1"); } -function channelName(channel: string | Buffer): string { - return Buffer.isBuffer(channel) ? channel.toString() : channel; +function channelName(channel: ChannelName): string { + return Buffer.isBuffer(channel) ? channel.toString() : String(channel); } /** @@ -31,11 +38,11 @@ export default class SubscriptionSet { ssubscribe: new Map(), }; - add(set: AddSet, channel: string | Buffer) { + add(set: AddSet, channel: ChannelName) { this.set[mapSet(set)].set(channelKey(channel), channelName(channel)); } - del(set: DelSet, channel: string | Buffer) { + del(set: DelSet, channel: ChannelName) { this.set[mapSet(set)].delete(channelKey(channel)); } diff --git a/test/unit/subscriberMode.ts b/test/unit/subscriberMode.ts index 1b1ebd29a..b901a5352 100644 --- a/test/unit/subscriberMode.ts +++ b/test/unit/subscriberMode.ts @@ -172,6 +172,30 @@ PROTOCOLS.forEach((protocol, index) => { expectSubscriptionStateCleared(redis, protocol); }); + // A server or proxy that answers SUBSCRIBE with a plain `+OK` instead of + // the usual three-element acknowledgement leaves `reply[1]` as one byte of + // that string rather than a channel name. Tracking that byte is harmless, + // but it must not throw: the subscription set runs inside the decoder's + // reply dispatch, so an exception there escapes as an uncaught error and + // the command it came from is never settled. + it("tracks a subscribe acknowledgement that is not an array", async () => { + server.handler = (argv) => { + const name = String(argv[0]).toLowerCase(); + if (name === "info") { + return "# Server\r\nredis_version:7.0.0\r\n"; + } + return "OK"; + }; + redis = new Redis({ port, protocol }); + await redis.subscribe("regular"); + + const { subscriber } = redis.condition; + expect(subscriber).to.not.equal(false); + // `"OK"[1]` decodes to the byte 0x4b, so the set records its decimal + // rendering, as it did before channel names were keyed by their bytes. + expect((subscriber as any).channels("subscribe")).to.eql(["75"]); + }); + // Channel names are binary safe, so the subscription set has to key them // by their bytes: `<80>` and `<81>` are both invalid utf8 and both render // as U+FFFD, so keying by that rendering collapses the two onto one entry From 00de7887748ba8f64af989ca34ecf26fbc5e4ec3 Mon Sep 17 00:00:00 2001 From: askalf Date: Fri, 18 Sep 2026 07:55:08 -0400 Subject: [PATCH 8/9] test: cover patterns and the count-and-set decision for subscriber mode Regular channels and patterns share one remaining count and shard channels have their own; the comments now say so instead of "same kind". New cases: pattern plus shard in both orders, a channel and a pattern sharing one count, a positive server count with an empty local set, and the fire-and-forget blocks in the callback tests now deliver a rejection to done. The byte-writing helper is inlined. --- lib/DataHandler.ts | 37 ++++----- test/unit/subscriberMode.ts | 160 +++++++++++++++++++++++++----------- 2 files changed, 130 insertions(+), 67 deletions(-) diff --git a/lib/DataHandler.ts b/lib/DataHandler.ts index b8c45c127..413085fb3 100644 --- a/lib/DataHandler.ts +++ b/lib/DataHandler.ts @@ -142,7 +142,10 @@ export default class DataHandler { // command response. Routing it by content would misinterpret replies that // merely look like pub/sub messages (e.g. an LRANGE result starting with // "message") and desync the command queue. - if (this.redis.condition.protocol !== 3 && this.handleSubscriberReply(reply)) { + if ( + this.redis.condition.protocol !== 3 && + this.handleSubscriberReply(reply) + ) { return; } @@ -209,9 +212,8 @@ export default class DataHandler { const count = reply[2] as string | Buffer | number; if (this.redis.condition.subscriber) { - // `""` is a valid channel name, so both guards test for a missing - // reply element rather than truthiness — skipping `del()` for it - // would keep the set non-empty and strand subscriber mode below. + // Empty channel names are valid; only a null or undefined reply + // element means no channel. const channel = reply[1] == null ? null : (reply[1] as ChannelName); if (channel !== null) { this.redis.condition.subscriber.del(replyType, channel); @@ -315,7 +317,8 @@ export default class DataHandler { case "sunsubscribe": case "unsubscribe": case "punsubscribe": { - // See the matching comment in `returnPush` about the empty channel name. + // Empty channel names are valid; only a null or undefined reply + // element means no channel. const channel = reply[1] == null ? null : (reply[1] as ChannelName); if (channel !== null) { this.redis.condition.subscriber.del(replyType, channel); @@ -392,22 +395,14 @@ export default class DataHandler { } } -// Both the count carried by the reply and the subscription set are partial -// views of the connection's subscriptions, and neither one alone answers -// "is this connection still in subscriber mode?": -// -// - The count only covers channels of the same kind as the command -// (SUNSUBSCRIBE reports remaining shard channels, and UNSUBSCRIBE / -// PUNSUBSCRIBE ignore shard channels), so it reaching zero says nothing -// about the other kinds. -// - The set only tracks what this connection subscribed to through ioredis, -// so it cannot see a subscription the server holds but never acknowledged -// to it. -// -// Requiring both covers each one's blind spot with the other: the count rules -// out a remaining same-kind subscription, and the set rules out the kinds the -// count ignores. A reply carrying no count is not authoritative about -// anything, so it does not hold the connection in subscriber mode on its own. +// The count carried by the reply and the subscription set are each a partial +// view of the connection's subscriptions. Regular channels and patterns share +// one count and shard channels have their own, so a count of zero says nothing +// about the other group; the set tracks only what this connection subscribed +// to through ioredis, so it cannot see a subscription the server holds but +// never acknowledged. Subscriber mode is left only when both are empty. A +// reply carrying no count is not authoritative and does not hold the +// connection in subscriber mode on its own. function leaveSubscriberModeIfDone( condition: Condition, count: string | Buffer | number diff --git a/test/unit/subscriberMode.ts b/test/unit/subscriberMode.ts index b901a5352..8f4de183c 100644 --- a/test/unit/subscriberMode.ts +++ b/test/unit/subscriberMode.ts @@ -1,15 +1,14 @@ import { expect } from "chai"; -import { Socket } from "net"; import MockServer, { pubSubReply, rawPubSubReply, } 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. +// Redis counts regular channels and patterns together and shard channels +// separately, so the count carried by an unsubscribe reply describes only its +// own group. Dropping the subscriber state on that count alone loses +// subscriptions of the other group that are still active on the connection. const PROTOCOLS = [3, 2] as const; // `Redis#mode` only reports the subscriber state under RESP2. With RESP3 push @@ -38,15 +37,20 @@ PROTOCOLS.forEach((protocol, index) => { 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") { + // Each group reports its own remaining count. + if ( + name === "subscribe" || + name === "ssubscribe" || + name === "psubscribe" + ) { 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); + if ( + name === "sunsubscribe" || + name === "unsubscribe" || + name === "punsubscribe" + ) { + return pubSubReply(protocol, name, argv[1], 0); } return "OK"; }); @@ -83,7 +87,7 @@ PROTOCOLS.forEach((protocol, index) => { await redis.ssubscribe("shard"); await redis.sunsubscribe("shard"); server.broadcast(pubSubReply(protocol, "message", "regular", "hi")); - })(); + })().catch(done); }); it("keeps the shard subscription after unsubscribe", async () => { @@ -108,9 +112,82 @@ PROTOCOLS.forEach((protocol, index) => { expectSubscriptionStateCleared(redis, protocol); }); - // `""` is a legal channel name, and the reply naming it must still be - // removed from the subscription set — a truthiness check on the channel - // skips the removal and leaves the connection stuck in subscriber mode. + it("keeps the pattern subscription after sunsubscribe", async () => { + redis = new Redis({ port, protocol }); + await redis.psubscribe("reg*"); + await redis.ssubscribe("shard"); + await redis.sunsubscribe("shard"); + + const { subscriber } = redis.condition; + expect(subscriber).to.not.equal(false); + expect((subscriber as any).channels("psubscribe")).to.eql(["reg*"]); + }); + + it("keeps the shard subscription after punsubscribe", async () => { + redis = new Redis({ port, protocol }); + await redis.ssubscribe("shard"); + await redis.psubscribe("reg*"); + await redis.punsubscribe("reg*"); + + const { subscriber } = redis.condition; + expect(subscriber).to.not.equal(false); + expect((subscriber as any).channels("ssubscribe")).to.eql(["shard"]); + }); + + // Regular channels and patterns share one count: unsubscribing the + // channel reports the pattern as remaining, and only the pattern's own + // acknowledgement brings that count to zero. + it("leaves subscriber mode only when the shared channel and pattern count reaches zero", async () => { + let shared = 0; + server.handler = (argv) => { + const name = String(argv[0]).toLowerCase(); + if (name === "info") { + return "# Server\r\nredis_version:7.0.0\r\n"; + } + if (name === "subscribe" || name === "psubscribe") { + shared += 1; + return pubSubReply(protocol, name, argv[1], shared); + } + if (name === "unsubscribe" || name === "punsubscribe") { + shared -= 1; + return pubSubReply(protocol, name, argv[1], shared); + } + return "OK"; + }; + redis = new Redis({ port, protocol }); + await redis.subscribe("regular"); + await redis.psubscribe("reg*"); + await redis.unsubscribe("regular"); + expect(redis.condition.subscriber).to.not.equal(false); + + await redis.punsubscribe("reg*"); + expectSubscriptionStateCleared(redis, protocol); + }); + + // The set can be empty while the server still counts a subscription this + // connection never tracked: the count alone keeps subscriber mode. + it("stays in subscriber mode on a positive count with an empty set", async () => { + server.handler = (argv) => { + const name = String(argv[0]).toLowerCase(); + if (name === "info") { + return "# Server\r\nredis_version:7.0.0\r\n"; + } + if (name === "subscribe") { + return pubSubReply(protocol, name, argv[1], 2); + } + if (name === "unsubscribe") { + return pubSubReply(protocol, name, argv[1], 1); + } + return "OK"; + }; + redis = new Redis({ port, protocol }); + await redis.subscribe("regular"); + await redis.unsubscribe("regular"); + + expect((redis.condition.subscriber as any).isEmpty()).to.equal(true); + expect(redis.condition.subscriber).to.not.equal(false); + }); + it("clears the subscription state after unsubscribing an empty channel name", async () => { redis = new Redis({ port, protocol }); await redis.subscribe(""); @@ -155,7 +232,7 @@ PROTOCOLS.forEach((protocol, index) => { await redis.ssubscribe("shard"); await redis.sunsubscribe("shard"); server.broadcast(pubSubReply(protocol, "message", "__proto__", "hi")); - })(); + })().catch(done); }); // The removal has to reach the stored key as well: the channel is tracked @@ -174,10 +251,9 @@ PROTOCOLS.forEach((protocol, index) => { // A server or proxy that answers SUBSCRIBE with a plain `+OK` instead of // the usual three-element acknowledgement leaves `reply[1]` as one byte of - // that string rather than a channel name. Tracking that byte is harmless, - // but it must not throw: the subscription set runs inside the decoder's - // reply dispatch, so an exception there escapes as an uncaught error and - // the command it came from is never settled. + // that string. The subscription set runs inside the decoder's reply + // dispatch, so it must not throw on it; it records the byte's decimal + // rendering and the command settles. it("tracks a subscribe acknowledgement that is not an array", async () => { server.handler = (argv) => { const name = String(argv[0]).toLowerCase(); @@ -191,8 +267,7 @@ PROTOCOLS.forEach((protocol, index) => { const { subscriber } = redis.condition; expect(subscriber).to.not.equal(false); - // `"OK"[1]` decodes to the byte 0x4b, so the set records its decimal - // rendering, as it did before channel names were keyed by their bytes. + // `"OK"[1]` decodes to the byte 0x4b. expect((subscriber as any).channels("subscribe")).to.eql(["75"]); }); @@ -205,19 +280,10 @@ PROTOCOLS.forEach((protocol, index) => { const second = Buffer.from([0x81]); // Replies naming these channels have to keep the raw bytes, which the - // mock server's string-based writer cannot do, so they are written to - // the socket directly and the automatic reply is suppressed. - function replyWithBytes( - socket: Socket, - type: string, - channel: Buffer, - count: number - ) { - socket.write(rawPubSubReply(protocol, type, channel, count)); - } - - // Both SUBSCRIBEs are acknowledged with the channel the client asked - // for; the server counts them separately, so the second reports 2. + // mock server's string-based writer cannot do, so the handler writes + // them to the socket itself and hangs the automatic reply. Both + // SUBSCRIBEs are acknowledged with the channel the client asked for; + // the server counts them separately, so the second reports 2. // UNSUBSCRIBE of the first then reports 1 remaining. function serveBinaryChannels(server: MockServer) { let subscribed = 0; @@ -229,18 +295,22 @@ PROTOCOLS.forEach((protocol, index) => { if (name === "subscribe") { subscribed += 1; flags.hang = true; - replyWithBytes( - socket, - "subscribe", - subscribed === 1 ? first : second, - subscribed + socket.write( + rawPubSubReply( + protocol, + "subscribe", + subscribed === 1 ? first : second, + subscribed + ) ); return undefined; } if (name === "unsubscribe") { subscribed -= 1; flags.hang = true; - replyWithBytes(socket, "unsubscribe", first, subscribed); + socket.write( + rawPubSubReply(protocol, "unsubscribe", first, subscribed) + ); return undefined; } // Shard channels are counted separately by the server, so the shard @@ -305,11 +375,9 @@ PROTOCOLS.forEach((protocol, index) => { server .getAllClients()[0] .write(rawPubSubReply(protocol, "message", second, "hi")); - })(); + })().catch(done); }); - // Both names have to stay individually tracked, or the reconnect path - // resubscribes to only one of them. it("tracks both names as separate channels", async () => { serveBinaryChannels(server); redis = new Redis({ port, protocol }); @@ -335,7 +403,7 @@ PROTOCOLS.forEach((protocol, index) => { server .getAllClients()[0] .write(rawPubSubReply(protocol, "message", second, "hi")); - })(); + })().catch(done); }); }); }); From 8f949b486bc90f469d819c3b5204d4476fb76a0c Mon Sep 17 00:00:00 2001 From: askalf <263217947+askalf@users.noreply.github.com> Date: Mon, 5 Oct 2026 14:38:07 -0400 Subject: [PATCH 9/9] test: cut the subscriber mode tests down to the cases the fix needs --- test/helpers/mock_server.ts | 32 --- test/unit/subscriberMode.ts | 374 +++--------------------------------- 2 files changed, 25 insertions(+), 381 deletions(-) diff --git a/test/helpers/mock_server.ts b/test/helpers/mock_server.ts index 1b1de6287..ab1633b5e 100644 --- a/test/helpers/mock_server.ts +++ b/test/helpers/mock_server.ts @@ -222,35 +222,3 @@ export function pubSubReply( } return [type, channel, value]; } - -// `pubSubReply` goes through the string-based writer, which cannot carry a -// channel name that is not valid utf8. This builds the same reply as raw bytes -// instead, for the binary-safe channel names, and returns a Buffer to write to -// the socket directly (RESP3 push frame or RESP2 array, as the protocol asks). -export function rawPubSubReply( - protocol: MockServerProtocol, - type: string, - channel: string | Buffer, - value: string | number = 1 -): Buffer { - const channelBytes = Buffer.isBuffer(channel) - ? channel - : Buffer.from(channel); - const parts = [ - Buffer.from(`${protocol === 3 ? ">" : "*"}3\r\n`), - blobString(Buffer.from(type)), - blobString(channelBytes), - typeof value === "number" - ? Buffer.from(`:${value}\r\n`) - : blobString(Buffer.from(value)), - ]; - return Buffer.concat(parts); -} - -function blobString(payload: Buffer): Buffer { - return Buffer.concat([ - Buffer.from(`$${payload.length}\r\n`), - payload, - Buffer.from("\r\n"), - ]); -} diff --git a/test/unit/subscriberMode.ts b/test/unit/subscriberMode.ts index 8f4de183c..73c09893d 100644 --- a/test/unit/subscriberMode.ts +++ b/test/unit/subscriberMode.ts @@ -1,9 +1,7 @@ import { expect } from "chai"; -import MockServer, { - pubSubReply, - rawPubSubReply, -} from "../helpers/mock_server"; +import MockServer, { pubSubReply } from "../helpers/mock_server"; import Redis from "../../lib/Redis"; +import SubscriptionSet from "../../lib/SubscriptionSet"; // Redis counts regular channels and patterns together and shard channels // separately, so the count carried by an unsubscribe reply describes only its @@ -11,20 +9,6 @@ import Redis from "../../lib/Redis"; // subscriptions of the other group that are still active on the connection. const PROTOCOLS = [3, 2] as const; -// `Redis#mode` only reports the subscriber state under RESP2. With RESP3 push -// messages share the connection, so it reads `"normal"` even while subscribed -// and asserting on it would pass whether or not the subscription state was -// cleaned up. `condition.subscriber` is the check that discriminates on both. -function expectSubscriptionStateCleared( - redis: Redis, - protocol: typeof PROTOCOLS[number] -) { - expect(redis.condition.subscriber).to.equal(false); - if (protocol === 2) { - expect(redis.mode).to.equal("normal"); - } -} - PROTOCOLS.forEach((protocol, index) => { describe(`subscriber mode across channel kinds (RESP${protocol})`, () => { const port = 17940 + index; @@ -38,18 +22,10 @@ PROTOCOLS.forEach((protocol, index) => { return "# Server\r\nredis_version:7.0.0\r\n"; } // Each group reports its own remaining count. - if ( - name === "subscribe" || - name === "ssubscribe" || - name === "psubscribe" - ) { + if (name === "subscribe" || name === "ssubscribe") { return pubSubReply(protocol, name, argv[1], 1); } - if ( - name === "sunsubscribe" || - name === "unsubscribe" || - name === "punsubscribe" - ) { + if (name === "unsubscribe" || name === "sunsubscribe") { return pubSubReply(protocol, name, argv[1], 0); } return "OK"; @@ -75,33 +51,6 @@ PROTOCOLS.forEach((protocol, index) => { 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")); - })().catch(done); - }); - - 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("clears the subscription state once every kind is unsubscribed", async () => { redis = new Redis({ port, protocol }); await redis.subscribe("regular"); @@ -109,302 +58,29 @@ PROTOCOLS.forEach((protocol, index) => { await redis.unsubscribe("regular"); await redis.sunsubscribe("shard"); - expectSubscriptionStateCleared(redis, protocol); - }); - - it("keeps the pattern subscription after sunsubscribe", async () => { - redis = new Redis({ port, protocol }); - await redis.psubscribe("reg*"); - await redis.ssubscribe("shard"); - await redis.sunsubscribe("shard"); - - const { subscriber } = redis.condition; - expect(subscriber).to.not.equal(false); - expect((subscriber as any).channels("psubscribe")).to.eql(["reg*"]); - }); - - it("keeps the shard subscription after punsubscribe", async () => { - redis = new Redis({ port, protocol }); - await redis.ssubscribe("shard"); - await redis.psubscribe("reg*"); - await redis.punsubscribe("reg*"); - - const { subscriber } = redis.condition; - expect(subscriber).to.not.equal(false); - expect((subscriber as any).channels("ssubscribe")).to.eql(["shard"]); - }); - - // Regular channels and patterns share one count: unsubscribing the - // channel reports the pattern as remaining, and only the pattern's own - // acknowledgement brings that count to zero. - it("leaves subscriber mode only when the shared channel and pattern count reaches zero", async () => { - let shared = 0; - server.handler = (argv) => { - const name = String(argv[0]).toLowerCase(); - if (name === "info") { - return "# Server\r\nredis_version:7.0.0\r\n"; - } - if (name === "subscribe" || name === "psubscribe") { - shared += 1; - return pubSubReply(protocol, name, argv[1], shared); - } - if (name === "unsubscribe" || name === "punsubscribe") { - shared -= 1; - return pubSubReply(protocol, name, argv[1], shared); - } - return "OK"; - }; - redis = new Redis({ port, protocol }); - await redis.subscribe("regular"); - await redis.psubscribe("reg*"); - await redis.unsubscribe("regular"); - expect(redis.condition.subscriber).to.not.equal(false); - - await redis.punsubscribe("reg*"); - expectSubscriptionStateCleared(redis, protocol); - }); - - // The set can be empty while the server still counts a subscription this - // connection never tracked: the count alone keeps subscriber mode. - it("stays in subscriber mode on a positive count with an empty set", async () => { - server.handler = (argv) => { - const name = String(argv[0]).toLowerCase(); - if (name === "info") { - return "# Server\r\nredis_version:7.0.0\r\n"; - } - if (name === "subscribe") { - return pubSubReply(protocol, name, argv[1], 2); - } - if (name === "unsubscribe") { - return pubSubReply(protocol, name, argv[1], 1); - } - return "OK"; - }; - redis = new Redis({ port, protocol }); - await redis.subscribe("regular"); - await redis.unsubscribe("regular"); - - expect((redis.condition.subscriber as any).isEmpty()).to.equal(true); - expect(redis.condition.subscriber).to.not.equal(false); - }); - - it("clears the subscription state after unsubscribing an empty channel name", async () => { - redis = new Redis({ port, protocol }); - await redis.subscribe(""); - await redis.unsubscribe(""); - - expectSubscriptionStateCleared(redis, protocol); - }); - - it("clears the subscription state after sunsubscribing an empty shard channel name", async () => { - redis = new Redis({ port, protocol }); - await redis.ssubscribe(""); - await redis.sunsubscribe(""); - - expectSubscriptionStateCleared(redis, protocol); - }); - - // Channel names are chosen by the application, so they can collide with - // the names inherited from `Object.prototype`. `__proto__` in particular - // is never stored as an own key on a plain object, so the subscription - // set would report itself empty while the channel is still subscribed. - it("keeps a channel named __proto__ subscribed after sunsubscribe", async () => { - redis = new Redis({ port, protocol }); - await redis.subscribe("__proto__"); - 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(["__proto__"]); - expect((subscriber as any).channels("ssubscribe")).to.eql([]); - }); - - it("delivers messages on a channel named __proto__ after sunsubscribe", (done) => { - redis = new Redis({ port, protocol }); - redis.on("message", (channel, message) => { - expect(channel).to.eql("__proto__"); - expect(message).to.eql("hi"); - done(); - }); - (async () => { - await redis.subscribe("__proto__"); - await redis.ssubscribe("shard"); - await redis.sunsubscribe("shard"); - server.broadcast(pubSubReply(protocol, "message", "__proto__", "hi")); - })().catch(done); - }); - - // The removal has to reach the stored key as well: the channel is tracked - // while subscribed, and unsubscribing it empties the set and leaves the - // connection out of subscriber mode. - it("tracks and then clears a subscription to __proto__", async () => { - redis = new Redis({ port, protocol }); - await redis.subscribe("__proto__"); - expect((redis.condition.subscriber as any).channels("subscribe")).to.eql([ - "__proto__", - ]); - - await redis.unsubscribe("__proto__"); - expectSubscriptionStateCleared(redis, protocol); - }); - - // A server or proxy that answers SUBSCRIBE with a plain `+OK` instead of - // the usual three-element acknowledgement leaves `reply[1]` as one byte of - // that string. The subscription set runs inside the decoder's reply - // dispatch, so it must not throw on it; it records the byte's decimal - // rendering and the command settles. - it("tracks a subscribe acknowledgement that is not an array", async () => { - server.handler = (argv) => { - const name = String(argv[0]).toLowerCase(); - if (name === "info") { - return "# Server\r\nredis_version:7.0.0\r\n"; - } - return "OK"; - }; - redis = new Redis({ port, protocol }); - await redis.subscribe("regular"); - - const { subscriber } = redis.condition; - expect(subscriber).to.not.equal(false); - // `"OK"[1]` decodes to the byte 0x4b. - expect((subscriber as any).channels("subscribe")).to.eql(["75"]); - }); - - // Channel names are binary safe, so the subscription set has to key them - // by their bytes: `<80>` and `<81>` are both invalid utf8 and both render - // as U+FFFD, so keying by that rendering collapses the two onto one entry - // and unsubscribing either one drops both from the set. - describe("with binary channel names that share a utf8 rendering", () => { - const first = Buffer.from([0x80]); - const second = Buffer.from([0x81]); - - // Replies naming these channels have to keep the raw bytes, which the - // mock server's string-based writer cannot do, so the handler writes - // them to the socket itself and hangs the automatic reply. Both - // SUBSCRIBEs are acknowledged with the channel the client asked for; - // the server counts them separately, so the second reports 2. - // UNSUBSCRIBE of the first then reports 1 remaining. - function serveBinaryChannels(server: MockServer) { - let subscribed = 0; - server.handler = (argv, socket, flags) => { - const name = String(argv[0]).toLowerCase(); - if (name === "info") { - return "# Server\r\nredis_version:7.0.0\r\n"; - } - if (name === "subscribe") { - subscribed += 1; - flags.hang = true; - socket.write( - rawPubSubReply( - protocol, - "subscribe", - subscribed === 1 ? first : second, - subscribed - ) - ); - return undefined; - } - if (name === "unsubscribe") { - subscribed -= 1; - flags.hang = true; - socket.write( - rawPubSubReply(protocol, "unsubscribe", first, subscribed) - ); - return undefined; - } - // Shard channels are counted separately by the server, so the shard - // acknowledgements carry their own count and say nothing about the - // regular channels that are still subscribed. - if (name === "ssubscribe") { - return pubSubReply(protocol, "ssubscribe", argv[1], 1); - } - if (name === "sunsubscribe") { - return pubSubReply(protocol, "sunsubscribe", argv[1], 0); - } - return "OK"; - }; + expect(redis.condition.subscriber).to.equal(false); + // `Redis#mode` reports the subscriber state under RESP2 only. + if (protocol === 2) { + expect(redis.mode).to.equal("normal"); } + }); + }); +}); - it("keeps subscriber mode while the other one is still subscribed", async () => { - serveBinaryChannels(server); - redis = new Redis({ port, protocol }); - await redis.subscribe(first); - await redis.subscribe(second); - await redis.unsubscribe(first); - - expect(redis.condition.subscriber).to.not.equal(false); - if (protocol === 2) { - expect(redis.mode).to.equal("subscriber"); - } - }); - - // The count in an acknowledgement only describes its own kind, so a - // shard count of 0 says nothing about the two regular channels. If the - // set has collapsed them onto one key it already reads empty by then, - // and the connection leaves subscriber mode with a channel still live. - it("keeps subscriber mode when a shard count of 0 arrives later", async () => { - serveBinaryChannels(server); - redis = new Redis({ port, protocol }); - await redis.subscribe(first); - await redis.subscribe(second); - await redis.unsubscribe(first); - await redis.ssubscribe("shard"); - await redis.sunsubscribe("shard"); - - expect(redis.condition.subscriber).to.not.equal(false); - if (protocol === 2) { - expect(redis.mode).to.equal("subscriber"); - } - }); - - it("still delivers messages after a later shard count of 0", (done) => { - serveBinaryChannels(server); - redis = new Redis({ port, protocol }); - redis.on("messageBuffer", (channel, message) => { - expect(channel).to.eql(second); - expect(message.toString()).to.equal("hi"); - done(); - }); - (async () => { - await redis.subscribe(first); - await redis.subscribe(second); - await redis.unsubscribe(first); - await redis.ssubscribe("shard"); - await redis.sunsubscribe("shard"); - server - .getAllClients()[0] - .write(rawPubSubReply(protocol, "message", second, "hi")); - })().catch(done); - }); - - it("tracks both names as separate channels", async () => { - serveBinaryChannels(server); - redis = new Redis({ port, protocol }); - await redis.subscribe(first); - await redis.subscribe(second); - - const { subscriber } = redis.condition; - expect((subscriber as any).channels("subscribe")).to.have.lengthOf(2); - }); +describe("SubscriptionSet", () => { + // `__proto__` is never stored as an own key on a plain object. + it("tracks a channel named __proto__", () => { + const set = new SubscriptionSet(); + set.add("subscribe", "__proto__"); + expect(set.isEmpty()).to.equal(false); + }); - it("still delivers messages on the one that is still subscribed", (done) => { - serveBinaryChannels(server); - redis = new Redis({ port, protocol }); - redis.on("messageBuffer", (channel, message) => { - expect(channel).to.eql(second); - expect(message.toString()).to.equal("hi"); - done(); - }); - (async () => { - await redis.subscribe(first); - await redis.subscribe(second); - await redis.unsubscribe(first); - server - .getAllClients()[0] - .write(rawPubSubReply(protocol, "message", second, "hi")); - })().catch(done); - }); - }); + // `<80>` and `<81>` are both invalid utf8 and render as the same string. + it("keeps binary channel names that share a utf8 rendering apart", () => { + const set = new SubscriptionSet(); + set.add("subscribe", Buffer.from([0x80])); + set.add("subscribe", Buffer.from([0x81])); + set.del("unsubscribe", Buffer.from([0x80])); + expect(set.isEmpty()).to.equal(false); }); });