OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
ralph(M26-001): Reference-count the shared Redis channel subscription so one socket closing cannot silence every other open socket
ralphf8145882909b
diff --git a/packages/redis/publisher.test.ts b/packages/redis/publisher.test.tsnew file mode 100644index 000000000..273cda4ee--- /dev/null+++ b/packages/redis/publisher.test.ts@@ -0,0 +1,241 @@+/**+ * M26-001 — F1: one subscriber leaving must not silence the others.+ *+ * `subscribeToPublishedEvent` hands every caller the SAME ioredis connection+ * (`getRedisSub()`, a process-wide singleton), so before this the unsubscribe+ * it returned took the channel away from the whole process. M23-003 measured+ * it on the `/live` sockets: close one, every other open socket received+ * nothing for 30 s. What is pinned here is the reference count — and, just as+ * important, that the LAST listener leaving still sends a real UNSUBSCRIBE, so+ * the fix cannot degrade into "never unsubscribe".+ *+ * No real Redis: a stand-in for the shared connection that records the wire+ * commands and can push a `message` event, which is the entire surface the+ * publisher uses.+ */++import { setSuperJson } from '@openpanel/shared';+import { beforeEach, describe, expect, it, vi } from 'vitest';++const EVENTS_CHANNEL = 'events:batch';+const ORGANIZATION_CHANNEL = 'organization:subscription_updated';++const redisSub = vi.hoisted(() => {+ const messageHandlers: Array<(channel: string, message: string) => void> = [];+ const commands: string[] = [];++ return {+ commands,+ messageHandlerCount: () => messageHandlers.length,+ emit(channel: string, message: string) {+ for (const handler of [...messageHandlers]) {+ handler(channel, message);+ }+ },+ on(event: string, handler: (channel: string, message: string) => void) {+ if (event === 'message') {+ messageHandlers.push(handler);+ }+ return redisSub;+ },+ off(event: string, handler: (channel: string, message: string) => void) {+ if (event === 'message') {+ const index = messageHandlers.indexOf(handler);+ if (index !== -1) {+ messageHandlers.splice(index, 1);+ }+ }+ return redisSub;+ },+ subscribe(channel: string) {+ commands.push(`subscribe ${channel}`);+ return Promise.resolve(1);+ },+ unsubscribe(channel: string) {+ commands.push(`unsubscribe ${channel}`);+ return Promise.resolve(0);+ },+ };+});++vi.mock('./redis', () => ({+ getRedisPub: () => redisSub,+ getRedisSub: () => redisSub,+}));++const { subscribeToPublishedEvent } = await import('./publisher');++const publishBatch = (projectId: string, count: number) =>+ redisSub.emit(EVENTS_CHANNEL, setSuperJson({ projectId, count }));++beforeEach(() => {+ redisSub.commands.length = 0;+});++describe('subscribeToPublishedEvent reference counting', () => {+ it('keeps delivering to the survivors when one listener drops', () => {+ const survivor = vi.fn();+ const leaver = vi.fn();++ const unsubscribeSurvivor = subscribeToPublishedEvent(+ 'events',+ 'batch',+ survivor+ );+ const unsubscribeLeaver = subscribeToPublishedEvent(+ 'events',+ 'batch',+ leaver+ );++ unsubscribeLeaver();+ publishBatch('project-a', 3);++ expect(survivor).toHaveBeenCalledWith({ projectId: 'project-a', count: 3 });+ expect(leaver).not.toHaveBeenCalled();+ expect(redisSub.commands).not.toContain(`unsubscribe ${EVENTS_CHANNEL}`);++ unsubscribeSurvivor();+ });++ it('sends a real UNSUBSCRIBE when the last listener leaves', () => {+ const first = vi.fn();+ const second = vi.fn();++ const unsubscribeFirst = subscribeToPublishedEvent(+ 'events',+ 'batch',+ first+ );+ const unsubscribeSecond = subscribeToPublishedEvent(+ 'events',+ 'batch',+ second+ );++ unsubscribeFirst();+ expect(redisSub.commands).not.toContain(`unsubscribe ${EVENTS_CHANNEL}`);++ unsubscribeSecond();+ expect(redisSub.commands).toContain(`unsubscribe ${EVENTS_CHANNEL}`);++ publishBatch('project-a', 1);+ expect(first).not.toHaveBeenCalled();+ expect(second).not.toHaveBeenCalled();+ });++ it('does not re-SUBSCRIBE when a listener joins a channel that has one', () => {+ const first = vi.fn();+ const second = vi.fn();++ const unsubscribeFirst = subscribeToPublishedEvent(+ 'events',+ 'batch',+ first+ );+ expect(redisSub.commands).toEqual([`subscribe ${EVENTS_CHANNEL}`]);++ const unsubscribeSecond = subscribeToPublishedEvent(+ 'events',+ 'batch',+ second+ );+ expect(redisSub.commands).toEqual([`subscribe ${EVENTS_CHANNEL}`]);++ publishBatch('project-a', 7);+ expect(first).toHaveBeenCalledTimes(1);+ expect(second).toHaveBeenCalledTimes(1);++ unsubscribeFirst();+ unsubscribeSecond();+ });++ it('installs exactly one message handler on the shared connection', () => {+ const before = redisSub.messageHandlerCount();+ const unsubscribers = [+ subscribeToPublishedEvent('events', 'batch', vi.fn()),+ subscribeToPublishedEvent('events', 'batch', vi.fn()),+ subscribeToPublishedEvent(+ 'organization',+ 'subscription_updated',+ vi.fn()+ ),+ ];++ expect(redisSub.messageHandlerCount()).toBe(before);++ for (const unsubscribe of unsubscribers) {+ unsubscribe();+ }+ });+});++describe('subscribeToPublishedEvent unsubscribe idempotence', () => {+ it('is safe to call twice and decrements at most once', () => {+ const survivor = vi.fn();+ const leaver = vi.fn();++ const unsubscribeSurvivor = subscribeToPublishedEvent(+ 'events',+ 'batch',+ survivor+ );+ const unsubscribeLeaver = subscribeToPublishedEvent(+ 'events',+ 'batch',+ leaver+ );++ unsubscribeLeaver();+ unsubscribeLeaver();+ unsubscribeLeaver();++ expect(redisSub.commands).not.toContain(`unsubscribe ${EVENTS_CHANNEL}`);++ publishBatch('project-a', 2);+ expect(survivor).toHaveBeenCalledTimes(1);++ // The count reached zero exactly once, so the survivor leaving still tears+ // the channel down rather than leaving it dangling at a negative count.+ unsubscribeSurvivor();+ expect(redisSub.commands).toContain(`unsubscribe ${EVENTS_CHANNEL}`);+ });+});++describe('subscribeToPublishedEvent channel isolation', () => {+ it('keeps listeners on different channels independent', () => {+ const onBatch = vi.fn();+ const onOrganization = vi.fn();++ const unsubscribeBatch = subscribeToPublishedEvent(+ 'events',+ 'batch',+ onBatch+ );+ const unsubscribeOrganization = subscribeToPublishedEvent(+ 'organization',+ 'subscription_updated',+ onOrganization+ );++ expect(redisSub.commands).toEqual([+ `subscribe ${EVENTS_CHANNEL}`,+ `subscribe ${ORGANIZATION_CHANNEL}`,+ ]);++ unsubscribeOrganization();+ expect(redisSub.commands).toContain(`unsubscribe ${ORGANIZATION_CHANNEL}`);+ expect(redisSub.commands).not.toContain(`unsubscribe ${EVENTS_CHANNEL}`);++ publishBatch('project-a', 5);+ redisSub.emit(+ ORGANIZATION_CHANNEL,+ setSuperJson({ organizationId: 'org-1' })+ );++ expect(onBatch).toHaveBeenCalledWith({ projectId: 'project-a', count: 5 });+ expect(onOrganization).not.toHaveBeenCalled();++ unsubscribeBatch();+ });+});diff --git a/packages/redis/publisher.ts b/packages/redis/publisher.tsindex 2c2c6ccb9..107626bcc 100644--- a/packages/redis/publisher.ts+++ b/packages/redis/publisher.ts@@ -43,6 +43,85 @@ export function parsePublishedEvent<Channel extends keyof IPublishChannels>(return getSuperJson<IPublishChannels[Channel][typeof _type]>(message)!;}+type ChannelMessageListener = (message: string) => void;++/**+ * Every subscriber in the process shares ONE ioredis connection+ * (`getRedisSub()`, a module-level singleton). SUBSCRIBE and UNSUBSCRIBE on it+ * are process-wide, so a per-subscriber UNSUBSCRIBE silences every other+ * subscriber on that channel — measured by M23-003 as F1: closing one `/live`+ * socket left every other open socket receiving nothing for 30 s, across+ * routes and across projects, because `/live/events` and `/live/visitors` both+ * ride `events:batch`.+ *+ * So the channel is reference-counted here rather than at each call site: the+ * wire SUBSCRIBE happens only on 0 -> 1 and the wire UNSUBSCRIBE only on+ * 1 -> 0. One `message` handler is installed on the shared connection for the+ * whole process and dispatches by channel name, which also keeps the+ * connection off Node's max-listeners warning as sockets accumulate.+ *+ * RECONNECT NEEDS NO HANDLING HERE. ioredis re-issues SUBSCRIBE for every+ * channel it believes it holds when the connection comes back+ * (`ioredis/built/redis/event_handler.js:254`, gated on `autoResubscribe`,+ * which defaults to `true` in `redis/RedisOptions.js:45` and is not overridden+ * for this client). Since the wire commands stay one-to-one with the 0 -> 1+ * and 1 -> 0 edges, ioredis's own set stays exactly the set of channels with a+ * non-zero count. Confirmed live against the local server on 2026-09-15:+ * destroying the socket under a subscribed client and publishing after the+ * reconnect still delivered, with no re-subscribe from here.+ */+const listenersByChannel = new Map<string, Set<ChannelMessageListener>>();++let isDispatcherInstalled = false;++// Deleting a not-yet-visited entry mid-iteration skips it, which is what an+// unsubscribe from inside a callback should do.+function dispatchToChannelListeners(channel: string, message: string) {+ const listeners = listenersByChannel.get(channel);+ if (!listeners) {+ return;+ }+ for (const listener of listeners) {+ listener(message);+ }+}++function installDispatcherOnce() {+ if (isDispatcherInstalled) {+ return;+ }+ isDispatcherInstalled = true;+ getRedisSub().on('message', dispatchToChannelListeners);+}++function addChannelListener(channel: string, listener: ChannelMessageListener) {+ installDispatcherOnce();+ const listeners = listenersByChannel.get(channel);+ if (listeners) {+ listeners.add(listener);+ return;+ }++ listenersByChannel.set(channel, new Set([listener]));+ getRedisSub().subscribe(channel);+}++function removeChannelListener(+ channel: string,+ listener: ChannelMessageListener,+) {+ const listeners = listenersByChannel.get(channel);+ if (!listeners?.delete(listener)) {+ return;+ }+ if (listeners.size > 0) {+ return;+ }++ listenersByChannel.delete(channel);+ getRedisSub().unsubscribe(channel);+}+export function subscribeToPublishedEvent<Channel extends keyof IPublishChannels,>(@@ -51,22 +130,23 @@ export function subscribeToPublishedEvent<callback: (event: IPublishChannels[Channel][typeof type]) => void,) {const subscribeChannel = getSubscribeChannel(channel, type);- getRedisSub().subscribe(subscribeChannel);-- const message = (messageChannel: string, message: string) => {- if (subscribeChannel === messageChannel) {- const event = parsePublishedEvent(channel, type, message);- if (event) {- callback(event);- }++ const listener: ChannelMessageListener = (message) => {+ const event = parsePublishedEvent(channel, type, message);+ if (event) {+ callback(event);}};- getRedisSub().on('message', message);+ addChannelListener(subscribeChannel, listener);+ let isReleased = false;return () => {- getRedisSub().unsubscribe(subscribeChannel);- getRedisSub().off('message', message);+ if (isReleased) {+ return;+ }+ isReleased = true;+ removeChannelListener(subscribeChannel, listener);};}