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

Avoid race conditions when the input attribute slice is shared #422

Merged
merged 7 commits into from
Apr 3, 2023

Conversation

jmacd
Copy link
Member

@jmacd jmacd commented Apr 3, 2023

Description:

Makes a copy of the attribute slice before calling NewSet to avoid race conditions when attribute.NewSet sorts the slice in place. Note that this is done after searching the map for a suitable exact match, prior to allocations, so benchmarks which measure re-use of existing attribute sets are not affected.

(Note that the call to NewSetWithSortable is no longer needed, since newer Go compilers are able to avoid that temporary.)

Link to tracking Issue: open-telemetry/opentelemetry-go#3943

Testing: A new test was added to exercise the input race condition.

Documentation: None added.

@jmacd jmacd requested a review from paivagustavo April 3, 2023 21:02
@jmacd
Copy link
Member Author

jmacd commented Apr 3, 2023

BenchmarkCounterAddOneAttrSafe-10                	14480460	        81.92 ns/op	      64 B/op	       1 allocs/op
BenchmarkCounterAddOneAttrUnsafe-10              	15952938	        74.94 ns/op	      64 B/op	       1 allocs/op
BenchmarkCounterAddOneAttrSliceReuseSafe-10      	22029919	        54.18 ns/op	       0 B/op	       0 allocs/op
BenchmarkCounterAddOneAttrSliceReuseUnsafe-10    	25136043	        47.17 ns/op	       0 B/op	       0 allocs/op

@jmacd
Copy link
Member Author

jmacd commented Apr 3, 2023

h/t @MadVikingGod for the test case. briefly, this SDK was susceptible to all the same race conditions outlined in open-telemetry/opentelemetry-go#3943. As discussed in that thread, this SDK is organized to perform a map lookup before creating an attribute.Set; this persists, which is to say that the race condition has been corrected after the map lookup meaning there the fast path still has zero allocations.

@codecov-commenter
Copy link

codecov-commenter commented Apr 3, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.01 🎉

Comparison is base (e623f70) 87.85% compared to head (b8d5c3e) 87.86%.

❗ Current head b8d5c3e differs from pull request most recent head 44e071d. Consider uploading reports for the commit 44e071d to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #422      +/-   ##
==========================================
+ Coverage   87.85%   87.86%   +0.01%     
==========================================
  Files          75       75              
  Lines        4282     4286       +4     
==========================================
+ Hits         3762     3766       +4     
  Misses        445      445              
  Partials       75       75              
Impacted Files Coverage Δ
lightstep/sdk/metric/internal/asyncstate/async.go 94.11% <100.00%> (+0.24%) ⬆️
lightstep/sdk/metric/internal/syncstate/sync.go 97.98% <100.00%> (+0.02%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@jmacd jmacd requested review from jaronoff97 and kristinapathak and removed request for paivagustavo April 3, 2023 21:20
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