Commit ed36a75
committed
Fix a race condition when tracing Kotlin coroutines.
* When using a `ThreadContextElement` to track suspensions and resumes, its important that we make
`updateThreadContext` and `restoreThreadContext` safe, when calls to them are concurrent and interleaved.
* For e.g. this is something that can happen when creating a child coroutine that in turn uses a multi threaded dispatcher.
```
coroutine.updateThreadContext() // Thread #1
... coroutine body ...
// suspension + immediate dispatch happen here
coroutine.updateThreadContext() // Thread #2, coroutine is already resumed
// ... coroutine body after suspension point on Thread #2 ...
coroutine.restoreThreadContext() // Thread #1, is invoked late because Thread #1 is slow
coroutine.restoreThreadContext() // Thread #2, may happen in parallel with the previous restore
```
* Previously when `updateThreadContext(...)` would get called on `Thread #2`, we won't emit an additional `begin` slice, for the new track because we are in `STATE_BEGIN` on the old thread.
* When ending a slice, we could end on `Thread #2` and not on `Thread #1` which ends up corrupting traces.
Test: This is something very difficult to to test, but I added additional integration tests, that involve jumping threads.
Example trace: https://ui.perfetto.dev/#!/?s=575b99d9a235654f9de85aadaaa0ce63f31e4770
Change-Id: I18a49c3f6b65fa41ab5f9ad855377ac779aa43261 parent faeec94 commit ed36a75
4 files changed
Lines changed: 77 additions & 4 deletions
File tree
- tracing
- tracing-wire/src/desktopTest/kotlin/androidx/tracing/wire
- tracing/src
- commonMain/kotlin/androidx/tracing
- jvmAndAndroidMain/kotlin/androidx/tracing
Lines changed: 14 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
123 | 137 | | |
124 | 138 | | |
125 | 139 | | |
| |||
Lines changed: 22 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
| 20 | + | |
19 | 21 | | |
20 | 22 | | |
21 | 23 | | |
| |||
40 | 42 | | |
41 | 43 | | |
42 | 44 | | |
43 | | - | |
44 | | - | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
45 | 58 | | |
46 | 59 | | |
47 | 60 | | |
48 | 61 | | |
49 | | - | |
50 | | - | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
51 | 69 | | |
52 | 70 | | |
53 | 71 | | |
| |||
Lines changed: 19 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
Lines changed: 22 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
0 commit comments