Skip to content

fix: make ProfilerSignalManager::_handler atomic to prevent data race#8451

Open
korniltsev-grafanista wants to merge 1 commit intoDataDog:masterfrom
korniltsev-grafanista:kk/fix-signal-handler-atomic-dd
Open

fix: make ProfilerSignalManager::_handler atomic to prevent data race#8451
korniltsev-grafanista wants to merge 1 commit intoDataDog:masterfrom
korniltsev-grafanista:kk/fix-signal-handler-atomic-dd

Conversation

@korniltsev-grafanista
Copy link
Copy Markdown

_handler is a plain function pointer that is written on a normal thread in RegisterHandler and read inside a signal handler in CallCustomHandler. Without atomics this is a data race — undefined behavior in C++. The C++ standard ([support.signal]/3) requires lock-free atomics for signal-safe access, and std::atomic<T*> is lock-free on all relevant platforms.

Changes:

  • Declare _handler as std::atomic<HandlerFn_t> instead of plain HandlerFn_t
  • Use explicit load/store with appropriate memory ordering at every access site
  • Fix broken double-checked locking in RegisterHandler: the second check after acquiring the mutex was comparing a stale local copy instead of re-loading _handler

Note: _isHandlerInPlace has a similar (less critical) data race, left for a follow-up.

Co-Reviewed-By: Claude Opus 4.6 (1M context)

@korniltsev-grafanista korniltsev-grafanista marked this pull request as ready for review April 14, 2026 01:54
@korniltsev-grafanista korniltsev-grafanista requested a review from a team as a code owner April 14, 2026 01:54
Copy link
Copy Markdown
Collaborator

@gleocadie gleocadie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@korniltsev-grafanista your commit must be signed for your PR to be mergeable.
if not possible for you, I can do something

}
std::unique_lock<std::mutex> lock(_handlerRegisterMutex);

current = _handler.load(std::memory_order_acquire);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch nice

@gleocadie
Copy link
Copy Markdown
Collaborator

@korniltsev-grafanista your commit must be signed for your PR to be mergeable. if not possible for you, I can do something

One piece of information I was not aware, as of today for security reasons (CI cannot run on external forks), I'll create the branch, cherry-pick your commit and create a PR with it.

_handler is written on a normal thread in RegisterHandler and read inside
a signal handler in CallCustomHandler. Without atomics this is undefined
behavior — the C++ standard requires lock-free atomics for signal-safe
access ([support.signal]/3).

Also fixes the double-checked locking in RegisterHandler: the second
check after acquiring the mutex was comparing a stale local copy instead
of re-loading from the (now atomic) _handler.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@korniltsev-grafanista korniltsev-grafanista force-pushed the kk/fix-signal-handler-atomic-dd branch from 63696f0 to 3928517 Compare April 14, 2026 21:14
@korniltsev-grafanista
Copy link
Copy Markdown
Author

signed #8451 and #8162 as well

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.

2 participants