Commit 2952e1f
fix(telemetry): refactor to shared_ptr and add deterministic shutdown() (#326)
* fix(telemetry): refactor to shared_ptr and add deterministic shutdown()
Refactors Telemetry to be heap-allocated via a shared_ptr
(enable_shared_from_this + factory create()). Scheduled-task callbacks
now capture a weak_from_this() so they become no-ops automatically once
the object is no longer reachable. Move semantics are removed.
Adds a shutdown() function that cancels scheduled tasks, sends the
app-closing payload, and releases the HTTP client. After the call, all
HTTP sending becomes a no-op: send_payload() snapshots http_client_
under http_client_mutex_ and returns early if null. http_client_ is
reset under the same mutex in shutdown(), so any concurrent call that
already holds a live snapshot completes cleanly while new calls
short-circuit. If this is the last shared_ptr to the client, the
background Curl thread is joined at that point.
Rationale: shutdown() cannot guarantee that, after it returns, there
are no in-flight HTTP callbacks still holding a reference to Telemetry
state. Using a shared_ptr ensures any late callback finds a dead
weak_ptr and short-circuits instead of accessing a destroyed object.
The load-bearing weak_from_this() captures are the on_response and
on_error lambdas inside send_payload(). Those run on CurlImpl's
independent event_loop_ thread (via handle_message()), which has no
blocking-cancel mechanism. app_closing() calls drain() with a 2-second
deadline, so if the agent is slow, requests can still be in flight on
the curl thread after shutdown() returns. The weak.lock() guard is what
prevents a use-after-free in that window.
The weak_from_this() captures in the scheduled-task callbacks
(schedule_tasks()) are defense-in-depth for a different concern:
ThreadedEventScheduler's cancel() blocks until any in-progress
callback finishes, making those captures redundant against the
threaded scheduler. However, the EventScheduler interface does not
mandate a blocking cancel(), so the captures guard against
non-blocking implementations where a callback could otherwise touch
Telemetry state after it has been torn down.
Also fixes a race in test_span.cpp: disabling telemetry there prevents
app_started() from posting to the Curl background thread, which would
otherwise fail to reach the (absent) agent asynchronously and call
log_error() on the shared MockLogger, racing against the
error_count == 0 assertion.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* fix comment wording
* run clang-format
---------
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>1 parent 93ed055 commit 2952e1f
6 files changed
Lines changed: 257 additions & 250 deletions
File tree
- include/datadog/telemetry
- src/datadog/telemetry
- test
- telemetry
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
47 | 66 | | |
48 | 67 | | |
49 | 68 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
41 | | - | |
42 | | - | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | | - | |
| 72 | + | |
73 | 73 | | |
74 | 74 | | |
75 | | - | |
| 75 | + | |
76 | 76 | | |
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
81 | 91 | | |
82 | 92 | | |
83 | 93 | | |
84 | | - | |
85 | | - | |
| 94 | + | |
| 95 | + | |
86 | 96 | | |
87 | 97 | | |
88 | 98 | | |
| |||
92 | 102 | | |
93 | 103 | | |
94 | 104 | | |
95 | | - | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
96 | 108 | | |
97 | 109 | | |
98 | 110 | | |
99 | 111 | | |
100 | 112 | | |
101 | 113 | | |
102 | 114 | | |
103 | | - | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
104 | 118 | | |
105 | 119 | | |
106 | 120 | | |
107 | 121 | | |
108 | 122 | | |
109 | 123 | | |
110 | 124 | | |
111 | | - | |
112 | | - | |
| 125 | + | |
| 126 | + | |
113 | 127 | | |
114 | 128 | | |
115 | 129 | | |
| |||
119 | 133 | | |
120 | 134 | | |
121 | 135 | | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
128 | 143 | | |
129 | 144 | | |
130 | 145 | | |
131 | 146 | | |
132 | | - | |
133 | | - | |
| 147 | + | |
| 148 | + | |
134 | 149 | | |
135 | 150 | | |
136 | 151 | | |
137 | 152 | | |
138 | 153 | | |
139 | 154 | | |
140 | 155 | | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
147 | 163 | | |
148 | 164 | | |
149 | 165 | | |
150 | 166 | | |
151 | | - | |
152 | | - | |
| 167 | + | |
| 168 | + | |
153 | 169 | | |
154 | 170 | | |
155 | 171 | | |
156 | 172 | | |
157 | 173 | | |
158 | 174 | | |
159 | 175 | | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
166 | 183 | | |
167 | 184 | | |
168 | 185 | | |
169 | 186 | | |
170 | 187 | | |
171 | | - | |
172 | | - | |
| 188 | + | |
| 189 | + | |
173 | 190 | | |
174 | 191 | | |
175 | 192 | | |
| |||
181 | 198 | | |
182 | 199 | | |
183 | 200 | | |
184 | | - | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
185 | 204 | | |
186 | 205 | | |
187 | 206 | | |
188 | 207 | | |
189 | 208 | | |
190 | 209 | | |
191 | 210 | | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
198 | 218 | | |
199 | 219 | | |
200 | 220 | | |
201 | 221 | | |
202 | 222 | | |
203 | 223 | | |
204 | 224 | | |
205 | | - | |
206 | | - | |
| 225 | + | |
| 226 | + | |
207 | 227 | | |
208 | 228 | | |
209 | 229 | | |
| |||
213 | 233 | | |
214 | 234 | | |
215 | 235 | | |
216 | | - | |
217 | | - | |
| 236 | + | |
| 237 | + | |
218 | 238 | | |
219 | 239 | | |
220 | 240 | | |
| |||
0 commit comments