tracy icon indicating copy to clipboard operation
tracy copied to clipboard

`InitRpmalloc` does not function for threads which outlive a profiling session

Open ccp-serpent opened this issue 11 months ago • 1 comments

Using TRACY_ON_DEMAND, TRACY_MANUAL_LIFETIME.

When starting up and shutting down the profiler via tracy::ShutdownProfiler() and tracy:StartupProfiler multiple times during a process lifetime, any threads exceeding the lifetime of the first profiler session into the second session will cause a crash when instrumented via ___tracy_alloc_srcloc.

Reading through the source code, this appears to be caused by deallocations made to each thread's _memory_thread_heap object during profiler shutdown. Since heap initialization state is managed by thread local storage, RpThreadInitDone, threads with deallocated memory are unable to recognize whether their backing heap has been deallocated when calling InitRpmalloc .

I'm not sure there's a workaround for this which supports the use case mentioned above, reading through the source code I cannot find one, at least. I'd be curious if a reader has further insight on this. I want to suggest using global, shared state instead of thread local storage to keep track of whether a thread has a properly initialized backing heap.

ccp-serpent avatar Feb 28 '25 16:02 ccp-serpent

I just wonder why Thread-local storage is used at all, I am pretty sure there is a good reason for it, maybe as a way to ensure thread-safety?

I have a similar but probably unrelated problem in rpmalloc, I get SIGSEGV crashes but I am pretty sure it's due to how I fail to understand how Tracy works under the hood.

PerMagnusH avatar Oct 01 '25 16:10 PerMagnusH