Description
Reported in https://github.com/couchbase/couchbase-lite-C/issues/288.
Analysis from the report:
I think that the root cause is that it is unsafe to call C4Query::enableObserver multiple times with the last observer.
The ListenerToken of a query disables itself in its destructor, which leads to a call of C4Query::enableObserver, and when the C4QueryObserver that is owned by the ListenerToken is implicitly destroyed C4Query::enableObserver is called again.The problem with C4Query::enableObserver is that it allows multiple consecutive calls to enter this code section which calls LiveQuerier::stop, an actor method which executes asynchronously.
When the LiverQuerier has stopped, C4Query::liveQuerierStopped is called, which releases _bgQuerier.
In this case, this happens while the second call to C4Query::enableObserver is still executing.