Skip to content

fix(cluster): emit sharded subscriber connect errors silently - #2241

Merged
PavelPashov merged 1 commit into
redis:mainfrom
maxymlyskov:fix/report-sharded-subscriber-connect
Oct 22, 2026
Merged

PavelPashov merged 1 commit into
redis:mainfrom
maxymlyskov:fix/report-sharded-subscriber-connect

Conversation

@maxymlyskov

@maxymlyskov maxymlyskov commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Small, isolated bug fix under the carve-out in .github/CONTRIBUTING.md, so I didn't open an issue first. It's 1 line in lib/cluster/index.ts.

If the master that owns a shard channel's slot refuses connections, cluster.ssubscribe() on that channel kills the process when the Cluster has no error listener. README.md:1445 says the Cluster error event "is emitted silently (only emitting if there's at least one listener)", so I'd expect a log line there.

Cause: ssubscribe() asks the subscriber group for the shard's subscriber, and an idle one gets started (ClusterSubscriberGroup.ts#L50-L66). Its connection has retryStrategy: null, so a refused connection makes start() reject (ShardedSubscriber.ts#L116-L124) and the group emits subscriberConnectFailed with a backoff delay (ClusterSubscriberGroup.ts#L475-L500).

The Cluster's handler for that event calls this.emit("error", error) (index.ts#L1468-L1476). With 0 listeners, EventEmitter throws. That throw happens inside the group's .catch() callback, so it comes out as an unhandled rejection of Connection is closed. and Node's default --unhandled-rejections=throw ends the process. The setTimeout that would retry after the backoff never runs.

connect() already reports ClusterAllFailedError the quiet way, with Redis.prototype.silentEmit.call(this, "error", err) (index.ts#L396-L399). silentEmit emits when someone listens and otherwise logs [ioredis] Unhandled error event: with the stack.

Fix: the subscriberConnectFailed handler uses the same silentEmit call. With a listener, the error is delivered as before.

One behaviour change to be upfront about. Like connect()'s call, it now drops the error when the cluster status is end, and while manuallyClosing for Connection is closed. and connect/read syscall errors. Those are silentEmit's own rules.

I ran a probe against a real Redis 8.6 cluster (redislabs/client-libs-test:custom-30445126297-debian, 3 masters + 3 replicas on 3000-3005, reached through natMap port-forwards on 4000-4005). It makes the master for the channel's slot unreachable, then calls ssubscribe with no error listener. 3 runs each; the first run of each below:

## built main 8526785
[  114ms] ready; channel huntb:scf:{a} -> 127.0.0.1:4002 (real 3002); mode = nolistener
[  115ms] the master owning the slot is now unreachable; calling ssubscribe
[  119ms] ACTUAL:   uncaught exception "Connection is closed." thrown from the Cluster "error" emit:
[  120ms] thrown by Cluster#emit("error") with 0 listeners: yes
RESULT: BUG REPRODUCED
## built fix
[  116ms] ready; channel huntb:scf:{a} -> 127.0.0.1:4002 (real 3002); mode = nolistener
[  117ms] the master owning the slot is now unreachable; calling ssubscribe
[ 2357ms] ssubscribe -> rejected: Too many Cluster redirections. Last error: Error: Connection is closed.
[ 4369ms] ACTUAL: no crash
RESULT: NOT REPRODUCED

On main all 3 runs died within 10 ms of the call. With the fix, in all 3 runs ssubscribe rejects with Too many Cluster redirections about 2 s later and the process is still up at ~4 s.

Tests: one new case in test/functional/cluster/spub_ssub.ts (+31). A MockServer on 30001 answers CLUSTER SLOTS with 8192-16383 on 30002, where nothing listens. The test asserts console.error got [ioredis] Unhandled error event: and a stack containing Connection is closed.. On main:

  cluster:spub/ssub
mocha test saw unexpected unhandledRejection Error: Connection is closed.
    at EventEmitter.connectionCloseHandler (/app/lib/Redis.ts:436:20)
    at Object.onceWrapper (node:events:634:26)
    at EventEmitter.emit (node:events:519:28)
    at EventEmitter.emit (node:domain:489:12)
    at processTicksAndRejections (node:internal/process/task_queues:84:11)
    1) logs a sharded subscriber connect error instead of throwing when nothing listens for errors


  0 passing (126ms)
  1 failing

  1) cluster:spub/ssub
       logs a sharded subscriber connect error instead of throwing when nothing listens for errors:
     Uncaught Error: mocha test saw unexpected unhandledRejection: Error: Connection is closed.
      at process.<anonymous> (test/helpers/global.ts:53:9)

