Hi, i came across an interesting deadlock issue on...
# exposed
r
Hi, i came across an interesting deadlock issue on class initialization related to circular references between two exposed tables. here's the offending structure:
Copy code
object ItemTable : IntIdTable("item") {
    val linkTokenId = optReference("link_token_id", LinkTokenTable)
}

object LinkTokenTable : IntIdTable("link_token") {
    val itemId = optReference("item_id", ItemTable)
}
on a cold JVM where neither of those classes have been initialized yet, if you happen to run two queries in parallel (on separate threads), you can enter a deadlock due to class initialization (similar to what's described here) I created a repro to highlight the issue - github.com/ragboyjr/exposed-init-deadlock-repro
This technically isn't an exposed bug, but rather just a limitation to exposed's Table's as kotlin objects design when two tables references each other. You can replicate this deadlock with just plain kotlin objects referencing each other with slow initializers and then accessing each object in different threads
however, the impacts of the bug are pretty nasty (total deadlock), so, i think there could be value in reconsidering the reference/optReference api to either detecting circular references and require explicit opt-in or by just removing the possibility all together
y
I ran into this issue early this year too. My solution was to write a Detekt rule to flag this issue. Other workaround includes having single-threaded warmups that initialize the tables before it handles traffic, etc.
r
ya, we're doing the warmup as a fix too
what does your detekt rule look like?
y
For each table, we looks for references to another table. (ie. via
reference
/
optReference
) And if there's an edge between A->B and also B->A, then we flag that. Limitation is that we don't detect longer cycles, such as
A → B → C → A
r
got it, we were going to try running SpotBugs because i believe it can catch these circular dependencies
but i haven't tested it on longer cycles