Skip to content

CDRIVER-1330 remove automatic init/cleanup #1938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .evergreen/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ variables:
set -o errexit
set -o verbose

cmake -DENABLE_TESTS=OFF -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=mongoc -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF . && make -j8 && make install
cmake -DENABLE_TESTS=OFF -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=mongoc . && make -j8 && make install
git clone https://github.com/mongodb/mongo-c-driver-performance.git
cd mongo-c-driver-performance
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=../mongoc . && make -j8
Expand Down
1 change: 0 additions & 1 deletion .evergreen/scripts/compile-openssl-static.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ configure_flags_append_if_not_null() {
}

configure_flags_append "-DCMAKE_SKIP_RPATH=TRUE" # Avoid hardcoding absolute paths to dependency libraries.
configure_flags_append "-DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF"
configure_flags_append "-DENABLE_MAINTAINER_FLAGS=ON"
configure_flags_append "-DENABLE_SSL=OPENSSL"
configure_flags_append "-DOPENSSL_USE_STATIC_LIBS=ON"
Expand Down
1 change: 0 additions & 1 deletion .evergreen/scripts/compile-scan-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ configure_flags_append_if_not_null() {

configure_flags_append "-DCMAKE_PREFIX_PATH=${cmake_prefix_path}"
configure_flags_append "-DCMAKE_SKIP_RPATH=TRUE" # Avoid hardcoding absolute paths to dependency libraries.
configure_flags_append "-DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF"
configure_flags_append "-DENABLE_CLIENT_SIDE_ENCRYPTION=ON"
configure_flags_append "-DENABLE_DEBUG_ASSERTIONS=ON"
configure_flags_append "-DENABLE_MAINTAINER_FLAGS=ON"
Expand Down
1 change: 0 additions & 1 deletion .evergreen/scripts/compile-std.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ configure_flags_append_if_not_null() {

configure_flags_append "-DCMAKE_PREFIX_PATH=${install_dir}"
configure_flags_append "-DCMAKE_SKIP_RPATH=TRUE" # Avoid hardcoding absolute paths to dependency libraries.
configure_flags_append "-DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF"
configure_flags_append "-DENABLE_CLIENT_SIDE_ENCRYPTION=ON"
configure_flags_append "-DENABLE_DEBUG_ASSERTIONS=ON"
configure_flags_append "-DENABLE_MAINTAINER_FLAGS=ON"
Expand Down
1 change: 0 additions & 1 deletion .evergreen/scripts/compile-unix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ configure_flags_append_if_not_null() {
configure_flags_append "-DCMAKE_INSTALL_PREFIX=${install_dir}"
configure_flags_append "-DCMAKE_PREFIX_PATH=${cmake_prefix_path}"
configure_flags_append "-DCMAKE_SKIP_RPATH=TRUE" # Avoid hardcoding absolute paths to dependency libraries.
configure_flags_append "-DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF"
configure_flags_append "-DENABLE_HTML_DOCS=OFF"
configure_flags_append "-DENABLE_MAINTAINER_FLAGS=ON"
configure_flags_append "-DENABLE_MAN_PAGES=OFF"
Expand Down
23 changes: 0 additions & 23 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -186,29 +186,6 @@ mongo_bool_setting(
)

# Deprecated options:
mongo_bool_setting(
ENABLE_AUTOMATIC_INIT_AND_CLEANUP
"[Deprecated] Enable automatic initialization of the C driver library"
DEFAULT EVAL [[
set(DEFAULT OFF)
if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
message(VERBOSE "For backwards compatibility, when using GCC the default of "
"ENABLE_AUTOMATIC_INIT_AND_CLEANUP is ON")
set(DEFAULT ON)
endif()
]]
DEVEL VALUE OFF
VALIDATE CODE [[
if(ENABLE_AUTOMATIC_INIT_AND_CLEANUP)
if(NOT CMAKE_C_COMPILER_ID STREQUAL "GNU")
message(WARNING "ENABLE_AUTOMATIC_INIT_AND_CLEANUP is only supported on GCC.")
endif()
message(DEPRECATION
"Enabling ENABLE_AUTOMATIC_INIT_AND_CLEANUP is deprecated and "
"may be removed in a future release")
endif()
]]
)
mongo_bool_setting(
ENABLE_EXTRA_ALIGNMENT
"[Deprecated] Enable extra alignment on libbson types"
Expand Down
1 change: 0 additions & 1 deletion Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ build:
COPY +version-current/ $source_dir
ENV CCACHE_HOME=/root/.cache/ccache
RUN cmake -S "$source_dir" -B "$build_dir" -G "Ninja Multi-Config" \
-D ENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF \
-D ENABLE_MAINTAINER_FLAGS=ON \
-D ENABLE_SHM_COUNTERS=ON \
-D ENABLE_EXTRA_ALIGNMENT=OFF \
Expand Down
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Unreleased (2.0.0)
* The associated Windows-only option `CYRUS_PLUGIN_PATH_PREFIX` is removed.
* Support for the deprecated `minPoolSize` URI option is removed along with associated API: `MONGOC_URI_MINPOOLSIZE` and `mongoc_client_pool_min_size`.
* Support for LibreSSL (the CMake option `ENABLE_SSL=LIBRESSL`) is removed. Associated API is removed (`MONGOC_ENABLE_SSL_LIBRESSL` and `mongoc_stream_tls_libressl_new`).
* The deprecated CMake option `ENABLE_AUTOMATIC_INIT_AND_CLEANUP` is removed. See [Initialization and cleanup](https://mongoc.org/libmongoc/1.30.2/init-cleanup.html) for expected use of `mongoc_init()` and `mongoc_cleanup()`.

### Forwarding headers (`#include <bson.h>` and `#include <mongoc.h>`)

Expand Down
6 changes: 0 additions & 6 deletions src/libmongoc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,6 @@ add_feature_info(
"authenticate with MongoDB servers using SASL: “Simple Authentication and Security Layer” (${SASL_BACKEND})"
)

if (ENABLE_AUTOMATIC_INIT_AND_CLEANUP)
set (MONGOC_NO_AUTOMATIC_GLOBALS 0)
else ()
set (MONGOC_NO_AUTOMATIC_GLOBALS 1)
endif ()

if (WIN32)
SET (CMAKE_EXTRA_INCLUDE_FILES "ws2tcpip.h")
else ()
Expand Down
2 changes: 2 additions & 0 deletions src/libmongoc/doc/errors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Many C Driver functions report errors by returning ``false`` or -1 and filling o
+-----------------------------------------+----------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| | ``MONGOC_ERROR_CLIENT_INVALID_LOAD_BALANCER`` | You attempted to connect to a MongoDB server behind a load balancer, but the server does not advertize load balanced support. |
+-----------------------------------------+----------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| | ``MONGOC_ERROR_CLIENT_NOT_READY`` | You attempted to create a client but did not previously call :symbol:`mongoc_init` |
+-----------------------------------------+----------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| ``MONGOC_ERROR_STREAM`` | ``MONGOC_ERROR_STREAM_NAME_RESOLUTION`` | DNS failure. |
+-----------------------------------------+----------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| | ``MONGOC_ERROR_STREAM_SOCKET`` | Timeout communicating with server, or connection closed. |
Expand Down
11 changes: 1 addition & 10 deletions src/libmongoc/doc/init-cleanup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,4 @@ Synopsis
mongoc_init
mongoc_cleanup

Deprecated feature: automatic initialization and cleanup
--------------------------------------------------------

On some platforms the driver can automatically call :symbol:`mongoc_init` before ``main``, and call :symbol:`mongoc_cleanup` as the process exits. This is problematic in situations where related libraries also execute cleanup code on shutdown, and it creates inconsistent rules across platforms. Therefore the automatic initialization and cleanup feature is deprecated, and will be dropped in version 2.0. Meanwhile, for backward compatibility, the feature is *enabled* by default on platforms where it is available.

For portable, future-proof code, always call :symbol:`mongoc_init` and :symbol:`mongoc_cleanup` yourself, and configure the driver like:

.. code-block:: none

cmake -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF
.. versionchanged:: 2.0.0 Versions prior to 2.0.0 supported a non-portable automatic initialization and cleanup with the CMake option ``ENABLE_AUTOMATIC_INIT_AND_CLEANUP``. This was removed in 2.0.0. Ensure your application call :symbol:`mongoc_init` and :symbol:`mongoc_cleanup`.
2 changes: 1 addition & 1 deletion src/libmongoc/examples/parse_handshake_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"MONGOC_MD_FLAG_EXPERIMENTAL_FEATURES": 10,
"MONGOC_MD_FLAG_HAVE_SASL_CLIENT_DONE": 11,
"MONGOC_MD_FLAG_HAVE_WEAK_SYMBOLS": 12,
"MONGOC_MD_FLAG_NO_AUTOMATIC_GLOBALS": 13,
"MONGOC_MD_FLAG_NO_AUTOMATIC_GLOBALS": 13, # Removed in CDRIVER-1330.
"MONGOC_MD_FLAG_ENABLE_SSL_LIBRESSL": 14, # Removed in CDRIVER-5693.
"MONGOC_MD_FLAG_ENABLE_SASL_CYRUS": 15,
"MONGOC_MD_FLAG_ENABLE_SASL_SSPI": 16,
Expand Down
9 changes: 9 additions & 0 deletions src/libmongoc/src/mongoc/mongoc-client-pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ mongoc_client_pool_new_with_error (const mongoc_uri_t *uri, bson_error_t *error)

BSON_ASSERT (uri);

extern bool mongoc_get_init_called (void);
if (!mongoc_get_init_called ()) {
_mongoc_set_error (error,
MONGOC_ERROR_CLIENT,
MONGOC_ERROR_CLIENT_NOT_READY,
"Attempting to create client pool, but libmongoc not initialized. Call mongoc_init");
return NULL;
}

#ifndef MONGOC_ENABLE_SSL
if (mongoc_uri_get_tls (uri)) {
_mongoc_set_error (error,
Expand Down
9 changes: 9 additions & 0 deletions src/libmongoc/src/mongoc/mongoc-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,15 @@ mongoc_client_new_from_uri_with_error (const mongoc_uri_t *uri, bson_error_t *er

BSON_ASSERT (uri);

extern bool mongoc_get_init_called (void);
if (!mongoc_get_init_called ()) {
_mongoc_set_error (error,
MONGOC_ERROR_CLIENT,
MONGOC_ERROR_CLIENT_NOT_READY,
"Attempting to create client, but libmongoc not initialized. Call mongoc_init");
return NULL;
}

#ifndef MONGOC_ENABLE_SSL
if (mongoc_uri_get_tls (uri)) {
_mongoc_set_error (error,
Expand Down
11 changes: 0 additions & 11 deletions src/libmongoc/src/mongoc/mongoc-config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,6 @@
# undef MONGOC_HAVE_SASL_CLIENT_DONE
#endif


/*
* Disable automatic calls to mongoc_init() and mongoc_cleanup()
* before main() is called, and after exit() (respectively).
*/
#define MONGOC_NO_AUTOMATIC_GLOBALS @MONGOC_NO_AUTOMATIC_GLOBALS@

#if MONGOC_NO_AUTOMATIC_GLOBALS != 1
# undef MONGOC_NO_AUTOMATIC_GLOBALS
#endif

/*
* MONGOC_HAVE_SOCKLEN is set from configure to determine if we
* need to emulate the type.
Expand Down
4 changes: 2 additions & 2 deletions src/libmongoc/src/mongoc/mongoc-handshake-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ typedef enum {
MONGOC_MD_FLAG_EXPERIMENTAL_FEATURES = 10,
MONGOC_MD_FLAG_HAVE_SASL_CLIENT_DONE = 11,
MONGOC_MD_FLAG_HAVE_WEAK_SYMBOLS = 12,
MONGOC_MD_FLAG_NO_AUTOMATIC_GLOBALS = 13,
MONGOC_MD_FLAG_ENABLE_SSL_LIBRESSL_UNUSED = 14, // Removed in CDRIVER-5693.
MONGOC_MD_FLAG_NO_AUTOMATIC_GLOBALS_UNUSED = 13, // Removed in CDRIVER-1330.
MONGOC_MD_FLAG_ENABLE_SSL_LIBRESSL_UNUSED = 14, // Removed in CDRIVER-5693.
MONGOC_MD_FLAG_ENABLE_SASL_CYRUS = 15,
MONGOC_MD_FLAG_ENABLE_SASL_SSPI = 16,
MONGOC_MD_FLAG_HAVE_SOCKLEN = 17,
Expand Down
4 changes: 0 additions & 4 deletions src/libmongoc/src/mongoc/mongoc-handshake.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,6 @@ _mongoc_handshake_get_config_hex_string (void)
_set_bit (bf, byte_count, MONGOC_MD_FLAG_HAVE_SASL_CLIENT_DONE);
#endif

#ifdef MONGOC_NO_AUTOMATIC_GLOBALS
_set_bit (bf, byte_count, MONGOC_MD_FLAG_NO_AUTOMATIC_GLOBALS);
#endif

#ifdef MONGOC_EXPERIMENTAL_FEATURES
_set_bit (bf, byte_count, MONGOC_MD_FLAG_EXPERIMENTAL_FEATURES);
#endif
Expand Down
35 changes: 7 additions & 28 deletions src/libmongoc/src/mongoc/mongoc-init.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@
#include <mongoc/mongoc-ocsp-cache-private.h>
#endif

#ifndef MONGOC_NO_AUTOMATIC_GLOBALS
#pragma message("Configure the driver with ENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF.\
Automatic cleanup is deprecated and will be removed in version 2.0.")
#endif

// CDRIVER-2722: Cyrus SASL is deprecated on MacOS.
#if defined(MONGOC_ENABLE_SASL_CYRUS) && defined(__APPLE__)
BEGIN_IGNORE_DEPRECATIONS
Expand Down Expand Up @@ -95,9 +90,16 @@ mongoc_cyrus_mutex_free (void *mutex)

#endif /* MONGOC_ENABLE_SASL_CYRUS */

static bool mongoc_init_called;
bool
mongoc_get_init_called (void)
{
return mongoc_init_called;
}

static BSON_ONCE_FUN (_mongoc_do_init)
{
mongoc_init_called = true;
#ifdef MONGOC_ENABLE_SASL_CYRUS
int status;
#endif
Expand Down Expand Up @@ -204,29 +206,6 @@ mongoc_cleanup (void)
bson_once (&once, _mongoc_do_cleanup);
}

/*
* On GCC, just use __attribute__((constructor)) to perform initialization
* automatically for the application.
*/
#if defined(__GNUC__) && !defined(MONGOC_NO_AUTOMATIC_GLOBALS)
static void
_mongoc_init_ctor (void) __attribute__ ((constructor));
static void
_mongoc_init_ctor (void)
{
mongoc_init ();
}

static void
_mongoc_init_dtor (void) __attribute__ ((destructor));
static void
_mongoc_init_dtor (void)
{
bson_mem_restore_vtable ();
mongoc_cleanup ();
}
#endif

// CDRIVER-2722: Cyrus SASL is deprecated on MacOS.
#if defined(MONGOC_ENABLE_SASL_CYRUS) && defined(__APPLE__)
BEGIN_IGNORE_DEPRECATIONS
Expand Down
4 changes: 1 addition & 3 deletions src/libmongoc/tests/test-mongoc-handshake.c
Original file line number Diff line number Diff line change
Expand Up @@ -1189,9 +1189,7 @@ test_handshake_platform_config (void)
BSON_ASSERT (_get_bit (config_str, MONGOC_MD_FLAG_HAVE_SASL_CLIENT_DONE));
#endif

#ifdef MONGOC_NO_AUTOMATIC_GLOBALS
BSON_ASSERT (_get_bit (config_str, MONGOC_MD_FLAG_NO_AUTOMATIC_GLOBALS));
#endif
BSON_ASSERT (!_get_bit (config_str, MONGOC_MD_FLAG_NO_AUTOMATIC_GLOBALS_UNUSED)); // Flag was removed.

#ifdef MONGOC_EXPERIMENTAL_FEATURES
BSON_ASSERT (_get_bit (config_str, MONGOC_MD_FLAG_EXPERIMENTAL_FEATURES));
Expand Down