Skip to content

Commit 9c42130

Browse files
committed
Workaround for KT-58685
1 parent 14ea7af commit 9c42130

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

kotlinx-coroutines-core/common/src/sync/Mutex.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,18 @@ public suspend inline fun <T> Mutex.withLock(owner: Any? = null, action: () -> T
122122
callsInPlace(action, InvocationKind.EXACTLY_ONCE)
123123
}
124124

125+
// Cannot use 'finally' in this function because of KT-58685
126+
// See kotlinx.coroutines.sync.MutexTest.testWithLockJsMiscompilation
127+
125128
lock(owner)
126-
try {
127-
return action()
128-
} finally {
129+
val result = try {
130+
action()
131+
} catch (e: Throwable) {
129132
unlock(owner)
133+
throw e
130134
}
135+
unlock(owner)
136+
return result
131137
}
132138

133139

kotlinx-coroutines-core/common/src/sync/Semaphore.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,18 @@ public suspend inline fun <T> Semaphore.withPermit(action: () -> T): T {
8383
callsInPlace(action, InvocationKind.EXACTLY_ONCE)
8484
}
8585

86+
// Cannot use 'finally' in this function because of KT-58685
87+
// See kotlinx.coroutines.sync.SemaphoreTest.testWithPermitJsMiscompilation
88+
8689
acquire()
87-
try {
88-
return action()
89-
} finally {
90+
val result = try {
91+
action()
92+
} catch (e: Throwable) {
9093
release()
94+
throw e
9195
}
96+
release()
97+
return result
9298
}
9399

94100
@Suppress("UNCHECKED_CAST")

0 commit comments

Comments
 (0)