JetBrains / JetBrains/Exposed

Using `forUpdate` in suspended transaction causes deadlock

Open
#2,411 6 comments 3 reactions 0 assignees View on GitHub
Dominant language
Kotlin
Stars
9.3k
Forks
798
Avg merge
4d 2h
Merged PRs (30d)
26

Description

Reproducer:

```
fun main(args: Array) {
Database.connect(
"jdbc:postgresql://localhost:54321/grazie",
user = "admin",
password = "admin",
driver = "org.postgresql.Driver"
)
while (true) {
try {
runBlocking {
repeat(2) {
launch {
newSuspendedTransaction {
val license = UserLicenseEntity.find { UserLicenseTable.id eq 1 }.forUpdate().singleOrNull()
println(license?.licenseType ?: "No license found")
delay(50)
}
}
}
}
} catch (e: Exception) {
}
}
}

class UserLicenseEntity(id: EntityID) : IntEntity(id) {
val licenseType by UserLicenseTable.license_type

companion object : IntEntityClass(UserLicenseTable)
}

object UserLicenseTable : IntIdTable("user_license") {
val license_type = text("license_type")
}
```

If I am not missing something, it should run endless and print licenseType. Instead, licenseType is printed once and then the program hangs

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.