Skip to content

Commit

Permalink
Workaround for KT-58685
Browse files Browse the repository at this point in the history
  • Loading branch information
CLOVIS-AI committed Sep 27, 2023
1 parent 14ea7af commit 9c42130
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 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

0 comments on commit 9c42130

Please sign in to comment.