Commit d9be88a
authored
fix(workloadmeta): fix data race and duplicate registration in process collector telemetry (#48093)
### What does this PR do?
Moves the `discovered_services` telemetry gauge initialization from the process collector's `Start()` method to the `newProcessCollector()` constructor. This fixes both a data race and a duplicate metrics registration panic.
### Motivation
Enabling `discovery.enabled` by default on Linux caused the process workloadmeta collector to start in tests where it previously didn't. This exposed two latent bugs:
1. **Data race**: `Start()` is called from `startCandidatesWithRetry` in a background goroutine (`store.go:74`). It calls `telemetry.NewGaugeWithOpts()` → `GetCompatComponent()` → `newTelemetry()`, which reads the global `registry` variable without holding the mutex. Meanwhile, the fx Stop hook calls `Reset()`, which writes to `registry` under the mutex. Since the background goroutine is fire-and-forget (the fx Start hook returns immediately), fx Stop can begin while the goroutine is still running.
2. **Duplicate registration panic** (with `-count=N`): Leaked background goroutines from test run N survive into test run N+1. After `Reset()` creates a new registry between runs, both the leaked goroutine and the new test's collector try to register the same gauge on the same new registry.
Both issues are fixed by moving the gauge initialization to the constructor, which runs synchronously during fx construction — before Start and well before Stop.
### Describe how you validated your changes
Reproduced the data race by running:
```
dda inv test --targets=./cmd/agent/subcommands/processchecks/ --race --extra-args="-count=20"
```
After the fix, confirmed 80/80 passes across multiple batches with the race detector enabled.
Co-authored-by: vincent.whitchurch <vincent.whitchurch@datadoghq.com>1 parent c43b6e2 commit d9be88a
File tree
1 file changed
+16
-9
lines changed- comp/core/workloadmeta/collectors/internal/process
1 file changed
+16
-9
lines changedLines changed: 16 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
95 | 105 | | |
96 | 106 | | |
97 | 107 | | |
| |||
108 | 118 | | |
109 | 119 | | |
110 | 120 | | |
| 121 | + | |
111 | 122 | | |
112 | 123 | | |
113 | 124 | | |
| |||
159 | 170 | | |
160 | 171 | | |
161 | 172 | | |
162 | | - | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
163 | 178 | | |
164 | 179 | | |
165 | 180 | | |
| |||
213 | 228 | | |
214 | 229 | | |
215 | 230 | | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
224 | 231 | | |
225 | 232 | | |
226 | 233 | | |
| |||
0 commit comments