In pooled mode, while the background thread is scanning topology->scanning is true. If the scan is finishing while an application thread requests a scan, that means the current scan hasn't found a suitable server for the application operation and probably won't. The scan should repeat ASAP (after minHeartbeatFrequencyMS), but instead the request is lost and the background thread sleeps the full heartbeatFrequencyMS (default 10 seconds).
The code is effectively:
void request_scan () { lock (mutex); if (!topology->scanning) { cond_signal (cond); } unlock (mutex); } void background_scanner () { for (;;) { lock (mutex); topology->scanning = true; unlock (mutex); scan (); lock (mutex); topology->scanning = false; cond_timedwait (cond, heartbeatFrequencyMS); unlock (mutex); sleep_until_min_heartbeat_frequency_passed (); } }
The lost notification happens if topology->scanning is about to be set false and mongoc_topology_request_scan checks it. The scan should recur ASAP, but waits 10 seconds instead.
The solution is to not check if topology-scanning in mongoc_topology_request_scan. Instead, always signal the condition.