[CORE] Fix race condition in AuthorizationRequestContext.loadRole()
Fixed #9084
-
Replaced separate
hasLoadRole.get()andhasLoadRole.set(true)calls with an atomic
hasLoadRole.compareAndSet(false, true)operation. -
This makes the check-and-set atomic, ensuring the runnable executes only once,
even under concurrent invocations. -
Previously, multiple threads could see
falseand execute the runnable simultaneously,
causing duplicate executions. -
Added a new unit test:
testLoadRoleRunsOnceEvenWhenInvokedConcurrently. -
Verified that the runnable executes only once, even when
loadRole()is called concurrently.
Result
Improved thread-safety of loadRole() and prevented race conditions during concurrent access.
Please see the conversation in #9084. While this does fix the race condition it introduces some other issues and I think a little more needs to be done here.