Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Mono.awaitSingleOrNull non-compliance with 2.7 #3363

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2016-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.coroutines.reactive
Expand Down
25 changes: 21 additions & 4 deletions reactive/kotlinx-coroutines-reactor/src/Mono.kt
Expand Up @@ -13,6 +13,8 @@ import reactor.core.*
import reactor.core.publisher.*
import kotlin.coroutines.*
import kotlinx.coroutines.internal.*
import java.util.concurrent.locks.*
import kotlin.concurrent.*

/**
* Creates a cold [mono][Mono] that runs a given [block] in a coroutine and emits its result.
Expand Down Expand Up @@ -44,12 +46,18 @@ public fun <T> mono(
* function immediately cancels its [Subscription] and resumes with [CancellationException].
*/
public suspend fun <T> Mono<T>.awaitSingleOrNull(): T? = suspendCancellableCoroutine { cont ->
injectCoroutineContext(cont.context).subscribe(object : Subscriber<T> {
val subscriber = object : Subscriber<T> {
private var seenValue = false

override fun onSubscribe(s: Subscription) {
cont.invokeOnCancellation { s.cancel() }
s.request(Long.MAX_VALUE)
cont.invokeOnCancellation {
withSubscriptionLock {
s.cancel()
}
}
withSubscriptionLock {
s.request(Long.MAX_VALUE)
}
}

override fun onComplete() {
Expand All @@ -62,7 +70,16 @@ public suspend fun <T> Mono<T>.awaitSingleOrNull(): T? = suspendCancellableCorou
}

override fun onError(error: Throwable) { cont.resumeWithException(error) }
})

/**
* Enforce rule 2.7: [Subscription.request] and [Subscription.cancel] must be executed serially
*/
@Synchronized
private fun withSubscriptionLock(block: () -> Unit) {
block()
}
}
injectCoroutineContext(cont.context).subscribe(subscriber)
}

/**
Expand Down
@@ -0,0 +1,45 @@
/*
* Copyright 2016-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.coroutines.reactor

import kotlinx.coroutines.*
import org.junit.*
import org.reactivestreams.*
import reactor.core.*
import reactor.core.publisher.*
import java.util.concurrent.locks.*

class MonoAwaitCancellationStressTest {
private val iterations = 10_000 * stressTestMultiplier

@Test
@Ignore // Will be able to test this after https://github.com/reactor/reactor-core/issues/3117 is fixed
fun testAwaitCancellationOrder() = runBlocking {
dkhalanskyjb marked this conversation as resolved.
Show resolved Hide resolved
repeat(iterations) {
val job = launch(Dispatchers.Default) {
TestMono.awaitSingleOrNull()
}
job.cancelAndJoin()
}
}

private object TestMono: Mono<Int>() {
override fun subscribe(s: CoreSubscriber<in Int>) {
val lock = ReentrantLock()
s.onSubscribe(object : Subscription {
override fun request(n: Long) {
check(lock.tryLock())
s.onNext(42)
lock.unlock()
}

override fun cancel() {
check(lock.tryLock())
lock.unlock()
}
})
}
}
}