Skip to content

fix(pubsub): avoid panics on Channel conflict and Ring empty subscribe - #3968

Open
VedantMadane wants to merge 5 commits into
redis:masterfrom
VedantMadane:fix-issue-3761
Open

VedantMadane wants to merge 5 commits into
redis:masterfrom
VedantMadane:fix-issue-3761

Conversation

@VedantMadane

@VedantMadane VedantMadane commented Aug 17, 2026

Copy link
Copy Markdown

Summary

Fixes #3761.

  • PubSub.Channel / ChannelWithSubscriptions mutual-exclusion misuse no longer panics; returns a closed channel and logs a warning.
  • Ring.Subscribe / PSubscribe / SSubscribe no longer panic on empty channels or shard lookup failure; return a PubSub with a sticky error (as the existing TODO suggested).

Test plan

  • TestRingSubscribeEmptyChannelsNoPanic
  • TestPubSubChannelMutualExclusionNoPanic

Note

Medium Risk
Changes PubSub failure semantics from panic to errors/closed channels; callers that relied on panics or expected blocking on misuse may see different behavior, though it is generally safer for production.

Overview
Fixes #3761 by replacing several PubSub panics with recoverable behavior.

Ring Subscribe / PSubscribe / SSubscribe no longer panic when channels are missing or shard lookup fails (e.g. empty ring). They return a PubSub built via failedPubSub that carries a stickyErr, which conn() and Receive* surface as errors instead of crashing at construction.

PubSub.Channel and ChannelWithSubscriptions now handle two failure modes without panicking: sticky construction errors, and calling the “wrong” channel API after the other was already used. Both paths log a warning and return an already-closed Go channel (logging uses context.Background() to avoid a race on c.cmd).

Tests cover Ring empty subscribe, channel API conflicts, and Ginkgo cases for sticky-error channels.

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

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 924803beea

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread pubsub.go Outdated
Comment thread pubsub.go Outdated

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 924803b. Configure here.

Comment thread pubsub.go Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0d618f37c6

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread pubsub.go Outdated
@ndyakov

ndyakov commented Aug 18, 2026

Copy link
Copy Markdown
Member

@VedantMadane thank you for this, Ring is not officially supported or recommended anymore, but I will take a look when possible.

@ndyakov ndyakov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for tackling this — approach looks right and matches the TODO/issue, and keeping the signatures non-breaking is the right call. A couple of things before merge, main ones being a dead stickyErr write in Close() and the Ring sticky-error path hanging silently when consumed via Channel(). Details inline.

Comment thread pubsub.go Outdated
Comment thread pubsub.go
Comment thread pubsub.go
Comment thread ring.go
Comment thread ring.go
Comment thread .gitattributes Outdated
Conflict-path logging used getContext(), which reads c.cmd while
the other Channel Receive loop may write it — fails CI under -race.

Closes nothing; Refs redis#3761
Match SingleConnPool.Close by setting stickyErr to ErrClosed so
failed Ring PubSub Channel receivers always observe the closed
exit signal. Add .gitattributes to keep Go sources on LF.
… and docs

- Remove dead stickyErr write in Close()
- Short-circuit Channel() and ChannelWithSubscriptions() when stickyErr is set to return an already-closed channel and log a warning
- Add warning log in failedPubSub for visibility
- Update doc comments for Subscribe/PSubscribe/SSubscribe and Channel methods
- Remove repo-wide .gitattributes

Signed-off-by: Vedant Madane <6527493+VedantMadane@users.noreply.github.com>
@VedantMadane

VedantMadane commented Sep 10, 2026

Copy link
Copy Markdown
Author

Addressed in dc6f17e:

  • Dropped the redundant stickyErr write in Close()
  • Added short-circuiting in Channel() and ChannelWithSubscriptions() when stickyErr != nil && !closed to log a warning and return an already-closed channel (so for range pubsub.Channel() completes without hanging).
  • Added warning logging in Ring.failedPubSub for visibility when constructing a failed PubSub.
  • Updated docstrings for Subscribe/PSubscribe/SSubscribe and Channel/ChannelWithSubscriptions.
  • Removed .gitattributes.
  • Rebased on latest master.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PubSub.Channel and Ring.Subscribe/PSubscribe/SSubscribe panic instead of returning error

2 participants