Commit affc3c7
authored
fix(profiling): correctly unwind on-CPU Tasks (#15562)
## What does this PR do?
More details and investigation results available in
https://datadoghq.atlassian.net/browse/PROF-13137
Related PRs
* Dependency: #15552
* Dependent: #15579
* Echion PR: P403n1x87/echion#198
This PR fixes the way we sample and capture stacks for on-CPU asyncio
Tasks. This is a first step towards fixing _all_ weird (sub-)stacks we
are seeing (both in Echion and dd-trace-py), for example this one...
```
background_math_function
Task-background_wait
background_wait_function
sleep
```
... where in practice, `background_math_function` was executed as part
of a Task that was completely unrelated to `Task-background_wait` and
should never have appeared in the same stack.
This by itself doesn't fix everything (see **The fix(es, actually)**),
but, as I said, it's a first step.
See [this
document](https://docs.google.com/document/d/10HmKhY6tM5j7ojdk7CG8anWsN7qP7IyeZlj3-zAXudM/edit?pli=1&tab=t.9pzrl0uli4g)
for more explanations around how this works / is supposed to work, the
fix is explained at the bottom here.
## The fix(es, actually)
### Flawed existing logic...
The fix logic is the following:
* To correctly interleave Task Stacks and Task names, we need to
determine the "synchronous Python top-of-stack" that goes on top of the
on-CPU Task (if there is an on-CPU Task).
* This includes the Python stack that eventually led to doing something
like `asyncio.run`, as well as the internal `asyncio` Runner machinery.
* We will have to push this on top of every Task Stack we capture (note
that the on-CPU Stack will already have it by definition).
* To do that, we need to determine which Task is "the on CPU Task" (if
any) and process it first, so that we know how "high" the "upper Python
stack" is and we can re-use it for subsequent Task unwindings.
* To ensure the first Task we unwind is the on-CPU one, we first sort
the Leaf Tasks array by on-CPU-ness
* Note I'm saying _the_ on CPU Task because, as always in Python, there
can only be one thing running at a time.
* (Well, that's the case at least in theory – in practice on-CPU-ness
may change as we sample and so we could end up with more than one on-CPU
Task, but that is fine because it won't change how deep the "upper
stack" is)
* Once we've done this, we know how high the "top of Stack is" because
it is the height of the "pure Python" Stack we get by unwinding the
first (top-most) Frame of the on-CPU Task. We need to remember that
"height" to push that many items from the Python Thread Stack on top of
each Task's Stack.
* One final thing to keep in mind is that we "stack" Stacks (hehe) such
that if `Task-1` is awaiting `Task-2`, the Stack for `Task-2` goes under
that for `Task-1`. We only ever want to add the "pure Python
top-of-stack" once per Stack, so we need to do that _once per leaf Task_
after we've recursively unwound awaiting Tasks for the leaf Task.
### ... and a race condition?
On top of that – because it's not enough – I think I discovered a rare
but very real race condition (which actually was the root cause of that
weird, rare but common-in-CI bug with the stack I showed on top). The
race condition happens when the pure Python Stack we capture involves
some asynchronous Frames, but the associated Task/Coroutine completes or
yields before we get to unwinding it. In that case, we assume in Stack
interleaving that the Task is running, by doing so we include some
Frames from the Python Stack that actually _are not_ in the Stack
generated by unwinding the Task, and because we share the same Python
Stack for all Tasks (we only use it's "upper part", which is the same
for every Task) some Frames from the
previously-running-but-now-paused-or-completed Coroutine are leaked to
other Tasks.
Working around this is not simple; also honestly we should have a
discussion around whether we want to properly work around it (which is
going to be somewhat difficult, imperfect and probably somewhat costly)
or just give up on sampling when we detect something is off.
In the interest of keeping things somewhat simple, this will be handled
in a separate PR. But I described the bug here anyway, for clarity
(temporary PR: KowalskiThomas/echion#43)
### Caveats
I realised after the fact that an assumption we implicitly make (that
_the on-CPU Task – if it exists – is a Leaf Task_) is actually not
generally true. In the case of certain `asyncio` utils, the Parent Task
will actually from time to time execute code/be on-CPU to manage the
Child Tasks it is awaiting on. This is an edge case, and something that
happens rarely, but saying it can never happen is false (and it shows in
Profiles).
We will also need to figure something out for that – it probably isn't
_that_ hard but will require some refactors (probably split the
unwinding in two steps: (1) only determine upper Python Stack (2) unwind
Tasks strictly speaking, starting from Leaf Tasks).
## Testing
The PR adds new tests around on-CPU Tasks and what their Stacks look
like
The PR also updates a lot of existing tests to output their
`DataSummary` as JSON. This is immensely useful to understand what is
actually going on when we don't see the expected Stacks (or see the
unexpected ones) and I do think it'd be worth keeping.
After these changes, the tests that still fail locally and in the CI
(flakily) are unrelated to on-CPU Tasks which, I guess, means _mission
failed successfully_?1 parent e34ab9d commit affc3c7
File tree
3 files changed
+81
-29
lines changed- ddtrace/internal/datadog/profiling/stack_v2/echion/echion
- releasenotes/notes
3 files changed
+81
-29
lines changedLines changed: 31 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
182 | 182 | | |
183 | 183 | | |
184 | 184 | | |
185 | | - | |
186 | | - | |
187 | 185 | | |
| 186 | + | |
| 187 | + | |
188 | 188 | | |
189 | 189 | | |
190 | 190 | | |
191 | | - | |
192 | 191 | | |
193 | 192 | | |
194 | 193 | | |
195 | 194 | | |
196 | 195 | | |
197 | | - | |
198 | 196 | | |
| 197 | + | |
| 198 | + | |
199 | 199 | | |
200 | | - | |
201 | 200 | | |
202 | 201 | | |
203 | 202 | | |
204 | 203 | | |
205 | | - | |
| 204 | + | |
206 | 205 | | |
207 | 206 | | |
208 | 207 | | |
| |||
344 | 343 | | |
345 | 344 | | |
346 | 345 | | |
347 | | - | |
| 346 | + | |
348 | 347 | | |
349 | 348 | | |
350 | 349 | | |
| |||
355 | 354 | | |
356 | 355 | | |
357 | 356 | | |
358 | | - | |
| 357 | + | |
| 358 | + | |
359 | 359 | | |
360 | 360 | | |
361 | 361 | | |
362 | 362 | | |
363 | 363 | | |
364 | 364 | | |
365 | | - | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
366 | 388 | | |
367 | 389 | | |
368 | 390 | | |
| |||
Lines changed: 47 additions & 20 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
264 | 264 | | |
265 | 265 | | |
266 | 266 | | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
267 | 283 | | |
| 284 | + | |
| 285 | + | |
268 | 286 | | |
269 | 287 | | |
| 288 | + | |
270 | 289 | | |
271 | 290 | | |
272 | 291 | | |
273 | | - | |
274 | | - | |
| 292 | + | |
| 293 | + | |
275 | 294 | | |
276 | | - | |
277 | | - | |
278 | | - | |
279 | | - | |
280 | | - | |
281 | | - | |
282 | | - | |
283 | | - | |
284 | | - | |
285 | | - | |
286 | | - | |
287 | | - | |
288 | | - | |
289 | | - | |
290 | | - | |
291 | | - | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
292 | 306 | | |
293 | 307 | | |
294 | 308 | | |
| |||
317 | 331 | | |
318 | 332 | | |
319 | 333 | | |
320 | | - | |
321 | | - | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
322 | 349 | | |
323 | 350 | | |
324 | 351 | | |
| |||
Lines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
0 commit comments