Skip to content

fix: Restore thread-local stat writer in MockSharedArbitratorTest#8

Open
jaylisde wants to merge 1 commit into
mainfrom
fix/mock-arbitrator-stat-writer-dangling
Open

fix: Restore thread-local stat writer in MockSharedArbitratorTest#8
jaylisde wants to merge 1 commit into
mainfrom
fix/mock-arbitrator-stat-writer-dangling

Conversation

@jaylisde

@jaylisde jaylisde commented Jun 7, 2026

Copy link
Copy Markdown
Owner

What

Three tests in MockSharedArbitratorTest (globalArbitrationWithoutSpill, globalArbitrationSmallParticipantLargeGrow, and a DEBUG_ONLY test) set the thread-local runtime stat writer to a stack-owned TestRuntimeStatWriter on the main thread via a bare setThreadLocalRunTimeStatWriter(), but never restore it. localRuntimeStatWriter is a thread_local raw pointer, so once the writer is destroyed at test exit the pointer dangles. The next test that runs on the main thread and reaches addThreadLocalRuntimeStat (e.g. any global arbitration calling finishArbitration during growCapacity) dereferences the freed writer and segfaults — the tests pass in isolation but crash when run back-to-back, depending on gtest order.

gdb backtrace:

#0 addThreadLocalRuntimeStat
#1 SharedArbitrator::finishArbitration
#2 ScopedArbitration::~ScopedArbitration
#3 SharedArbitrator::growCapacity

Fix

Use the existing RAII RuntimeStatWriterScopeGuard, which restores the previous writer on destruction, so the thread-local pointer is never left dangling.

Three tests (globalArbitrationWithoutSpill,
globalArbitrationSmallParticipantLargeGrow, and a DEBUG_ONLY test) set the
thread-local runtime stat writer to a stack-owned TestRuntimeStatWriter on the
main thread via a bare setThreadLocalRunTimeStatWriter() call, but never
restore it. localRuntimeStatWriter is a thread_local raw pointer, so once the
TestRuntimeStatWriter is destroyed at test exit the pointer dangles. The next
test that runs on the main thread and reaches addThreadLocalRuntimeStat (e.g.
any global arbitration that calls finishArbitration during growCapacity)
dereferences the freed writer and segfaults. The tests pass in isolation but
crash when run after one of these, depending on execution order.

Use the existing RAII RuntimeStatWriterScopeGuard, which restores the previous
writer on destruction, so the thread-local pointer is never left dangling.
@jaylisde
jaylisde force-pushed the fix/mock-arbitrator-stat-writer-dangling branch from 4be6914 to f875b0d Compare June 7, 2026 22:57
@jaylisde jaylisde closed this Jun 7, 2026
@jaylisde jaylisde reopened this Jun 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant