Posted by closure_tree here:
d. Healing recommendations are usually correct - you should purchase table locks in the same order to prevent deadlocks. In this case, however, the deadlock is due to row locks in the hierarchy table.
Interestingly, you suggest using with_advisory_lock ! I just wrote this library for clos_tree, and if you are using version> = 3.7.0, there are already advisory locks protecting class methods #rebuild and #find_or_create_by_path at the class level.
The problem with advisory locks (at least with MySQL and PostgreSQL) is that they do not respect transaction boundaries - if the caller does not transfer its transaction before the lock is released, other connections will not see these when they try to get advisory lock, so we need to be careful. We may need to add a table lock to the hierarchy table for any records, but this will be in the worst case.
I opened question 41 , and we can track it there. The first thing to do is to reliably reproduce the dead end in the parallel test. We already have tests that do this for #rebuild and #find_or_create_by_path .
source share