[codex] fix gpu example attach races - #582
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes several startup/teardown race conditions in the CUDA/GPU attach path and makes PTX pass execution more robust by running pass logic in a dedicated bpftime_ptxpass_runner subprocess (with LD_PRELOAD/LD_AUDIT cleared).
Changes:
- Move syscall-server CUDA initialization to occur after global shared memory setup, and add retries for
cuda_comm_shared_memdiscovery inSHM_OPEN_ONLYclients. - Harden CUDA watcher thread teardown by clearing global watcher pointers before attach context teardown.
- Run PTX pass
print_config/process_inputvia a newbpftime_ptxpass_runnerexecutable instead of in-processdlmopen/dlsym.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| runtime/syscall-server/syscall_server_utils.cpp | Defers CUDA init until after global shm init in syscall-server startup. |
| runtime/syscall-server/syscall_context.hpp | Adds initialize_cuda() API under BPFTIME_ENABLE_CUDA_ATTACH. |
| runtime/syscall-server/syscall_context.cpp | Moves CUDA init logic into initialize_cuda() with RAII flag reset. |
| runtime/src/bpftime_shm_internal.cpp | Adds retry loop for cuda_comm_shared_mem lookup; guards CUDA host registration. |
| runtime/src/attach/bpf_attach_ctx.cpp | Uses centralized CUDA watcher stop routine in destructor. |
| runtime/src/attach/bpf_attach_ctx_cuda.cpp | Implements stop_cuda_watcher_thread() that clears global watcher state before join. |
| runtime/include/bpf_attach_ctx.hpp | Declares stop_cuda_watcher_thread() under CUDA attach build. |
| attach/nv_attach_impl/pass/ptxpass_runner.cpp | New subprocess runner that dlopens a pass library and isolates stdout for JSON output. |
| attach/nv_attach_impl/nv_attach_impl.hpp | Removes in-process pass function pointers/handles from pass config struct. |
| attach/nv_attach_impl/nv_attach_impl.cpp | Uses subprocess runner for pass config/process; removes boost::asio thread-pool patching. |
| attach/nv_attach_impl/CMakeLists.txt | Builds bpftime_ptxpass_runner and exports runner path via generated header. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+121
to
+131
| void syscall_context::initialize_cuda() | ||
| { | ||
| SPDLOG_INFO("Initializing CUDA at syscall-server side"); | ||
| initializing_cuda = true; | ||
| struct cuda_init_guard { | ||
| bool &flag; | ||
| ~cuda_init_guard() | ||
| { | ||
| flag = false; | ||
| } | ||
| } guard{ initializing_cuda }; |
yunwei37
force-pushed
the
codex/fix-gpu-examples
branch
from
June 15, 2026 21:05
a12df71 to
a4d5e2c
Compare
This was referenced Jun 29, 2026
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.
Summary
Fix GPU example startup/teardown races in the existing runtime and syscall-server path. This PR keeps the change scoped to runtime/shared-memory coordination; it does not add the PTX pass subprocess runner from #583.
What changed:
syscall_contextconstruction and run it after global shared memory is created.initializing_cudaflag so intercepted syscalls fall back to the real libc/syscall path during CUDA startup.cuda_comm_shared_memlookup forSHM_OPEN_ONLYclients before registering CUDA host memory, covering the window where the shared segment exists but the CUDA communication object is not visible yet.bpf_attach_ctxteardown, including the global atexit pointer/stop flag, to avoid stale watcher-thread state at process exit.Why
Several GPU examples exposed races around startup and process teardown:
cuda_comm_shared_memhad been constructed.Together these showed up as flaky GPU example startup, dynamic attach failures, or exit-time aborts.
Scope
This PR changes only runtime/syscall-server/shared-memory code:
runtime/syscall-server/*runtime/src/bpftime_shm_internal.cppruntime/src/attach/bpf_attach_ctx*.cppruntime/include/bpf_attach_ctx.hppThe PTX pass isolation runner is intentionally split out into #583 and is not required for this PR to work.
Validation
atomizer,kernel_trace,gpu_shared_map,threadscheduling,threadscheduling_dynamic_hook,cudagraph,threadhist-gpu-kernel-shared-map,directly_run_on_gpu,kernelretsnoop,threadhist,cuda-counter,launchlate,launchlate-kernel-gpu-shared-map,mem_trace,host_map_test, andcutlass.malloc,minimal,queue_demo,stack_demo,bloom_filter_demo,get_stack_id_example, andlpm_trie_demo.