Skip to content

Ensure that non-local returns work properly in inline functions #3986

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 2 commits into from
Dec 21, 2023
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
11 changes: 2 additions & 9 deletions kotlinx-coroutines-core/common/src/sync/Mutex.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,12 @@ public suspend inline fun <T> Mutex.withLock(owner: Any? = null, action: () -> T
contract {
callsInPlace(action, InvocationKind.EXACTLY_ONCE)
}

// Cannot use 'finally' in this function because of KT-58685
// See kotlinx.coroutines.sync.MutexTest.testWithLockJsMiscompilation

lock(owner)
val result = try {
return try {
action()
} catch (e: Throwable) {
} finally {
unlock(owner)
throw e
}
unlock(owner)
return result
}


Expand Down
11 changes: 2 additions & 9 deletions kotlinx-coroutines-core/common/src/sync/Semaphore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,12 @@ public suspend inline fun <T> Semaphore.withPermit(action: () -> T): T {
contract {
callsInPlace(action, InvocationKind.EXACTLY_ONCE)
}

// Cannot use 'finally' in this function because of KT-58685
// See kotlinx.coroutines.sync.SemaphoreTest.testWithPermitJsMiscompilation

acquire()
val result = try {
return try {
action()
} catch (e: Throwable) {
} finally {
release()
throw e
}
release()
return result
}

@Suppress("UNCHECKED_CAST")
Expand Down
111 changes: 111 additions & 0 deletions kotlinx-coroutines-core/common/test/channels/ConsumeTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright 2016-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

@file:OptIn(DelicateCoroutinesApi::class)
package kotlinx.coroutines.channels

import kotlinx.coroutines.*
import kotlin.test.*

class ConsumeTest: TestBase() {

/** Check that [ReceiveChannel.consume] does not suffer from KT-58685 */
@Test
fun testConsumeJsMiscompilation() = runTest {
val channel = Channel<Int>()
assertFailsWith<IndexOutOfBoundsException> {
try {
channel.consume { null } ?: throw IndexOutOfBoundsException() // should throw…
} catch (e: Exception) {
throw e // …but instead fails here
}
}
}

/** Checks that [ReceiveChannel.consume] closes the channel when the block executes successfully. */
@Test
fun testConsumeClosesOnSuccess() = runTest {
val channel = Channel<Int>()
channel.consume { }
assertTrue(channel.isClosedForReceive)
}

/** Checks that [ReceiveChannel.consume] closes the channel when the block executes successfully. */
@Test
fun testConsumeClosesOnFailure() = runTest {
val channel = Channel<Int>()
try {
channel.consume { throw TestException() }
} catch (e: TestException) {
// Expected
}
assertTrue(channel.isClosedForReceive)
}

/** Checks that [ReceiveChannel.consume] closes the channel when the block does an early return. */
@Test
fun testConsumeClosesOnEarlyReturn() = runTest {
val channel = Channel<Int>()
fun f() {
try {
channel.consume { return }
} catch (e: TestException) {
// Expected
}
}
f()
assertTrue(channel.isClosedForReceive)
}

/** Checks that [ReceiveChannel.consume] closes the channel when the block executes successfully. */
@Test
fun testConsumeEachClosesOnSuccess() = runTest {
val channel = Channel<Int>(Channel.UNLIMITED)
launch { channel.close() }
channel.consumeEach { fail("unreached") }
assertTrue(channel.isClosedForReceive)
}

/** Checks that [ReceiveChannel.consume] closes the channel when the block executes successfully. */
@Test
fun testConsumeEachClosesOnFailure() = runTest {
val channel = Channel<Unit>(Channel.UNLIMITED)
channel.send(Unit)
try {
channel.consumeEach { throw TestException() }
} catch (e: TestException) {
// Expected
}
assertTrue(channel.isClosedForReceive)
}

/** Checks that [ReceiveChannel.consume] closes the channel when the block does an early return. */
@Test
fun testConsumeEachClosesOnEarlyReturn() = runTest {
val channel = Channel<Unit>(Channel.UNLIMITED)
channel.send(Unit)
suspend fun f() {
channel.consumeEach {
return@f
}
}
f()
assertTrue(channel.isClosedForReceive)
}

/** Check that [BroadcastChannel.consume] does not suffer from KT-58685 */
@OptIn(ObsoleteCoroutinesApi::class)
@Suppress("DEPRECATION")
@Test
fun testBroadcastChannelConsumeJsMiscompilation() = runTest {
val channel = BroadcastChannel<Int>(1)
assertFailsWith<IndexOutOfBoundsException> {
try {
channel.consume { null } ?: throw IndexOutOfBoundsException() // should throw…
} catch (e: Exception) {
throw e // …but instead fails here
}
}
}
}
18 changes: 17 additions & 1 deletion kotlinx-coroutines-core/common/test/sync/MutexTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,27 @@ class MutexTest : TestBase() {
try {
mutex.withLock {
expect(1)
assertTrue(mutex.isLocked)
throw TestException()
}
} catch (e: TestException) {
finish(2)
expect(2)
}
assertFalse(mutex.isLocked)
finish(3)
}

@Test
fun withLockOnEarlyReturnTest() = runTest {
val mutex = Mutex()
assertFalse(mutex.isLocked)
suspend fun f() {
mutex.withLock {
assertTrue(mutex.isLocked)
return@f
}
}
f()
assertFalse(mutex.isLocked)
}

Expand Down
29 changes: 29 additions & 0 deletions kotlinx-coroutines-core/common/test/sync/SemaphoreTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,35 @@ class SemaphoreTest : TestBase() {
assertEquals(1, semaphore.availablePermits)
}

@Test
fun withSemaphoreOnFailureTest() = runTest {
val semaphore = Semaphore(1)
assertEquals(1, semaphore.availablePermits)
try {
semaphore.withPermit {
assertEquals(0, semaphore.availablePermits)
throw TestException()
}
} catch (e: TestException) {
// Expected
}
assertEquals(1, semaphore.availablePermits)
}

@Test
fun withSemaphoreOnEarlyReturnTest() = runTest {
val semaphore = Semaphore(1)
assertEquals(1, semaphore.availablePermits)
suspend fun f() {
semaphore.withPermit {
assertEquals(0, semaphore.availablePermits)
return@f
}
}
f()
assertEquals(1, semaphore.availablePermits)
}

@Test
fun fairnessTest() = runTest {
val semaphore = Semaphore(1)
Expand Down