Unverified Commit db973dc7 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Fix binary trace self-time on same-span re-entry (#100205)

## Summary

Fix lost binary-trace self-time when a span re-enters itself on the same
thread, as happens with instrumented futures. Previously, the inner Exit
restarted the outer frame's clock under the same `(span_id, thread_id)`
key and immediately removed it, dropping the outer frame's remaining
work.

Store the active slice start on each per-thread stack frame instead.
Enter closes the current top's slice; Exit closes the matching frame and
resumes the new top only when the exiting frame was on top. Non-LIFO
exits remove suspended frames without restarting another clock;
unmatched exits leave clocks unchanged. Allocation attribution,
queued-parent processing, and `DROP_SPANS` remain intact.

**Binary flame CPU/self-time, concurrency-corrected time, and
concurrency metrics will change because the previously missing slices
are now accounted for correctly.** This does not change the binary trace
format.

Add encoded `TraceRow` parser regressions for single/multiple same-span
nesting, child interleaving, cross-thread activity, non-LIFO/unmatched
exits, live input with a partial final row, allocations, queued parents,
and dropped spans. Five regression cases failed against the original
reader before the fix.

## Verification

- `cargo test -p turbopack-trace-server` — 84 library and 58 binary
tests passed.
- `cargo clippy -p turbopack-trace-server --all-targets -- -D warnings`
— passed.
- `cargo fmt --all -- --check` — passed.
- Generated a real binary trace from a small local app using published
`16.5.0-canary.10` with `NEXT_TURBOPACK_TRACING=turbo-tasks`: 2,282,825
rows, 652,508 Enter/Exit pairs, 11 threads, and 109,050 same-span
re-entries; all exits LIFO.
- Independently decoded the raw postcard rows with a standalone decoder
and reconstructed slices using per-thread span-ID stacks and transition
timestamps. Of 1,118,980 physical positive frame slices, five were
intentionally excluded by existing thread/blocking-span rules. The
remaining **1,118,975-slice multiset matched exactly** in both Store
events and `self_time_tree`, including ownership and start/end
timestamps. Independent self-time, stored self-time, and root flame CPU
total all equaled **11.537977 CPU seconds**. No trace payload or local
app files are included.
- Repository-wide `pnpm build-all` bootstrap could not start because
this checkout has no `node_modules` (`turbo: command not found`); the
Rust checks above ran successfully.

<!-- NEXT_JS_LLM -->


<!-- fleet 8e507624-bae8-4561-8469-867912a2654e -->
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
parent a73eee23
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment