SIGN IN SIGN UP

Fix multiple-reader race condition for Sets/Objects (#5016)

This commit fixes a race condition around keys slice sorting for Sets and Objects, using `sync.Once` guards to ensure that the sorting operation only occurs *at most once* between successive insertion operations.

The race condition could be readily triggered by using iteration methods, such as `.Iter()` or `.Foreach()`, on a Set/Object across multiple goroutines. The goroutines would race one another to sort the keys slice before iterating, with unfortunate results. Tests are included in this commit to exercise these problematic cases.

Benchmarks suggest the performance impact of the `sync.Once` guards is minimal, and increases the variance of keys slice accesses by a few ns.

Signed-off-by: Philip Conrad <philipaconrad@gmail.com>
P
Philip Conrad committed
a1f0cdd7ca898c98cb07843bab9b14a8eadb5fc1
Parent: 68b4307
Committed by GitHub <noreply@github.com> on 8/16/2022, 6:07:26 PM