Draft
Fix CUDA watcher thread segfault at exit due to shared memory unmapped before thread stops#548
Conversation
Co-authored-by: yunwei37 <34985212+yunwei37@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix segfault issue in CUDA agent watcher thread
Fix CUDA watcher thread segfault at exit due to shared memory unmapped before thread stops
Feb 26, 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.
The CUDA watcher thread (
start_cuda_watcher_thread) pollsctx->cuda_shared_mem->flag1in shared memory. On exit,__destruct_shm()(.fini_array, priority 65535) callsbpftime_destroy_global_shm()which unmaps the Boost IPC segment — includingCommSharedMem— before the thread stops, causing a SIGSEGV.~bpf_attach_ctx()never fires becausebpf_attach_ctxlives in a union with an empty destructor, and the existingatexithandler's ordering relative to.fini_arrayis not guaranteed.Changes
bpf_attach_ctx_cuda.cpp: Exposebpftime::bpftime_stop_cuda_watcher_thread()— a non-anonymous-namespace wrapper around the existingstop_cuda_watcher_thread_at_exit(). Idempotent; safe to call from bothatexitand__destruct_shm.bpftime_shm_internal.cpp: Callbpftime_stop_cuda_watcher_thread()in__destruct_shm()beforebpftime_destroy_global_shm(), guaranteeing the thread is joined before the segment is unmapped regardless of atexit/fini_array ordering:bpf_attach_ctx.cpp: Guardcuda_ctx->...in~bpf_attach_ctx()with a null check to prevent dereference on a partially-constructed object.Original prompt
🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.