[MONGOCRYPT-526] csfle lib_create() failed: Cannot initialize the Mongo Crypt Shared Support Library when it is already initialized Created: 30/Jan/23  Updated: 28/Oct/23  Resolved: 05/May/23

Status: Closed
Project: Libmongocrypt
Component/s: None
Affects Version/s: None
Fix Version/s: 1.7.5

Type: Bug Priority: Unknown
Reporter: Shane Harvey Assignee: Kyle Kloberdanz
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Problem/Incident
causes RUST-1578 CSFLE test bypass_mongocryptd_via_sha... Closed
causes PYTHON-3559 Shared library encryption tests inter... Blocked
Binding Changes: Not Needed

 Description   

Both rust and python are seeing occasional csfle lib_create() failed: Cannot initialize the Mongo Crypt Shared Support Library when it is already initialized. [Error 3, code 1] errors when testing crypt_shared: PYTHON-3559 and RUST-1578. I have not been able to reproduce the error locally yet.

abraham.egnor@mongodb.com was able to remove this error by putting a global mutex around mongocrypt_init and mongocrypt_destroy (here https://github.com/mongodb/libmongocrypt-rust/pull/20/files) which indicates this is a race condition in libmongocrypt itself.

Here's an example:

 [2022/12/20 22:35:48.583] ERROR: test_legacy_badQueries_$pullAll_succeeds_on_unencrypted,_error_on_encrypted (test.test_encryption.TestSpec)
 [2022/12/20 22:35:48.583] ----------------------------------------------------------------------
 [2022/12/20 22:35:48.583] Traceback (most recent call last):
 [2022/12/20 22:35:48.583]   File "C:\data\mci\7d56f8d50d34074aa87d3e48f70ad8bc\src\test\__init__.py", line 603, in wrap
 [2022/12/20 22:35:48.583]     return f(*args, **kwargs)
 [2022/12/20 22:35:48.583]   File "C:\data\mci\7d56f8d50d34074aa87d3e48f70ad8bc\src\test\__init__.py", line 603, in wrap
 [2022/12/20 22:35:48.583]     return f(*args, **kwargs)
 [2022/12/20 22:35:48.583]   File "C:\data\mci\7d56f8d50d34074aa87d3e48f70ad8bc\src\test\test_encryption.py", line 686, in run_scenario
 [2022/12/20 22:35:48.583]     self.run_scenario(scenario_def, test)
 [2022/12/20 22:35:48.583]   File "C:\data\mci\7d56f8d50d34074aa87d3e48f70ad8bc\src\test\utils_spec_runner.py", line 520, in run_scenario
 [2022/12/20 22:35:48.583]     client = rs_client(
 [2022/12/20 22:35:48.583]   File "C:\data\mci\7d56f8d50d34074aa87d3e48f70ad8bc\src\test\utils.py", line 595, in rs_client
 [2022/12/20 22:35:48.583]     return _mongo_client(h, p, **kwargs)
 [2022/12/20 22:35:48.583]   File "C:\data\mci\7d56f8d50d34074aa87d3e48f70ad8bc\src\test\utils.py", line 575, in _mongo_client
 [2022/12/20 22:35:48.583]     return MongoClient(uri, port, **client_options)
 [2022/12/20 22:35:48.583]   File "C:\data\mci\7d56f8d50d34074aa87d3e48f70ad8bc\src\pymongo\mongo_client.py", line 831, in __init__
 [2022/12/20 22:35:48.583]     self._encrypter = _Encrypter(self, self.__options.auto_encryption_opts)
 [2022/12/20 22:35:48.583]   File "C:\data\mci\7d56f8d50d34074aa87d3e48f70ad8bc\src\pymongo\encryption.py", line 337, in __init__
 [2022/12/20 22:35:48.583]     self._auto_encrypter = AutoEncrypter(
 [2022/12/20 22:35:48.583]   File "Z:\data\mci\7d56f8d50d34074aa87d3e48f70ad8bc\src\venv-encryption\lib\site-packages\pymongocrypt\auto_encrypter.py", line 31, in __init__
 [2022/12/20 22:35:48.583]     self.mongocrypt = MongoCrypt(mongo_crypt_opts, callback)
 [2022/12/20 22:35:48.583]   File "Z:\data\mci\7d56f8d50d34074aa87d3e48f70ad8bc\src\venv-encryption\lib\site-packages\pymongocrypt\mongocrypt.py", line 194, in __init__
 [2022/12/20 22:35:48.583]     self.__init()
 [2022/12/20 22:35:48.583]   File "Z:\data\mci\7d56f8d50d34074aa87d3e48f70ad8bc\src\venv-encryption\lib\site-packages\pymongocrypt\mongocrypt.py", line 263, in __init
 [2022/12/20 22:35:48.583]     self.__raise_from_status()
 [2022/12/20 22:35:48.583]   File "Z:\data\mci\7d56f8d50d34074aa87d3e48f70ad8bc\src\venv-encryption\lib\site-packages\pymongocrypt\mongocrypt.py", line 279, in __raise_from_status
 [2022/12/20 22:35:48.583]     raise exc
 [2022/12/20 22:35:48.583] pymongocrypt.errors.MongoCryptError: csfle lib_create() failed: Cannot initialize the Mongo Crypt Shared Support Library when it is already initialized. [Error 3, code 1]
 [2022/12/20 22:35:48.583] ----------------------------------------------------------------------

https://evergreen.mongodb.com/task/mongo_python_driver_tests_windows_encryption__platform~windows_64_vsMulti_small_auth_ssl~noauth_nossl_python_version_windows~3.10_encryption~encryption_crypt_shared_test_6.0_sharded_cluster_f5d09e1c97ca12c94c555b40364b2ae10ec5126c_22_12_20_19_39_04



 Comments   
Comment by Githook User [ 09/May/23 ]

Author:

{'name': 'Kyle Kloberdanz', 'email': 'kyle.kloberdanz@mongodb.com', 'username': 'kkloberdanz'}

Message: Fix Race Condition (#637)

Related: MONGOCRYPT-526

Fix race condition by moving critical section into the mutex protected scope.

See the following repo for a tool to test the race condition fixed in this PR: https://github.com/kkloberdanz/libmongocrypt_stresstest
Branch: NODE-5239-deps
https://github.com/mongodb/libmongocrypt/commit/152589787c5c8351c7db755ebc7606f088b4e1bd

Comment by Githook User [ 05/May/23 ]

Author:

{'name': 'Kyle Kloberdanz', 'email': 'kyle.kloberdanz@mongodb.com', 'username': 'kkloberdanz'}

Message: Fix Race Condition (#637)

Related: MONGOCRYPT-526

Fix race condition by moving critical section into the mutex protected scope.

See the following repo for a tool to test the race condition fixed in this PR: https://github.com/kkloberdanz/libmongocrypt_stresstest
Branch: r1.7
https://github.com/mongodb/libmongocrypt/commit/79f50d5c7c46d2ef04272b407dd2e4e16b37dd4e

Comment by Kyle Kloberdanz [ 05/May/23 ]

I believe the PR above will fix this issue. Shout out to erwin.pe@mongodb.com for help debugging this!

Comment by Githook User [ 05/May/23 ]

Author:

{'name': 'Kyle Kloberdanz', 'email': 'kyle.kloberdanz@mongodb.com', 'username': 'kkloberdanz'}

Message: Fix Race Condition (#637)

Related: MONGOCRYPT-526

Fix race condition by moving critical section into the mutex protected scope.

See the following repo for a tool to test the race condition fixed in this PR: https://github.com/kkloberdanz/libmongocrypt_stresstest
Branch: master
https://github.com/mongodb/libmongocrypt/commit/152589787c5c8351c7db755ebc7606f088b4e1bd

Generated at Thu Feb 08 09:08:53 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.