Skip to content

Fixed a bug when onUndeliveredElement was invoked for normally-receiv… #2828

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

Merged
merged 1 commit into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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