Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK] Valgrind errors on std::atomic variables #2244

Merged
merged 1 commit into from
Jul 22, 2023

Conversation

marcalff
Copy link
Member

@marcalff marcalff commented Jul 22, 2023

Fixes #2243

Changes

Using only the default constructor to initialize std::atomic objects is incorrect.
This is a particularity of std::atomic itself, that does not follow the same rules as C++ in general.

See: https://en.cppreference.com/w/cpp/atomic/atomic/atomic

In the entire code base, reviewed every use of std::atomic,
and changed initialization to use the direct list initialization syntax.

see: https://en.cppreference.com/w/cpp/language/list_initialization

This form is the most robust and maintainable:

  • auditing the source code for correctness is easier
  • this form guarantees a proper initialization will be done, in all cases

This fixed the bug reported on the periodic metric reader, tested under valgrind.

This fixed a missing initialization in one of the BatchLogRecordProcessor constructors,
found by code review.

This also fixed the following pattern, which technically, is incorrect:

std::atomic<bool> flag;
flag.store(true);

With the fix, code such as:

std::atomic<bool> flag{false};
flag.store(true);

is correct, because it is now legal to call store() as the object is initialized.

  • CHANGELOG.md updated for non-trivial changes
  • Unit tests have been added
  • Changes in public API reviewed

@marcalff marcalff requested a review from a team July 22, 2023 12:57
@codecov
Copy link

codecov bot commented Jul 22, 2023

Codecov Report

Merging #2244 (65001c2) into main (92a8a54) will decrease coverage by 0.03%.
The diff coverage is 100.00%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2244      +/-   ##
==========================================
- Coverage   87.34%   87.30%   -0.03%     
==========================================
  Files         169      169              
  Lines        4902     4897       -5     
==========================================
- Hits         4281     4275       -6     
- Misses        621      622       +1     
Impacted Files Coverage Δ
sdk/src/trace/batch_span_processor.cc 92.43% <100.00%> (-0.27%) ⬇️

... and 1 file with indirect coverage changes

Copy link
Member

@esigo esigo left a comment

Choose a reason for hiding this comment

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

LGTM
thanks for the fix :)

@esigo esigo added the ok-to-merge The PR is ok to merge (has two approves or raised by a maintainer/approver and has one approve) label Jul 22, 2023
@marcalff marcalff merged commit 0c5f90d into open-telemetry:main Jul 22, 2023
42 checks passed
@marcalff marcalff changed the title Valgrind errors on std::atomic variables [SDK] Valgrind errors on std::atomic variables Jul 22, 2023
@marcalff marcalff deleted the fix_std_atomic_2243 branch September 5, 2023 15:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ok-to-merge The PR is ok to merge (has two approves or raised by a maintainer/approver and has one approve)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[SDK] Valgrind errors on std::atomic variables
2 participants