sdk-java
sdk-java copied to clipboard
Ensure thread safety of AllOfPromise
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
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.
@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 .
Does it mean that those subsequent handle() callbacks are effectively executed serially by the same thread?
They are executed serially, potentially by different threads, all with all threads using a common lock for serialization