If an explicit session (i.e. "session" option) is provided to phongo_execute_bulk_write(), the underlying libmongoc client session is assigned to the mongoc_bulk_operation_t object with mongoc_bulk_operation_set_client_session(). This sets the session pointer on the mongoc_bulk_operation_t struct, which is only ever accessed by mongoc_bulk_operation_execute(), which we call later in phongo_execute_bulk_write().
Since we prohibit executing BulkWrite objects multiple times, we can trust that the session will always correspond to the correct libmongoc client and that the mongoc_client_session_destroy() is never called between mongoc_bulk_operation_set_client_session() and mongoc_bulk_operation_execute(). Even if the incoming "session" option had only a single reference (e.g. ["session" => $manager->startSession()]), it would exist for the duration of the executeBulkWrite() method call before the object is cleaned up by php_phongo_session_free_object() and mongoc_client_session_destroy() is called.
That said, we are still technically leaving a dangling pointer on the the mongoc_bulk_operation_t object contained within the BulkWrite object. Unfortunately, mongoc_bulk_operation_set_client_session() does not allow us to reset the session to NULL, so addressing this may require us to track a reference to the Session object on the BulkWrite object as it is executed. This would be comparable to what was done for Cursors in PHPC-1151.
- is related to
-
PHPC-1151 Possible segfault is Session object is freed before Cursor
- Closed