Uploaded image for project: 'C Driver'
  1. C Driver
  2. CDRIVER-3702

Add test assertions to check lock preconditions

    XMLWordPrintableJSON

Details

    • Icon: Improvement Improvement
    • Resolution: Fixed
    • Icon: Major - P3 Major - P3
    • 1.18.0, 1.18.0-alpha
    • 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.

      Attachments

        Activity

          People

            andrew.witten@mongodb.com Andrew Witten (Inactive)
            kevin.albertson@mongodb.com Kevin Albertson
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: