The sample code for withTransaction does not set an explicit session for the calls to mongoc_collection_insert_one. This means these write operations are not actually part of the transaction. To associate these write operations with the transaction, the session ID should be passed as part of the opts argument.
Additionally, the sample code passes the reply argument to the mongoc_collection_insert_one calls, but that is not the intended use of the parameter. The bson_t that reply points to is NULL, then the callback can choose to construct a bson_t if propagating a reply is desired. To be clear, this code isn't incorrect, but passing *reply to write operations when it is actually NULL is misleading. In most cases, passing NULL explicitly as the reply is suitable.
All in all, sample code that looks like this:
ret = mongoc_collection_insert_one(coll, doc, NULL, *reply, error);
should be amended to look like this:
bson_t *opts = bson_new(); BSON_ASSERT(mongoc_client_session_append(session, opts, error)); ret = mongoc_collection_insert_one(coll, doc, opts, NULL, error); bson_free(opts);