Skip to content

Commit 1ca2bfa

Browse files
authored
CDRIVER-1330 remove automatic init/cleanup (#1938)
* remove automatic init/cleanup * add check that `mongoc_init` is called on client creation
1 parent 82a294f commit 1ca2bfa

19 files changed

+34
-94
lines changed

.evergreen/benchmark.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ variables:
3535
set -o errexit
3636
set -o verbose
3737
38-
cmake -DENABLE_TESTS=OFF -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=mongoc -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF . && make -j8 && make install
38+
cmake -DENABLE_TESTS=OFF -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=mongoc . && make -j8 && make install
3939
git clone https://github.com/mongodb/mongo-c-driver-performance.git
4040
cd mongo-c-driver-performance
4141
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=../mongoc . && make -j8

.evergreen/scripts/compile-openssl-static.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ configure_flags_append_if_not_null() {
4141
}
4242

4343
configure_flags_append "-DCMAKE_SKIP_RPATH=TRUE" # Avoid hardcoding absolute paths to dependency libraries.
44-
configure_flags_append "-DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF"
4544
configure_flags_append "-DENABLE_MAINTAINER_FLAGS=ON"
4645
configure_flags_append "-DENABLE_SSL=OPENSSL"
4746
configure_flags_append "-DOPENSSL_USE_STATIC_LIBS=ON"

.evergreen/scripts/compile-scan-build.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ configure_flags_append_if_not_null() {
3232

3333
configure_flags_append "-DCMAKE_PREFIX_PATH=${cmake_prefix_path}"
3434
configure_flags_append "-DCMAKE_SKIP_RPATH=TRUE" # Avoid hardcoding absolute paths to dependency libraries.
35-
configure_flags_append "-DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF"
3635
configure_flags_append "-DENABLE_CLIENT_SIDE_ENCRYPTION=ON"
3736
configure_flags_append "-DENABLE_DEBUG_ASSERTIONS=ON"
3837
configure_flags_append "-DENABLE_MAINTAINER_FLAGS=ON"

.evergreen/scripts/compile-std.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ configure_flags_append_if_not_null() {
4040

4141
configure_flags_append "-DCMAKE_PREFIX_PATH=${install_dir}"
4242
configure_flags_append "-DCMAKE_SKIP_RPATH=TRUE" # Avoid hardcoding absolute paths to dependency libraries.
43-
configure_flags_append "-DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF"
4443
configure_flags_append "-DENABLE_CLIENT_SIDE_ENCRYPTION=ON"
4544
configure_flags_append "-DENABLE_DEBUG_ASSERTIONS=ON"
4645
configure_flags_append "-DENABLE_MAINTAINER_FLAGS=ON"

.evergreen/scripts/compile-unix.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ configure_flags_append_if_not_null() {
6868
configure_flags_append "-DCMAKE_INSTALL_PREFIX=${install_dir}"
6969
configure_flags_append "-DCMAKE_PREFIX_PATH=${cmake_prefix_path}"
7070
configure_flags_append "-DCMAKE_SKIP_RPATH=TRUE" # Avoid hardcoding absolute paths to dependency libraries.
71-
configure_flags_append "-DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF"
7271
configure_flags_append "-DENABLE_HTML_DOCS=OFF"
7372
configure_flags_append "-DENABLE_MAINTAINER_FLAGS=ON"
7473
configure_flags_append "-DENABLE_MAN_PAGES=OFF"

CMakeLists.txt

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -186,29 +186,6 @@ mongo_bool_setting(
186186
)
187187

188188
# Deprecated options:
189-
mongo_bool_setting(
190-
ENABLE_AUTOMATIC_INIT_AND_CLEANUP
191-
"[Deprecated] Enable automatic initialization of the C driver library"
192-
DEFAULT EVAL [[
193-
set(DEFAULT OFF)
194-
if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
195-
message(VERBOSE "For backwards compatibility, when using GCC the default of "
196-
"ENABLE_AUTOMATIC_INIT_AND_CLEANUP is ON")
197-
set(DEFAULT ON)
198-
endif()
199-
]]
200-
DEVEL VALUE OFF
201-
VALIDATE CODE [[
202-
if(ENABLE_AUTOMATIC_INIT_AND_CLEANUP)
203-
if(NOT CMAKE_C_COMPILER_ID STREQUAL "GNU")
204-
message(WARNING "ENABLE_AUTOMATIC_INIT_AND_CLEANUP is only supported on GCC.")
205-
endif()
206-
message(DEPRECATION
207-
"Enabling ENABLE_AUTOMATIC_INIT_AND_CLEANUP is deprecated and "
208-
"may be removed in a future release")
209-
endif()
210-
]]
211-
)
212189
mongo_bool_setting(
213190
ENABLE_EXTRA_ALIGNMENT
214191
"[Deprecated] Enable extra alignment on libbson types"

Earthfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ build:
3737
COPY +version-current/ $source_dir
3838
ENV CCACHE_HOME=/root/.cache/ccache
3939
RUN cmake -S "$source_dir" -B "$build_dir" -G "Ninja Multi-Config" \
40-
-D ENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF \
4140
-D ENABLE_MAINTAINER_FLAGS=ON \
4241
-D ENABLE_SHM_COUNTERS=ON \
4342
-D ENABLE_EXTRA_ALIGNMENT=OFF \

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Unreleased (2.0.0)
4444
* The associated Windows-only option `CYRUS_PLUGIN_PATH_PREFIX` is removed.
4545
* Support for the deprecated `minPoolSize` URI option is removed along with associated API: `MONGOC_URI_MINPOOLSIZE` and `mongoc_client_pool_min_size`.
4646
* 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`).
47+
* 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()`.
4748

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

src/libmongoc/CMakeLists.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -380,12 +380,6 @@ add_feature_info(
380380
"authenticate with MongoDB servers using SASL: “Simple Authentication and Security Layer” (${SASL_BACKEND})"
381381
)
382382

383-
if (ENABLE_AUTOMATIC_INIT_AND_CLEANUP)
384-
set (MONGOC_NO_AUTOMATIC_GLOBALS 0)
385-
else ()
386-
set (MONGOC_NO_AUTOMATIC_GLOBALS 1)
387-
endif ()
388-
389383
if (WIN32)
390384
SET (CMAKE_EXTRA_INCLUDE_FILES "ws2tcpip.h")
391385
else ()

src/libmongoc/doc/errors.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Many C Driver functions report errors by returning ``false`` or -1 and filling o
3535
+-----------------------------------------+----------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
3636
| | ``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. |
3737
+-----------------------------------------+----------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
38+
| | ``MONGOC_ERROR_CLIENT_NOT_READY`` | You attempted to create a client but did not previously call :symbol:`mongoc_init` |
39+
+-----------------------------------------+----------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
3840
| ``MONGOC_ERROR_STREAM`` | ``MONGOC_ERROR_STREAM_NAME_RESOLUTION`` | DNS failure. |
3941
+-----------------------------------------+----------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
4042
| | ``MONGOC_ERROR_STREAM_SOCKET`` | Timeout communicating with server, or connection closed. |

src/libmongoc/doc/init-cleanup.rst

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,4 @@ Synopsis
1717
mongoc_init
1818
mongoc_cleanup
1919

20-
Deprecated feature: automatic initialization and cleanup
21-
--------------------------------------------------------
22-
23-
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.
24-
25-
For portable, future-proof code, always call :symbol:`mongoc_init` and :symbol:`mongoc_cleanup` yourself, and configure the driver like:
26-
27-
.. code-block:: none
28-
29-
cmake -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF
20+
.. 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`.

src/libmongoc/examples/parse_handshake_cfg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"MONGOC_MD_FLAG_EXPERIMENTAL_FEATURES": 10,
1717
"MONGOC_MD_FLAG_HAVE_SASL_CLIENT_DONE": 11,
1818
"MONGOC_MD_FLAG_HAVE_WEAK_SYMBOLS": 12,
19-
"MONGOC_MD_FLAG_NO_AUTOMATIC_GLOBALS": 13,
19+
"MONGOC_MD_FLAG_NO_AUTOMATIC_GLOBALS": 13, # Removed in CDRIVER-1330.
2020
"MONGOC_MD_FLAG_ENABLE_SSL_LIBRESSL": 14, # Removed in CDRIVER-5693.
2121
"MONGOC_MD_FLAG_ENABLE_SASL_CYRUS": 15,
2222
"MONGOC_MD_FLAG_ENABLE_SASL_SSPI": 16,

src/libmongoc/src/mongoc/mongoc-client-pool.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ mongoc_client_pool_new_with_error (const mongoc_uri_t *uri, bson_error_t *error)
136136

137137
BSON_ASSERT (uri);
138138

139+
extern bool mongoc_get_init_called (void);
140+
if (!mongoc_get_init_called ()) {
141+
_mongoc_set_error (error,
142+
MONGOC_ERROR_CLIENT,
143+
MONGOC_ERROR_CLIENT_NOT_READY,
144+
"Attempting to create client pool, but libmongoc not initialized. Call mongoc_init");
145+
return NULL;
146+
}
147+
139148
#ifndef MONGOC_ENABLE_SSL
140149
if (mongoc_uri_get_tls (uri)) {
141150
_mongoc_set_error (error,

src/libmongoc/src/mongoc/mongoc-client.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,15 @@ mongoc_client_new_from_uri_with_error (const mongoc_uri_t *uri, bson_error_t *er
10311031

10321032
BSON_ASSERT (uri);
10331033

1034+
extern bool mongoc_get_init_called (void);
1035+
if (!mongoc_get_init_called ()) {
1036+
_mongoc_set_error (error,
1037+
MONGOC_ERROR_CLIENT,
1038+
MONGOC_ERROR_CLIENT_NOT_READY,
1039+
"Attempting to create client, but libmongoc not initialized. Call mongoc_init");
1040+
return NULL;
1041+
}
1042+
10341043
#ifndef MONGOC_ENABLE_SSL
10351044
if (mongoc_uri_get_tls (uri)) {
10361045
_mongoc_set_error (error,

src/libmongoc/src/mongoc/mongoc-config.h.in

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,6 @@
203203
# undef MONGOC_HAVE_SASL_CLIENT_DONE
204204
#endif
205205

206-
207-
/*
208-
* Disable automatic calls to mongoc_init() and mongoc_cleanup()
209-
* before main() is called, and after exit() (respectively).
210-
*/
211-
#define MONGOC_NO_AUTOMATIC_GLOBALS @MONGOC_NO_AUTOMATIC_GLOBALS@
212-
213-
#if MONGOC_NO_AUTOMATIC_GLOBALS != 1
214-
# undef MONGOC_NO_AUTOMATIC_GLOBALS
215-
#endif
216-
217206
/*
218207
* MONGOC_HAVE_SOCKLEN is set from configure to determine if we
219208
* need to emulate the type.

src/libmongoc/src/mongoc/mongoc-handshake-private.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ typedef enum {
6161
MONGOC_MD_FLAG_EXPERIMENTAL_FEATURES = 10,
6262
MONGOC_MD_FLAG_HAVE_SASL_CLIENT_DONE = 11,
6363
MONGOC_MD_FLAG_HAVE_WEAK_SYMBOLS = 12,
64-
MONGOC_MD_FLAG_NO_AUTOMATIC_GLOBALS = 13,
65-
MONGOC_MD_FLAG_ENABLE_SSL_LIBRESSL_UNUSED = 14, // Removed in CDRIVER-5693.
64+
MONGOC_MD_FLAG_NO_AUTOMATIC_GLOBALS_UNUSED = 13, // Removed in CDRIVER-1330.
65+
MONGOC_MD_FLAG_ENABLE_SSL_LIBRESSL_UNUSED = 14, // Removed in CDRIVER-5693.
6666
MONGOC_MD_FLAG_ENABLE_SASL_CYRUS = 15,
6767
MONGOC_MD_FLAG_ENABLE_SASL_SSPI = 16,
6868
MONGOC_MD_FLAG_HAVE_SOCKLEN = 17,

src/libmongoc/src/mongoc/mongoc-handshake.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,6 @@ _mongoc_handshake_get_config_hex_string (void)
117117
_set_bit (bf, byte_count, MONGOC_MD_FLAG_HAVE_SASL_CLIENT_DONE);
118118
#endif
119119

120-
#ifdef MONGOC_NO_AUTOMATIC_GLOBALS
121-
_set_bit (bf, byte_count, MONGOC_MD_FLAG_NO_AUTOMATIC_GLOBALS);
122-
#endif
123-
124120
#ifdef MONGOC_EXPERIMENTAL_FEATURES
125121
_set_bit (bf, byte_count, MONGOC_MD_FLAG_EXPERIMENTAL_FEATURES);
126122
#endif

src/libmongoc/src/mongoc/mongoc-init.c

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@
4343
#include <mongoc/mongoc-ocsp-cache-private.h>
4444
#endif
4545

46-
#ifndef MONGOC_NO_AUTOMATIC_GLOBALS
47-
#pragma message("Configure the driver with ENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF.\
48-
Automatic cleanup is deprecated and will be removed in version 2.0.")
49-
#endif
50-
5146
// CDRIVER-2722: Cyrus SASL is deprecated on MacOS.
5247
#if defined(MONGOC_ENABLE_SASL_CYRUS) && defined(__APPLE__)
5348
BEGIN_IGNORE_DEPRECATIONS
@@ -95,9 +90,16 @@ mongoc_cyrus_mutex_free (void *mutex)
9590

9691
#endif /* MONGOC_ENABLE_SASL_CYRUS */
9792

93+
static bool mongoc_init_called;
94+
bool
95+
mongoc_get_init_called (void)
96+
{
97+
return mongoc_init_called;
98+
}
9899

99100
static BSON_ONCE_FUN (_mongoc_do_init)
100101
{
102+
mongoc_init_called = true;
101103
#ifdef MONGOC_ENABLE_SASL_CYRUS
102104
int status;
103105
#endif
@@ -204,29 +206,6 @@ mongoc_cleanup (void)
204206
bson_once (&once, _mongoc_do_cleanup);
205207
}
206208

207-
/*
208-
* On GCC, just use __attribute__((constructor)) to perform initialization
209-
* automatically for the application.
210-
*/
211-
#if defined(__GNUC__) && !defined(MONGOC_NO_AUTOMATIC_GLOBALS)
212-
static void
213-
_mongoc_init_ctor (void) __attribute__ ((constructor));
214-
static void
215-
_mongoc_init_ctor (void)
216-
{
217-
mongoc_init ();
218-
}
219-
220-
static void
221-
_mongoc_init_dtor (void) __attribute__ ((destructor));
222-
static void
223-
_mongoc_init_dtor (void)
224-
{
225-
bson_mem_restore_vtable ();
226-
mongoc_cleanup ();
227-
}
228-
#endif
229-
230209
// CDRIVER-2722: Cyrus SASL is deprecated on MacOS.
231210
#if defined(MONGOC_ENABLE_SASL_CYRUS) && defined(__APPLE__)
232211
BEGIN_IGNORE_DEPRECATIONS

src/libmongoc/tests/test-mongoc-handshake.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,9 +1189,7 @@ test_handshake_platform_config (void)
11891189
BSON_ASSERT (_get_bit (config_str, MONGOC_MD_FLAG_HAVE_SASL_CLIENT_DONE));
11901190
#endif
11911191

1192-
#ifdef MONGOC_NO_AUTOMATIC_GLOBALS
1193-
BSON_ASSERT (_get_bit (config_str, MONGOC_MD_FLAG_NO_AUTOMATIC_GLOBALS));
1194-
#endif
1192+
BSON_ASSERT (!_get_bit (config_str, MONGOC_MD_FLAG_NO_AUTOMATIC_GLOBALS_UNUSED)); // Flag was removed.
11951193

11961194
#ifdef MONGOC_EXPERIMENTAL_FEATURES
11971195
BSON_ASSERT (_get_bit (config_str, MONGOC_MD_FLAG_EXPERIMENTAL_FEATURES));

0 commit comments

Comments
 (0)