Skip to content

Commit 87ea164

Browse files
authored
fix the race condition in waitForOperationCompletion (#93)
1 parent c4f440f commit 87ea164

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

sdk/src/main/java/com/amazonaws/lambda/durable/operation/BaseDurableOperation.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,15 @@ protected Operation waitForOperationCompletion() {
136136

137137
validateCurrentThreadType();
138138

139+
// register to prevent the state from advancing
140+
phaser.register();
141+
139142
// If we are in a replay where the operation is already complete (SUCCEEDED /
140143
// FAILED), the Phaser will be
141144
// advanced in .execute() already and we don't block but return the result
142145
// immediately.
143146
if (phaser.getPhase() == ExecutionPhase.RUNNING.getValue()) {
144147
// Operation not done yet
145-
phaser.register();
146-
147148
var context = executionManager.getCurrentContext();
148149
// Deregister current context - allows suspension
149150
logger.debug(
@@ -162,6 +163,9 @@ protected Operation waitForOperationCompletion() {
162163

163164
// Complete phase 1
164165
phaser.arriveAndDeregister();
166+
} else {
167+
// The phaser is already completed. Deregister now.
168+
phaser.arriveAndDeregister();
165169
}
166170

167171
// Get result based on status

0 commit comments

Comments
 (0)