Skip to content

Fix DatadogMonitor metrics forwarder goroutine leak on CR deletion#2805

Closed
majiayu000 wants to merge 1 commit intoDataDog:mainfrom
majiayu000:fix/issue-2803-datadogmonitor-forwarder-goroutine-leak
Closed

Fix DatadogMonitor metrics forwarder goroutine leak on CR deletion#2805
majiayu000 wants to merge 1 commit intoDataDog:mainfrom
majiayu000:fix/issue-2803-datadogmonitor-forwarder-goroutine-leak

Conversation

@majiayu000
Copy link

Summary

  • Add r.forwarders.Unregister(dm) call in finalizeDatadogMonitor() to stop the metrics forwarder goroutine when a DatadogMonitor CR is deleted
  • Guarded by r.operatorMetricsEnabled check, matching the existing pattern in finalizeDadV2() for the DatadogAgent controller
  • Placed before deleteMonitor() to ensure cleanup happens even if deleteMonitor() returns early on error

Fixes #2803

Test plan

  • Existing Test_handleFinalizer tests pass (deletion case uses operatorMetricsEnabled=false by default)
  • go vet ./... passes
  • go build ./internal/controller/datadogmonitor/... passes
  • All datadogmonitor package tests pass

Add r.forwarders.Unregister(dm) call in finalizeDatadogMonitor() to
stop the metrics forwarder goroutine when a DatadogMonitor CR is deleted.
Without this, the forwarder goroutine leaks since its stopChan is never
closed. This mirrors the existing pattern in DatadogAgent's
finalizeDadV2().

The Unregister call is placed before deleteMonitor() to ensure the
forwarder is always cleaned up, even if deleteMonitor() returns early
on error.

Fixes DataDog#2803

Signed-off-by: majiayu000 <1835304752@qq.com>
@majiayu000 majiayu000 requested a review from a team March 24, 2026 10:28
@tbavelier
Copy link
Member

Thank you for the contribution, I was working on the same issue at the same time unfortunately! Closing in favour of #2804

@tbavelier tbavelier closed this Mar 24, 2026
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.

[BUG] DatadogMonitor metrics forwarder goroutines not unregistered on CR deletion

2 participants