Skip to content

Commit f85b207

Browse files
authored
Fixed a bug when onUndeliveredElement was invoked for normally-receive elements on JS (#2828)
Fixes #2826
1 parent 3694ac9 commit f85b207

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

Diff for: kotlinx-coroutines-core/common/test/channels/ChannelUndeliveredElementTest.kt

+15
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,19 @@ class ChannelUndeliveredElementTest : TestBase() {
123123
check(!_cancelled.getAndSet(true)) { "Already cancelled" }
124124
}
125125
}
126+
127+
@Test
128+
fun testHandlerIsNotInvoked() = runTest { // #2826
129+
val channel = Channel<Unit> {
130+
expectUnreached()
131+
}
132+
133+
expect(1)
134+
launch {
135+
expect(2)
136+
channel.receive()
137+
}
138+
channel.send(Unit)
139+
finish(3)
140+
}
126141
}

Diff for: kotlinx-coroutines-core/js/src/internal/LinkedList.kt

+13-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,18 @@ public open class LinkedListNode {
3232
this._prev = node
3333
}
3434

35+
/*
36+
* Remove that is invoked as a virtual function with a
37+
* potentially augmented behaviour.
38+
* I.g. `LockFreeLinkedListHead` throws, while `SendElementWithUndeliveredHandler`
39+
* invokes handler on remove
40+
*/
3541
public open fun remove(): Boolean {
42+
return removeImpl()
43+
}
44+
45+
@PublishedApi
46+
internal fun removeImpl(): Boolean {
3647
if (_removed) return false
3748
val prev = this._prev
3849
val next = this._next
@@ -76,7 +87,7 @@ public open class LinkedListNode {
7687
public fun removeFirstOrNull(): Node? {
7788
val next = _next
7889
if (next === this) return null
79-
check(next.remove()) { "Should remove" }
90+
check(next.removeImpl()) { "Should remove" }
8091
return next
8192
}
8293

@@ -85,7 +96,7 @@ public open class LinkedListNode {
8596
if (next === this) return null
8697
if (next !is T) return null
8798
if (predicate(next)) return next
88-
check(next.remove()) { "Should remove" }
99+
check(next.removeImpl()) { "Should remove" }
89100
return next
90101
}
91102
}

0 commit comments

Comments
 (0)