Skip to content

Commit

Permalink
Work around KT-58685 (#3881)
Browse files Browse the repository at this point in the history
Implement `withLock` and `withPermit` without try/finally,
as the idiomatic implementation leads to incorrect code
being produced by the compiler.
  • Loading branch information
CLOVIS-AI committed Nov 2, 2023
1 parent 2b5d93f commit 5a570e1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
12 changes: 9 additions & 3 deletions kotlinx-coroutines-core/common/src/sync/Mutex.kt
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,18 @@ public suspend inline fun <T> Mutex.withLock(owner: Any? = null, action: () -> T
callsInPlace(action, InvocationKind.EXACTLY_ONCE)
}

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

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


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

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

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

@Suppress("UNCHECKED_CAST")
Expand Down
16 changes: 16 additions & 0 deletions kotlinx-coroutines-core/common/test/sync/MutexTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,20 @@ class MutexTest : TestBase() {
assertFailsWith<IllegalStateException> { mutex.lock(owner) }
assertFailsWith<IllegalStateException> { select { mutex.onLock(owner) {} } }
}

@Test
fun testWithLockJsMiscompilation() = runTest {
// This is a reproducer for KT-58685
// On Kotlin/JS IR, the compiler miscompiles calls to 'unlock' in an inlined finally
// This is visible on the withLock function
// Until the compiler bug is fixed, this test case checks that we do not suffer from it
val mutex = Mutex()
assertFailsWith<IndexOutOfBoundsException> {
try {
mutex.withLock { 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/SemaphoreTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,20 @@ class SemaphoreTest : TestBase() {
assertFailsWith<IllegalArgumentException> { Semaphore(1, -1) }
assertFailsWith<IllegalArgumentException> { Semaphore(1, 2) }
}
}

@Test
fun testWithPermitJsMiscompilation() = runTest {
// This is a reproducer for KT-58685
// On Kotlin/JS IR, the compiler miscompiles calls to 'release' in an inlined finally
// This is visible on the withPermit function
// Until the compiler bug is fixed, this test case checks that we do not suffer from it
val semaphore = Semaphore(1)
assertFailsWith<IndexOutOfBoundsException> {
try {
semaphore.withPermit { null } ?: throw IndexOutOfBoundsException() // should throw…
} catch (e: Exception) {
throw e // …but instead fails here
}
}
}
}

0 comments on commit 5a570e1

Please sign in to comment.