Skip to content

Commit 4396f38

Browse files
committed
Fix race condition in pair select
This bug was introduced by #1524. The crux of problem is that TryOffer/PollDesc.onPrepare method is no longer allowed to update fields in these classes (like "resumeToken" and "pollResult") after call to tryResumeSend/Receive method, because the latter will complete the ongoing atomic operation and helper method might find it complete and try reading "resumeToken" which was not initialized yet. This change removes "pollResult" field which was not really needed ("result.pollResult" field is used) and removes "resumeToken" by exploiting the fact that current implementation of CancellableContinuationImpl does not need a token anymore. However, CancellableContinuation.tryResume/completeResume ABI is left intact, because it is used by 3rd party code. This fix lead to overall simplification of the code. A number of fields and an auxiliary IdempotentTokenValue class are removed, tokens used to indicate various results are consolidated, so that resume success is now consistently indicated by a single RESUME_TOKEN symbol. Fixes #1561
1 parent 93d2fe7 commit 4396f38

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

kotlinx-coroutines-core/common/src/CancellableContinuationImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ internal open class CancellableContinuationImpl<in T>(
320320
val update = CompletedExceptionally(exception)
321321
if (!_state.compareAndSet(state, update)) return@loop // retry on cas failure
322322
disposeParentHandle()
323-
return state
323+
return RESUME_TOKEN
324324
}
325325
else -> return null // cannot resume -- not active anymore
326326
}

0 commit comments

Comments
 (0)