Add test assertions to check lock preconditions

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Fixed
    • Priority: Major - P3
    • 1.18.0, 1.18.0-alpha
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      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.

            Assignee:
            Andrew Witten (Inactive)
            Reporter:
            Kevin Albertson
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: