Commit df5d108
fix(tracing): Fix inflated http.client span durations on iOS background cancellation (#5944)
* fix(tracing): Use foreground-exit timestamp for http.client spans on iOS background cancellation
On iOS, when the app goes inactive, `cancelInBackground` schedules a
5-second deferred cancellation timer. If the JS thread is suspended
between the `inactive` and `background` transitions, the timer fires
only after JS resumes — potentially seconds or minutes later. The idle
span's `onIdleSpanEnded` then force-ends all still-recording child
spans with this late timestamp, producing http.client spans with
inflated durations that don't match the corresponding breadcrumbs.
This was reported by a customer whose traces showed http.client spans
with long durations and `cancelled` status, while the app was marked
as "In Foreground" with no backgrounding breadcrumb — the hallmark of
the iOS inactive timer race.
The fix records `timestampInSeconds()` when the app first leaves the
foreground (`inactive` or `background` event) and uses that timestamp
to end http.client child spans in `cancelSpan()`, before the parent
idle span ends. This prevents `onIdleSpanEnded` from overwriting them
with the later timer-fire timestamp.
Fixes #5942
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* docs: Add changelog entry for http.client span duration fix
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix(tracing): Remove unused variable in test
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix(tracing): Make timestamp reset test independent of fake timer clock
The test was asserting on duration ranges (>2s, <5s) which relied on
jest.advanceTimersByTime advancing the value returned by
timestampInSeconds(). Since performance is not faked, this only works
when the cached time function falls back to Date.now() — a fragile
implementation detail.
Instead, capture timestampInSeconds() at each state transition and
compare the span's end time directly against the background timestamp.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Lint fix
---------
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>1 parent 4ae2ca9 commit df5d108
File tree
3 files changed
+140
-3
lines changed- packages/core
- src/js/tracing
- test/tracing
3 files changed
+140
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| |||
198 | 198 | | |
199 | 199 | | |
200 | 200 | | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
201 | 207 | | |
202 | 208 | | |
203 | 209 | | |
204 | 210 | | |
205 | 211 | | |
206 | 212 | | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
207 | 228 | | |
208 | 229 | | |
209 | 230 | | |
210 | 231 | | |
211 | 232 | | |
212 | 233 | | |
| 234 | + | |
213 | 235 | | |
214 | 236 | | |
| 237 | + | |
| 238 | + | |
215 | 239 | | |
216 | 240 | | |
217 | 241 | | |
| |||
220 | 244 | | |
221 | 245 | | |
222 | 246 | | |
| 247 | + | |
223 | 248 | | |
224 | 249 | | |
225 | 250 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
5 | 12 | | |
6 | 13 | | |
7 | 14 | | |
| |||
40 | 47 | | |
41 | 48 | | |
42 | 49 | | |
43 | | - | |
| 50 | + | |
44 | 51 | | |
45 | 52 | | |
46 | 53 | | |
| |||
217 | 224 | | |
218 | 225 | | |
219 | 226 | | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
220 | 331 | | |
221 | 332 | | |
222 | 333 | | |
| |||
0 commit comments