Details
-
Improvement
-
Resolution: Fixed
-
Major - P3
-
None
-
None
-
None
Description
Many functions in mongoc-topology.c, mongoc-topology-description.c, mongoc-topology-background-monitoring.c, and (after CDRIVER-3535) mongoc-server-monitor.c require callers to lock a mutex. This precondition is usually documented in a function level comment:
/*
|
*--------------------------------------------------------------------------
|
*
|
* mongoc_topology_scan_once --
|
*
|
* Runs a single complete scan.
|
*
|
* NOTE: this method expects @topology's mutex to be locked on entry.
|
*
|
* NOTE: this method unlocks and re-locks @topology's mutex.
|
*
|
* Only runs for single threaded monitoring. (obey_cooldown is always
|
* true).
|
*
|
*--------------------------------------------------------------------------
|
*/
|
static void
|
mongoc_topology_scan_once (mongoc_topology_t *topology, bool obey_cooldown)
|
Though sometimes these preconditions are missed. Here is one such real life example: https://github.com/mongodb/mongo-c-driver/pull/609#discussion_r424602399
It may be worth adding test-only assertions in these functions to check that the expected mutex is locked. pthread_mutex_try_lock could accomplish this on POSIX and TryEnterCriticalSection on Windows.