Skip to content

Commit b5e2dd3

Browse files
committed
Fixes
1 parent 6e2f090 commit b5e2dd3

File tree

1 file changed

+8
-6
lines changed
  • reactive/kotlinx-coroutines-reactive/src

1 file changed

+8
-6
lines changed

reactive/kotlinx-coroutines-reactive/src/Await.kt

+8-6
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,9 @@ private suspend fun <T> Publisher<T>.awaitOne(
146146
override fun onSubscribe(sub: Subscription) {
147147
/** cancelling the existing subscription due to rule 2.5, though the publisher would either have to
148148
* subscribe more than once, which would break 2.12, or leak this [Subscriber]. */
149-
subscription?.let {
150-
value = null
151-
seenValue = false
152-
inTerminalState = false
153-
it.cancel()
149+
if (subscription != null) {
150+
sub.cancel()
151+
return
154152
}
155153
subscription = sub
156154
cont.invokeOnCancellation { sub.cancel() }
@@ -185,6 +183,7 @@ private suspend fun <T> Publisher<T>.awaitOne(
185183
Mode.LAST, Mode.SINGLE, Mode.SINGLE_OR_DEFAULT -> {
186184
if ((mode == Mode.SINGLE || mode == Mode.SINGLE_OR_DEFAULT) && seenValue) {
187185
sub.cancel()
186+
// the check for `cont.isActive` is just a slight optimization and doesn't affect correctness
188187
if (cont.isActive)
189188
cont.resumeWithException(IllegalArgumentException("More than one onNext value for $mode"))
190189
} else {
@@ -200,14 +199,17 @@ private suspend fun <T> Publisher<T>.awaitOne(
200199
if (!tryEnterTerminalState("onComplete"))
201200
return
202201
if (seenValue) {
203-
if (cont.isActive) cont.resume(value as T)
202+
// the check for `cont.isActive` is just a slight optimization and doesn't affect correctness
203+
if (mode != Mode.FIRST_OR_DEFAULT && mode != Mode.FIRST && cont.isActive)
204+
cont.resume(value as T)
204205
return
205206
}
206207
when {
207208
(mode == Mode.FIRST_OR_DEFAULT || mode == Mode.SINGLE_OR_DEFAULT) -> {
208209
cont.resume(default as T)
209210
}
210211
cont.isActive -> {
212+
// the check for `cont.isActive` is just a slight optimization and doesn't affect correctness
211213
cont.resumeWithException(NoSuchElementException("No value received via onNext for $mode"))
212214
}
213215
}

0 commit comments

Comments
 (0)