On this branch it passes 3/3 (~100 ms). Deleting the silentEmit line makes it fail again.

  • npm run test:js (Redis 8.10.2, Node 22): 2909 passing, 25 pending, 1 failing. The failure is autoPipelining for single node › should handle large pipelines without RangeError, an 8000 ms timeout. Run alone on this branch it failed once, then passed 3/3 (4-7 s); on main it passes (5 s). It's a single-node test that never constructs a Cluster, so I don't think this change touches it.
  • npm run test:cluster against the 6-node cluster above: 71 passing.
  • npm run build passes. npm run lint: 0 errors, the 23 warnings already on main. prettier --check is clean on both files and tsc --noEmit reports 0 errors on main and the branch.

Left out on purpose: with an error listener attached, the failed subscriber's end emits -node, which refreshes the slots right away and restarts the subscriber, skipping the 200-2000 ms backoff. In the probe that was ~80-105 errors and ~120-152 slot refreshes in 3 s. Fixing it changes the -node lifecycle, so I'd rather raise it in an issue first and keep this PR to one bug.


Note

Low Risk
Single-line behavioral alignment with existing silentEmit usage on Cluster connect failures; no auth or data-path changes.

Overview
When a sharded subscriber cannot connect (e.g. the slot owner refuses the connection), the Cluster subscriberConnectFailed handler now reports the failure via Redis.prototype.silentEmit instead of a plain emit("error"). That matches connect() and the documented “silent error” behavior: with an error listener the event is delivered as before; with none, ioredis logs [ioredis] Unhandled error event: instead of throwing and killing the process, so the backoff refreshSlotsCache retry can still run.

A functional test drives ssubscribe against a mock slot owner with no listener and asserts the console log path. silentEmit’s own rules also suppress some errors while the cluster is end or manually closing.

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

When a sharded subscriber cannot connect, the subscriberConnectFailed
handler called this.emit("error"), which throws when nothing listens for
"error" and crashes the process. Every other Cluster error path uses
silentEmit, and the README documents the Cluster error event as emitted
silently. Use the same call as connect() does.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review 🔄 Running since 2026-10-21T22:24:27.578538Z 988bb17 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@PavelPashov PavelPashov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@maxymlyskov LGTM thank you for the contribution

@PavelPashov
PavelPashov merged commit 00c9a22 into redis:main Oct 22, 2026
22 checks passed
github-actions Bot pushed a commit that referenced this pull request Nov 23, 2026
# [6.1.0](v6.0.0...v6.1.0) (2026-11-23)

### Bug Fixes

