Remove race condition in Scheduler.effect.delayCancellable #1078
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #1077
Scheduler.effect.timedCancellable
schedules an action that completes a Promise withSome(a)
after a timer completes, and returns the result of reading the Promise along with an action that cancel s the timer and completes the Promise withNone
.This means that if
cancel
is called after the timer and the action have completed, it will try to complete the Promise with None after the action has already completed it withSome(a)
, and therefore the cancel action will fail with a PromiseAlreadyCompletedException.If you look at
timedGet
(ortimedDecrementBy
in Semaphore, which has the same problem), we can see where the problem is:If
g
wins therace
, thencancelTimer.as(Some(a))
is executed, but if in the meantimetimer
has completed (so it lost therace
, but it completed beforecancelTimer
executes), thencancelTimer.as(Some(a))
will be a failedF
as explained above, and therefore it will short circuit over theas(Some(a))
and result in the wholetimedGet
failing with aPromiseAlreadyCompleted
exception.The fix is to make sure that
cancelTimer
is a no op if called after the timer has already completed, instead of having it fail.