Skip to content

fix(backend): stop leaking connection-metrics observers per connect#564

Merged
garmr-ulfr merged 1 commit into
mainfrom
fix-telemetry-conn-metrics-leak
Jul 14, 2026
Merged

fix(backend): stop leaking connection-metrics observers per connect#564
garmr-ulfr merged 1 commit into
mainfrom
fix-telemetry-conn-metrics-leak

Conversation

@garmr-ulfr

Copy link
Copy Markdown
Collaborator

Problem

StartConnectionMetrics was called on every vpn.Connected, allocating a fresh buffered event channel and OTel instruments each connect. The observers were never reclaimed on disconnect, so they accumulated on every connect/disconnect cycle.

Fix

  • Build the connection-metrics observer once and reuse it across connects: attach it to the tunnel on Connected, detach (without destroying) on any other status.
  • Fully release it — cancelling its recording goroutine — only when telemetry shuts down, via the new teardownConnMetrics.
  • Also fixes stopTelemetry leaking the observer when telemetry was already disabled: the old updateConnMetrics(vpn.Disconnected) no-op'd once TelemetryKey was off.

StartConnectionMetrics was called on every vpn.Connected, allocating a
fresh buffered event channel and OTel instruments each connect. The
observers were never reclaimed on disconnect, so they accumulated on
every connect/disconnect cycle.

Build the observer once and reuse it: attach it to the tunnel on
Connected, detach (without destroying) on any other status, and fully
release it -- cancelling its recording goroutine -- only when telemetry
shuts down, via teardownConnMetrics. This also fixes stopTelemetry
leaking the observer when telemetry was already disabled (the old
updateConnMetrics(Disconnected) no-op'd once TelemetryKey was off).

Copilot AI 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.

Pull request overview

This PR fixes a backend telemetry leak by ensuring connection-metrics observers are not re-created on every VPN reconnect and are properly released when telemetry shuts down.

Changes:

  • Reuses a single connection-metrics observer across connect/disconnect cycles by attaching on vpn.Connected and detaching on other statuses.
  • Adds teardownConnMetrics() to fully detach and cancel the metrics goroutine during telemetry shutdown, fixing a leak when telemetry is disabled.
  • Refactors observer initialization into ensureConnMetricsObserverLocked() to centralize “create once” behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@garmr-ulfr garmr-ulfr marked this pull request as ready for review July 14, 2026 01:02
@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@garmr-ulfr, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 31 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 7e2c7f69-8cd6-41f1-b511-c79bec650dcd

📥 Commits

Reviewing files that changed from the base of the PR and between 19f3684 and 553943f.

📒 Files selected for processing (1)
  • backend/radiance.go
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-telemetry-conn-metrics-leak

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.

@garmr-ulfr garmr-ulfr merged commit 0639f62 into main Jul 14, 2026
4 checks passed
@garmr-ulfr garmr-ulfr deleted the fix-telemetry-conn-metrics-leak branch July 14, 2026 15:50
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.

4 participants