Skip to content

Commit

Permalink
avoid ChildCancelledException Kotlin/kotlinx.coroutines#2550
Browse files Browse the repository at this point in the history
  • Loading branch information
nbransby committed Mar 5, 2021
1 parent 6d339de commit 505ba3c
Showing 1 changed file with 2 additions and 4 deletions.
Expand Up @@ -6,8 +6,6 @@ import kotlinx.coroutines.channels.SendChannel

//workaround for https://github.com/Kotlin/kotlinx.coroutines/issues/974
@ExperimentalCoroutinesApi
fun <E> SendChannel<E>.safeOffer(element: E) = try {
!isClosedForSend && offer(element)
} catch (e : ClosedSendChannelException) {
false
fun <E> SendChannel<E>.safeOffer(element: E): Boolean {
return runCatching { !isClosedForSend && offer(element) }.getOrDefault(false)
}

0 comments on commit 505ba3c

Please sign in to comment.