sdk-java icon indicating copy to clipboard operation
sdk-java copied to clipboard

Ensure thread safety of AllOfPromise

Open pivovarit opened this issue 1 year ago • 5 comments

What was changed

Replaced the internal AllOfPromise int notReadyCount counter with a thread-safe final AtomicInteger

Why?

AllOfPromise registers multiple Promise instances that were created outside of its scope and uses the handle() method to schedule counter decrements. When the counter reaches zero, it means that all futures completed.

However, handle() method can be executed by different threads, and the internal counter in the current form:

  • doesn't guarantee that new value will be immediately visible for other threads due to the lack of volatile
  • doesn't guarantee atomicity of the increment/decrement operations, which can result in lost updates

pivovarit avatar May 28 '24 20:05 pivovarit

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar May 28 '24 20:05 CLAassistant

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

CLAassistant avatar May 28 '24 20:05 CLAassistant

@pivovarit all threads in a workflow are managed and coordinated by the SDK, no external threads not managed by the SDK are allowed to be created inside a workflow, so no two threads are ever running concurrently so this should not be necessary .

Quinn-With-Two-Ns avatar May 28 '24 20:05 Quinn-With-Two-Ns

Does it mean that those subsequent handle() callbacks are effectively executed serially by the same thread?

pivovarit avatar May 28 '24 21:05 pivovarit

They are executed serially, potentially by different threads, all with all threads using a common lock for serialization

Quinn-With-Two-Ns avatar May 29 '24 14:05 Quinn-With-Two-Ns