**Describe the bug**
One thread calling bm_commit (store manager applying the WAL) and making a bat persistent (calling BATmode), and another thread unloading the same bat (BBPmanager calling BBPtrim) at the same time may cause deadlock. BATmode has a reference to the heaps (bat_iterator) when it calls BBPretain, which calls incref, which waits until unloading is finished; and BBPtrim calls BBPfree which calls BATfree which waits until the heap reference count goes down.  In other words, two threads waiting for each other.

possible deadlock when a bat is made persistent when it is also getting unloaded
This is a GitHub Issue
repo:MonetDB
owner:MonetDB
Title : possible deadlock when a bat is made persistent when it is also getting unloaded
Issue date:

--- start body ---
**Describe the bug**
One thread calling bm_commit (store manager applying the WAL) and making a bat persistent (calling BATmode), and another thread unloading the same bat (BBPmanager calling BBPtrim) at the same time may cause deadlock. BATmode has a reference to the heaps (bat_iterator) when it calls BBPretain, which calls incref, which waits until unloading is finished; and BBPtrim calls BBPfree which calls BATfree which waits until the heap reference count goes down.  In other words, two threads waiting for each other.

--- end body ---
