-
Type:
Task
-
Resolution: Fixed
-
Priority:
Major - P3
-
Affects Version/s: None
-
Component/s: None
-
None
-
Not Needed
-
None
-
C Drivers
-
Not Needed
-
None
-
None
-
None
-
None
-
None
-
None
Motivated by comments in https://github.com/mongodb/mongo-c-driver/pull/2261.
An implicit assumption of `gMongocHandshake` and `_mongoc_handshake_get()` (in library code) is that, once either:
- `mongoc_handshake_data_append()` is invoked, or
- a client or client pool attempts its first initial connection hanshake,
the value of `gMongocHandshake` is made read-only, aka "frozen", for all subsequent uses. The "frozen" state ensures read-only access is thread-safe without synchronization, as no thread can modify the frozen state. This assumption is disregarded by handshake-related test code which directly modifies `gMongocHandshake` via `_mongoc_handshake_get()` without unnecessary concern for thread-safety.
This PR proposes making `_mongoc_handshake_get()` return `T const*` instead of `T*` to enforce the frozen state and expectation. This enforcement is further strengthened by an unconditional assertion within `_mongoc_handshake_get()` that `_mongoc_handshake_is_frozen()` is true. For testing purposes, a separate `_mongoc_handshake_get_unfrozen()` function is used instead.
The `frozen` data member is changed from `bool` to `int8_t` so that atomic operations can be used to query the frozen state via `mongoc_handshake_is_frozen()` without acquiring a lock on `gHandshakeLock`: given the otherwise _unsynchronized read-only access of `gMongocHandshake` once it has been frozen, it should be undesirable to pay the penalty for unnecessary synchronization.
Note this PR also cherry-picks the ["extraction"](https://github.com/mongodb/mongo-c-driver/pull/2261/commits/0d32fb31f7faed9c30827f6d07735208082bec97) commit from https://github.com/mongodb/mongo-c-driver/pull/2261 to reduce instances of invoking `mongoc_handshake_get()` by library code. This PR goes further and removes _all other instances of `_mongoc_handshake_get()` in favor of direct access to `gMongocHandshake` (in `mongoc-handshake.c`) or using `_mongoc_handshake_get_unfrozen()` (in test code) instead: all remaining instances of `_mongoc_handshake_get()` (in library code) are now consistent with the read-only frozen handshake expectation.
- is related to
-
CDRIVER-5993 Allow Instantiated MongoClients to Send Client Metadata On-Demand
-
- Development Complete
-