fix(backend): stop leaking connection-metrics observers per connect#564
Conversation
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).
There was a problem hiding this comment.
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.Connectedand 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.
|
Warning Review limit reached
Next review available in: 31 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the 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. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
Problem
StartConnectionMetricswas called on everyvpn.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
Connected, detach (without destroying) on any other status.teardownConnMetrics.stopTelemetryleaking the observer when telemetry was already disabled: the oldupdateConnMetrics(vpn.Disconnected)no-op'd onceTelemetryKeywas off.