* accept field arrays in hmget/hmgetBuffer types ([#2167](#2167)) ([c0cd66c](c0cd66c))
* align auto-pipeline callbacks for dynamic commands ([#2180](#2180)) ([7479cab](7479cab))
* apply the legacy reply shape to uppercase command names ([#2252](#2252)) ([8ecb423](8ecb423))
* clean up Sentinel failover subscriptions after quit ([#2198](#2198)) ([727edfa](727edfa))
* close the monitor connection when monitor() rejects ([#2247](#2247)) ([3cfe93f](3cfe93f))
* cluster stuck in close ([#2217](#2217)) ([a5fad1d](a5fad1d))
* **cluster:** bucket autopipelined MSET objects by their first key ([#2207](#2207)) ([b4e3237](b4e3237))
* **cluster:** do not reject pipeline commands for connecting nodes when offline queue is disabled ([#2277](#2277)) ([71516ca](71516ca)), closes [#2096](#2096) [Cluster#sendCommand](https://github.com/Cluster/issues/sendCommand) [#2276](#2276)
* **cluster:** do not throw when auto-pipelining meets an uncached slot ([#2233](#2233)) ([e04bb49](e04bb49))
* **cluster:** don't fall back to a replica when no primary is left ([#2275](#2275)) ([c353e34](c353e34))
* **cluster:** emit sharded subscriber connect errors silently ([#2241](#2241)) ([00c9a22](00c9a22))
* **cluster:** end when quit() or disconnect() is called while reconnecting ([#2240](#2240)) ([8526785](8526785))
* **cluster:** hash autopipelined Buffer keys by their bytes ([#2208](#2208)) ([a8378bd](a8378bd)), closes [Command#getSlot](https://github.com/Command/issues/getSlot)
* **cluster:** hash lone surrogates as the UTF-8 Node sends ([#2242](#2242)) ([cfa464d](cfa464d))
* **cluster:** keep forwarding sharded messages after reconnecting from end ([#2251](#2251)) ([326e987](326e987))
* **cluster:** map every slot when stringNumbers is enabled ([#2255](#2255)) ([8ce4115](8ce4115))
* **cluster:** match Buffer shard channels when unsubscribing ([#2203](#2203)) ([d997d01](d997d01))
* **cluster:** pass the retry delay to the reconnecting event ([#2291](#2291)) ([ed50383](ed50383))
* **cluster:** resend only the redirected command in a pipeline ([#2214](#2214)) ([46b3191](46b3191)), closes [#2075](#2075) [Pipeline#exec](https://github.com/Pipeline/issues/exec) [Cluster#sendCommand](https://github.com/Cluster/issues/sendCommand) [Pipeline#exec](https://github.com/Pipeline/issues/exec) [Cluster#sendCommand](https://github.com/Cluster/issues/sendCommand) [Pipeline#exec](https://github.com/Pipeline/issues/exec)
* **cluster:** stay ended when nodes removed by disconnect() end late ([#2237](#2237)) ([61896e5](61896e5)), closes [#2217](#2217)
* **cluster:** stop refreshing slots when disconnect() is called during a refresh ([#2249](#2249)) ([80501ed](80501ed))
* **cluster:** support zero-argument sunsubscribe with sharded subscribers ([#2137](#2137)) ([13ff050](13ff050)), closes [#2133](#2133)
* **cluster:** vendor cluster-key-slot to fix empty hash tag slot bug ([#2172](#2172)) ([b479b76](b479b76))
* correct Sentinel retry ordering after node connection failures ([#2246](#2246)) ([a10e0fa](a10e0fa))
* decode integer replies up to MAX_SAFE_INTEGER exactly ([#2164](#2164)) ([28940b5](28940b5))
* decode RESP3 attribute replies instead of failing the connection ([#2213](#2213)) ([6a8cb3e](6a8cb3e)), closes [#decodeNestedType](https://github.com/redis/ioredis/issues/decodeNestedType) [#decodeMapAsObject](https://github.com/redis/ioredis/issues/decodeMapAsObject) [#decodeMapKey](https://github.com/redis/ioredis/issues/decodeMapKey)
* decode RESP3 doubles with Number ([#2150](#2150)) ([6fb5a10](6fb5a10))
* end when quit() or disconnect() is called while reconnecting ([#2245](#2245)) ([81bc16a](81bc16a))
* handle rejection from the db-restoring select on reconnectOnError ([#2229](#2229)) ([1b744ca](1b744ca))
* handle rejections from auto-resubscribe and select on reconnect ([#2187](#2187)) ([de2fbdf](de2fbdf))
* inherit connectTimeout for cluster subscribers ([#2220](#2220)) ([db57c2d](db57c2d))
* keep String.prototype in fast mode after defining VerbatimString ([#2206](#2206)) ([90e1261](90e1261))
* keep subscriber mode while other channel kinds remain subscribed ([#2196](#2196)) ([eb97beb](eb97beb))
* keep the exec error out of its own previousErrors ([#2218](#2218)) ([2c602cf](2c602cf))
* keep the selected db for commands queued while reconnecting ([#2235](#2235)) ([9f90154](9f90154))
* preserve empty keys and match patterns in scan streams ([#2199](#2199)) ([b169c3a](b169c3a))
* redact credentials from the command attached to errors ([#2231](#2231)) ([a26f8de](a26f8de)), closes [#1713](#1713)
* reject unfulfilled commands dropped on reconnect ([#2194](#2194)) ([d95d05a](d95d05a)), closes [#2193](#2193)
* reset retry attempts when connect() is called after giving up ([#2204](#2204)) ([b597303](b597303)), closes [#1463](#1463)
* reset the manual close state when connect() is called after end ([#2250](#2250)) ([7713c4b](7713c4b))
* resolve argument-less (p)unsubscribe once its own kind is empty ([#2238](#2238)) ([280812b](280812b))
* restore the db selected by a non-lowercase SELECT after reconnecting ([#2248](#2248)) ([400b774](400b774))
* **sentinel:** handle errors on failover detector connections ([#2257](#2257)) ([09bcda5](09bcda5))
* **sentinel:** retry unreachable sentinels in a loop instead of recursing ([#2270](#2270)) ([6bee8d2](6bee8d2))
* **types:** accept documented TLS profile options ([#2195](#2195)) ([56a7fb8](56a7fb8)), closes [#1626](#1626)
* **types:** add event overloads to Cluster interface ([#2201](#2201)) ([3996eaf](3996eaf)), closes [#2191](#2191)
* **types:** add smessage and smessageBuffer event overloads ([#2191](#2191)) ([ee48f4c](ee48f4c)), closes [#2190](#2190)
* **types:** allow number for zrange stop argument ([#2163](#2163)) ([932f4c2](932f4c2))

### Features

* add BITRANGEINTARRAY command ([#2279](#2279)) ([b141399](b141399))
* add set member expiration commands ([#2261](#2261)) ([5efba6b](5efba6b)), closes [redis/redis#15938](redis/redis#15938)
* bless commands ([#2209](#2209)) ([9181ee0](9181ee0))
* **cluster:** allow configuring node role for Pub/Sub subscriber ([#2176](#2176)) ([6b9130c](6b9130c))
* sharded subscriber health check ([#2268](#2268)) ([1b25ffb](1b25ffb))
* smart client handoffs ([#2165](#2165)) ([d8ae3cb](d8ae3cb))
* sorted set move commands ([#2288](#2288)) ([2f4f09d](2f4f09d))
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 6.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants