Skip to content

Commit be2aa9f

Browse files
committed
~move benign race explanation to declaration site
1 parent 6d91be8 commit be2aa9f

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

kotlinx-coroutines-core/common/src/channels/BufferedChannel.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1582,8 +1582,12 @@ internal open class BufferedChannel<E>(
15821582
* When [hasNext] suspends, this field stores the corresponding
15831583
* continuation. The [tryResumeHasNext] and [tryResumeHasNextOnClosedChannel]
15841584
* function resume this continuation when the [hasNext] invocation should complete.
1585+
*
1586+
* This property is the subject to bening data race:
1587+
* It is nulled-out on both completion and cancellation paths that
1588+
* could happen concurrently.
15851589
*/
1586-
@BenignDataRace // See its uses for the explanation of the race
1590+
@BenignDataRace
15871591
private var continuation: CancellableContinuationImpl<Boolean>? = null
15881592

15891593
// `hasNext()` is just a special receive operation.

kotlinx-coroutines-core/common/src/selects/Select.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ internal open class SelectImplementation<R>(
373373
/**
374374
* List of clauses waiting on this `select` instance.
375375
*
376-
* This property is the subject to bening data-race: concurrent cancellation might null-out this property
376+
* This property is the subject to bening data race: concurrent cancellation might null-out this property
377377
* while [trySelect] operation reads it and iterates over its content.
378378
* A logical race is resolved by the consensus on [state] property.
379379
*/
@@ -412,8 +412,13 @@ internal open class SelectImplementation<R>(
412412
* one that stores either result when the clause is successfully registered ([inRegistrationPhase] is `true`),
413413
* or [DisposableHandle] instance when the clause is completed during registration ([inRegistrationPhase] is `false`).
414414
* Yet, this optimization is omitted for code simplicity.
415+
*
416+
* This property is the subject to benign data race:
417+
* [Cleanup][cleanup] procedure can be invoked both as part of the completion sequence
418+
* and as a cancellation handler triggered by an external cancellation.
419+
* In both scenarios, [NO_RESULT] is written to this property via race.
415420
*/
416-
@BenignDataRace // See its cleanup phase for the explanation
421+
@BenignDataRace
417422
private var internalResult: Any? = NO_RESULT
418423

419424
/**
@@ -627,10 +632,7 @@ internal open class SelectImplementation<R>(
627632
// try to resume the continuation.
628633
this.internalResult = internalResult
629634
if (cont.tryResume(onCancellation)) return TRY_SELECT_SUCCESSFUL
630-
/*
631-
* If the resumption failed, we need to clean the [result] field to avoid memory leaks.
632-
* This write is benignly races with the very same write in cancellation invoke() handler
633-
*/
635+
// If the resumption failed, we need to clean the [result] field to avoid memory leaks.
634636
this.internalResult = NO_RESULT
635637
return TRY_SELECT_CANCELLED
636638
}

0 commit comments

Comments
 (0)