fix(zpages): release stored span state on SpanProcessor.Shutdown #10416
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
<!-- pull-request-dashboard-status -->
<!-- pull-request-dashboard-status-revision:25 -->
Pull request dashboard status
Merged · refreshed 2026-12-01 15:16 UTC
<details>
<summary>Status above doesn't look right?</summary>
<br>
</details>
Codecov Report
:x: Patch coverage is
97.67442% with 1 line in your changes missing coverage. Please review.:white_check_mark: Project coverage is 90.7%. Comparing base (
1903d1a) to head (c64961e).| Files with missing lines | Patch % | Lines |
|---|---|---|
| zpages/spanprocessor.go | 97.6% | 1 Missing :warning: |
<details><summary>Additional details and impacted files</summary>

@@ Coverage Diff @@
## main #10416 +/- ##
======================================
Coverage 90.7% 90.7%
======================================
Files 232 232
Lines 16793 16827 +34
======================================
+ Hits 15242 15275 +33
- Misses 1551 1552 +1
| Files with missing lines | Coverage Δ | |
|---|---|---|
| zpages/spanprocessor.go | 99.1% <97.6%> (-0.9%) | :arrow_down: |
</details>
<details><summary> :rocket: New features to boost your workflow: </summary>
</details>
<!-- ccr-overview-v2 -->
🟢 Approval recommended
The review identified no issues requiring action, although tests were not run.
No open issues 🎉
<sub>Review effort: Balanced</sub>
OnStart and OnEnd now take mu.RLock() on every call. The lock is what closes the race with Shutdown, so I think it's the right approach. I'm wondering about contention, though, because the reader count is a shared atomic and parallel callers will all hit it. Could you add benchstat output for BenchmarkSpanProcessorOnStartOnEnd (Serial and Parallel) against main to the PR description? If Parallel shows a meaningful regression, we should look at a cheaper guard.
Nit: activeStoreLen and sampleStoreLen differ only in which sync.Map they range over. A single mapLen(m *sync.Map) int would cover both.
Whether Shutdown lands mid-flight here depends on scheduling, so on a fast run the producers may finish before it starts, and the test then covers only the post-shutdown path. Could we make the overlap deterministic? For example, run the producers until a channel closes and call Shutdown once a producer has signalled it's inside its loop. It would also help to confirm the test fails with the stopped check removed, using -race -count=100, so we know it guards the regression.
The store-level state here is already covered by TestSpanProcessorShutdownAcrossTracerProviderShutdown. I think this test is most useful when it checks only what the handler adds: the summary omits the span names, and the active and error detail views show "0 Requests". Could you trim the setup and assertions to that?
addressed in c69a6cb5a. The previous sync.RWMutex guard regressed Parallel by ~13%, so I switched to an atomic.Pointer[spanState] with a stopped sentinel and updated the benchstat output in the PR description (now statistically indistinguishable from main in both Serial and Parallel).
addressed in 13900f72c
addressed in 645d9d85c. Confirmed with -race -count=100 that the test passes 100/100 times as written and fails 100/100 times with the stopped check removed.
addressed in 0127d8ece
<!-- ccr-overview-v2 -->
🟢 Approval recommended
No active review findings were reported, although tests were not run during this review.
No open issues 🎉
<sub>Review effort: Balanced</sub>
Approved these changes.
Approved these changes.
<!-- ccr-overview-v2 -->
🟢 Approval recommended
There are no active review issues for the change that releases zpages span state on shutdown.
No open issues 🎉
<sub>Review effort: Balanced</sub>
MrAlias
dashpole
Fixes #10396
Summary
SpanProcessor.Shutdownwas previously a no-op. If aTracerProvidershut down while spans were still active, the SDK removed its processors before those spans ended, so their laterEnd()calls never reachedzpages. Anytracezhandler that outlived the provider retained those span references indefinitely and kept reporting active spans as still running.This change updates
SpanProcessor.Shutdownto stop processing further span lifecycle events and release all stored span state:activeSpansStoreandspanSampleStoresin an internalspanStatestruct held byatomic.Pointer[spanState], lazily initialized viaCompareAndSwapso zero-value&SpanProcessor{}remains usable.Shutdown, atomically swap in astoppedStatesentinel and clear both maps on the previous state. SubsequentShutdowncalls are no-ops, and calls toOnStartandOnEndafterShutdownreturn immediately via a single atomic pointer load.TracerProvider.Shutdown(includingtracezHTTP rendering), directSpanProcessor.Shutdownwith subsequentOnStart/OnEnd/ForceFlush/Shutdowncalls, zero-valueSpanProcessor,TracerProvider.UnregisterSpanProcessor, and concurrentOnStart/OnEnd/HTTP handler reads/Shutdown.BenchmarkSpanProcessorOnStartOnEnd(SerialandParallel) to cover theOnStart/OnEndhot path. BothSerialandParallelare statistically indistinguishable frommainwith no additional allocations.Benchmarks
Alternatives Considered
spanSampleStoresafterShutdown: Keeping completed span samples would allow post-shutdown inspection of historical latency and error samples when an HTTP server outlives theTracerProvider, at the cost of pinning completed span attributes and events in memory indefinitely whenever a handler remains registered. Clearing both stores matchessdktrace.SpanProcessor.Shutdown("release of resources held by the processor") and avoids retaining stale diagnostics after shutdown.OnStart/OnEnd/Shutdownwithsync.RWMutex: TakingRLock/RUnlockon everyOnStartandOnEndsynchronously drains in-flight calls onShutdown, but introduces shared reader-count atomic contention (+10% Serial, +13% Parallel inBenchmarkSpanProcessorOnStartOnEnd). Swapping anatomic.Pointer[spanState]to astoppedStatesentinel avoids hot-path contention while detaching the old state immediately so any concurrent in-flight call finishes on the unreachable struct and is collected on return.