Skip to content

fix(router): connection leak with redis pub/sub#2916

Open
ehaynes99 wants to merge 1 commit into
wundergraph:mainfrom
OpenPhone:ehaynes-fix-redis-connection-leak
Open

fix(router): connection leak with redis pub/sub#2916
ehaynes99 wants to merge 1 commit into
wundergraph:mainfrom
OpenPhone:ehaynes-fix-redis-connection-leak

Conversation

@ehaynes99

@ehaynes99 ehaynes99 commented Jun 3, 2026

Copy link
Copy Markdown

Fix for: #2915

Summary by CodeRabbit

  • Bug Fixes

    • Improved Redis subscription connection cleanup to ensure resources are properly released and prevent connection leaks.
  • Tests

    • Added test to verify Redis connections are released when subscriptions are canceled.

Checklist

  • I have discussed my proposed changes in an issue and have received approval to proceed.
  • I have followed the coding standards of the project.
  • Tests or benchmarks have been added or updated.
  • Documentation has been updated on https://github.com/wundergraph/docs-website.
  • I have read the Contributors Guide.

Open Source AI Manifesto

This project follows the principles of the Open Source AI Manifesto. Please ensure your contribution aligns with its principles.

@ehaynes99 ehaynes99 requested a review from a team as a code owner June 3, 2026 16:35

@claude claude 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.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@github-actions github-actions Bot added the router label Jun 3, 2026
@coderabbitai

coderabbitai Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Redis Pub/Sub connection cleanup in the provider adapter is refactored to use deferred Close() instead of explicit unsubscribe calls, eliminating error-prone manual cleanup paths. A test validates that subscriptions properly release connections when their contexts are canceled.

Changes

Redis Pub/Sub connection cleanup

Layer / File(s) Summary
Deferred connection cleanup in Subscribe
router/pkg/pubsub/redis/adapter.go
Subscribe adds a deferred sub.Close() call in the subscription goroutine to ensure the Pub/Sub connection releases on all exit paths. The ctx.Done() branch is simplified to only log and return, removing the prior explicit unsubscribe cleanup.
Connection leak validation
router/pkg/pubsub/redis/adapter_test.go
Adds noopUpdater test implementation, newTestAdapter helper that constructs and starts an adapter against a miniredis instance, and TestProviderAdapter_Subscribe_ReleasesConnectionOnCancel to verify connections are released when subscription contexts are canceled.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related issues

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix(router): connection leak with redis pub/sub' clearly and specifically describes the main change—fixing a connection leak issue in Redis pub/sub functionality.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.12.2)

level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies"


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@router/pkg/pubsub/redis/adapter_test.go`:
- Around line 65-69: The test eagerly evaluates p.conn.PoolStats().TotalConns in
the require.Eventually call, producing misleading args on failure; fix by
capturing the observed connection count inside the polling closure (introduce a
local var like observed int and set observed = p.conn.PoolStats().TotalConns
inside the func passed to the poller), change require.Eventually to
assert.Eventually (which returns bool) and then, if it returns false, call
t.Fatalf("redis pub/sub connections leaked: have %d, baseline %d", observed,
baseline) so the printed "have" value reflects the last observed measurement at
failure time; update references to p.conn.PoolStats().TotalConns and baseline in
the new code accordingly.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 91753c39-0739-4ad0-b58e-ceeca0d9228f

📥 Commits

Reviewing files that changed from the base of the PR and between 40922ff and 55b808d.

📒 Files selected for processing (2)
  • router/pkg/pubsub/redis/adapter.go
  • router/pkg/pubsub/redis/adapter_test.go

Comment on lines +65 to +69
require.Eventually(t, func() bool {
return p.conn.PoolStats().TotalConns <= baseline
}, 5*time.Second, 10*time.Millisecond,
"redis pub/sub connections leaked: have %d, baseline %d",
p.conn.PoolStats().TotalConns, baseline)

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.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

require.Eventually failure message args are evaluated eagerly.

p.conn.PoolStats().TotalConns at Line 69 is evaluated once when Eventually is invoked (right after cancelling, before any polling), not at failure time. For a leak-detection test, the printed "have %d" can be misleading during CI debugging. Capture the value inside the condition or via a local closure variable.

💚 Proposed fix to report the value observed at failure time
-	require.Eventually(t, func() bool {
-		return p.conn.PoolStats().TotalConns <= baseline
-	}, 5*time.Second, 10*time.Millisecond,
-		"redis pub/sub connections leaked: have %d, baseline %d",
-		p.conn.PoolStats().TotalConns, baseline)
+	var lastTotal uint32
+	require.Eventually(t, func() bool {
+		lastTotal = p.conn.PoolStats().TotalConns
+		return lastTotal <= baseline
+	}, 5*time.Second, 10*time.Millisecond,
+		"redis pub/sub connections leaked: have %d, baseline %d",
+		lastTotal, baseline)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
require.Eventually(t, func() bool {
return p.conn.PoolStats().TotalConns <= baseline
}, 5*time.Second, 10*time.Millisecond,
"redis pub/sub connections leaked: have %d, baseline %d",
p.conn.PoolStats().TotalConns, baseline)
var lastTotal uint32
require.Eventually(t, func() bool {
lastTotal = p.conn.PoolStats().TotalConns
return lastTotal <= baseline
}, 5*time.Second, 10*time.Millisecond,
"redis pub/sub connections leaked: have %d, baseline %d",
lastTotal, baseline)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@router/pkg/pubsub/redis/adapter_test.go` around lines 65 - 69, The test
eagerly evaluates p.conn.PoolStats().TotalConns in the require.Eventually call,
producing misleading args on failure; fix by capturing the observed connection
count inside the polling closure (introduce a local var like observed int and
set observed = p.conn.PoolStats().TotalConns inside the func passed to the
poller), change require.Eventually to assert.Eventually (which returns bool) and
then, if it returns false, call t.Fatalf("redis pub/sub connections leaked: have
%d, baseline %d", observed, baseline) so the printed "have" value reflects the
last observed measurement at failure time; update references to
p.conn.PoolStats().TotalConns and baseline in the new code accordingly.

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.

1 participant