Defer CUDA GpuNewContext for on-demand connections - #1448
Merged
Conversation
CUDACtx's constructor writes GpuNewContext directly through QueueSerialFinish(), unlike every other GPU backend (Vulkan, OpenGL, D3D11/12, Metal, WebGPU, Rocprof), which all defer it via GetProfiler().DeferItem() so it survives on-demand's per-connection queue clear. A profiler connecting any time after the CUDA context is created (in practice: any time after process start) never receives GpuNewContext. The GpuContextName message that Name() sends right after (already correctly deferred) then crashes the server's Worker::ProcessGpuContextName with an unregistered context id (assert(ctx) fails; undefined behavior in release builds). Same fix already applied to the Rocprof backend in wolfpld#1336. Fixes wolfpld#1171. Includes a repro test under tests/cuda/repro/on_demand/, mirroring the structure wolfpld#1336 added for Rocprof: a minimal CUDA program that creates an on-demand context (repro.cu/CMakeLists.txt), and a check_gpu_zones tool that loads the resulting .tracy file and verifies the GPU context was named and populated with zones. Verified locally: unpatched tracy-capture crashes on the first connection attempt; patched, three consecutive connect/disconnect cycles all succeed and check_gpu_zones reports a named context with recorded zones.
17steen
marked this pull request as ready for review
August 3, 2026 09:23
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is very similar to #1336, but for CUDA.
Problem
The CUDA GPU backend crashes when a profiler connects to an application built with
TRACY_ON_DEMAND, even on the very first connection attempt:In release builds (assert compiled out) this is undefined behavior — typically a segfault. See also #1171, which describes this exact crash.
Root cause
CUDACtx's constructor (TracyCUDA.hpp) writes aGpuNewContextqueue item but never callsDeferItem(), unlike every other GPU backend (Vulkan, OpenGL, D3D11/12, Metal, WebGPU, and Rocprof as of #1336), which all defer it under#ifdef TRACY_ON_DEMAND.Under on-demand mode, a new connection clears the client's pending serial queue (
ClearQueues()) before replaying deferred items. SinceGpuNewContextis never deferred, it never survives that clear — no matter how early the connection happens, even immediately after the context is created. TheGpuContextNamemessage thatName()sends right after (already correctly deferred, viaSubmitQueueItem()) is then replayed to a server that has no record of the context, triggering the assertion above.Fix
Add a
DeferItem()call forGpuNewContextunder#ifdef TRACY_ON_DEMAND, matching the patternName()already uses a few lines below, and the pattern #1336 used for the identical bug in the Rocprof backend.Repro case
tests/cuda/repro/on_demand/contains a minimal CUDA program and acheck_gpu_zonestool, mirroring the structure #1336 added for Rocprof. See the README in that directory for details.Test results
Tested on an NVIDIA RTX 2000 Ada Generation, CUDA 13.3, both release and debug builds, using both the vendored
repro.cuand a standalone CUDA + TracyClient test program:-O2)tracy-capturesegfaults on first connection-g -O0)Assertion 'ctx' failedinWorker::ProcessGpuContextNamecheck_gpu_zonesThree consecutive
tracy-captureconnect/disconnect cycles against the same long-running process all succeeded after the fix, confirming on-demand profiling now works for repeated attach/detach, not just a single session.