Skip to content

Defer CUDA GpuNewContext for on-demand connections - #1448

Merged
wolfpld merged 1 commit into
wolfpld:masterfrom
17steen:fix/cuda-defer-gpu-new-context
Aug 3, 2026
Merged

Defer CUDA GpuNewContext for on-demand connections#1448
wolfpld merged 1 commit into
wolfpld:masterfrom
17steen:fix/cuda-defer-gpu-new-context

Conversation

@17steen

@17steen 17steen commented Aug 3, 2026

Copy link
Copy Markdown

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:

Assertion `ctx' failed in Worker::ProcessGpuContextName

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 a GpuNewContext queue item but never calls DeferItem(), 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. Since GpuNewContext is never deferred, it never survives that clear — no matter how early the connection happens, even immediately after the context is created. The GpuContextName message that Name() sends right after (already correctly deferred, via SubmitQueueItem()) is then replayed to a server that has no record of the context, triggering the assertion above.

Fix

Add a DeferItem() call for GpuNewContext under #ifdef TRACY_ON_DEMAND, matching the pattern Name() 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 a check_gpu_zones tool, 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.cu and a standalone CUDA + TracyClient test program:

Build Unpatched Patched
Release (-O2) tracy-capture segfaults on first connection Capture succeeds, GPU zones recorded
Debug (-g -O0) Assertion 'ctx' failed in Worker::ProcessGpuContextName No assertions
Reconnect (2nd, 3rd connection) N/A (already crashed) Capture succeeds each time
check_gpu_zones N/A (crash, no trace file) Context named, zones > 0

Three consecutive tracy-capture connect/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.

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
17steen marked this pull request as ready for review August 3, 2026 09:23
@wolfpld
wolfpld merged commit d0ffd8d into wolfpld:master Aug 3, 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.

3 participants