Skip to content

Commit

Permalink
Fixed a bug when onUndeliveredElement was invoked for normally-receiv…
Browse files Browse the repository at this point in the history
…e elements on JS (Kotlin#2828)

Fixes Kotlin#2826
  • Loading branch information
qwwdfsad committed Jul 20, 2021
1 parent 3694ac9 commit f85b207
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Expand Up @@ -123,4 +123,19 @@ class ChannelUndeliveredElementTest : TestBase() {
check(!_cancelled.getAndSet(true)) { "Already cancelled" }
}
}

@Test
fun testHandlerIsNotInvoked() = runTest { // #2826
val channel = Channel<Unit> {
expectUnreached()
}

expect(1)
launch {
expect(2)
channel.receive()
}
channel.send(Unit)
finish(3)
}
}
15 changes: 13 additions & 2 deletions kotlinx-coroutines-core/js/src/internal/LinkedList.kt
Expand Up @@ -32,7 +32,18 @@ public open class LinkedListNode {
this._prev = node
}

/*
* Remove that is invoked as a virtual function with a
* potentially augmented behaviour.
* I.g. `LockFreeLinkedListHead` throws, while `SendElementWithUndeliveredHandler`
* invokes handler on remove
*/
public open fun remove(): Boolean {
return removeImpl()
}

@PublishedApi
internal fun removeImpl(): Boolean {
if (_removed) return false
val prev = this._prev
val next = this._next
Expand Down Expand Up @@ -76,7 +87,7 @@ public open class LinkedListNode {
public fun removeFirstOrNull(): Node? {
val next = _next
if (next === this) return null
check(next.remove()) { "Should remove" }
check(next.removeImpl()) { "Should remove" }
return next
}

Expand All @@ -85,7 +96,7 @@ public open class LinkedListNode {
if (next === this) return null
if (next !is T) return null
if (predicate(next)) return next
check(next.remove()) { "Should remove" }
check(next.removeImpl()) { "Should remove" }
return next
}
}
Expand Down

0 comments on commit f85b207

Please sign in to comment.