libtracker-data: Add an usage reference early when getting an interface
Currently, there's a gap between a TrackerDBInterface being picked for
execution, and a cursor increasing its usage counter. This opened the
following race condition:
- Random thread executes SPARQL select query, a TrackerDBInterface
is picked.
- TrackerSparqlConnection reaches a threshold/timeout and tries to
release memory, performed by the update thread
- All unused readonly connections are closed
- The random thread proceeds to create a stmt/cursor, finds invalid
memory in place of the TrackerDBInterface
- Whoops
To fix this, increase the usage counter when getting a readonly
interface from the TrackerDBManager, while the connection pool is
locked (thus mutually exclusive with the "release memory" operation)
and decrease it after having created the statement/cursor (at which
point it is additionally increased by the cursor).
This will ensure that an interface is considered "in use" as soon
as it gets out of the readonly connections pool, usage counter will
always be greater than 1 until there are no cursors left.
Fixes: https://gitlab.gnome.org/GNOME/tracker-miners/-/issues/154