Skip to content

Commit 8070b8d

Browse files
committed
PYTHON-5014 Fix handling of async socket errors in kms request
1 parent d696857 commit 8070b8d

File tree

3 files changed

+35
-29
lines changed

3 files changed

+35
-29
lines changed

.evergreen/run-tests.sh

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -136,35 +136,35 @@ if [ -n "$TEST_ENCRYPTION" ] || [ -n "$TEST_FLE_AZURE_AUTO" ] || [ -n "$TEST_FLE
136136
fi
137137

138138
python -m pip install '.[encryption]'
139-
pip install pymongocrypt==1.11
140-
# # Use the nocrypto build to avoid dependency issues with older windows/python versions.
141-
# BASE=$(pwd)/libmongocrypt/nocrypto
142-
# if [ -f "${BASE}/lib/libmongocrypt.so" ]; then
143-
# PYMONGOCRYPT_LIB=${BASE}/lib/libmongocrypt.so
144-
# elif [ -f "${BASE}/lib/libmongocrypt.dylib" ]; then
145-
# PYMONGOCRYPT_LIB=${BASE}/lib/libmongocrypt.dylib
146-
# elif [ -f "${BASE}/bin/mongocrypt.dll" ]; then
147-
# PYMONGOCRYPT_LIB=${BASE}/bin/mongocrypt.dll
148-
# # libmongocrypt's windows dll is not marked executable.
149-
# chmod +x $PYMONGOCRYPT_LIB
150-
# PYMONGOCRYPT_LIB=$(cygpath -m $PYMONGOCRYPT_LIB)
151-
# elif [ -f "${BASE}/lib64/libmongocrypt.so" ]; then
152-
# PYMONGOCRYPT_LIB=${BASE}/lib64/libmongocrypt.so
153-
# else
154-
# echo "Cannot find libmongocrypt shared object file"
155-
# exit 1
156-
# fi
157-
# export PYMONGOCRYPT_LIB
158-
159-
# # TODO: Test with 'pip install pymongocrypt'
160-
# if [ ! -d "libmongocrypt_git" ]; then
161-
# git clone https://github.com/mongodb/libmongocrypt.git libmongocrypt_git
162-
# fi
163-
# python -m pip install -U setuptools
164-
# python -m pip install ./libmongocrypt_git/bindings/python
165-
# python -c "import pymongocrypt; print('pymongocrypt version: '+pymongocrypt.__version__)"
166-
# python -c "import pymongocrypt; print('libmongocrypt version: '+pymongocrypt.libmongocrypt_version())"
167-
# # PATH is updated by PREPARE_SHELL for access to mongocryptd.
139+
140+
# Use the nocrypto build to avoid dependency issues with older windows/python versions.
141+
BASE=$(pwd)/libmongocrypt/nocrypto
142+
if [ -f "${BASE}/lib/libmongocrypt.so" ]; then
143+
PYMONGOCRYPT_LIB=${BASE}/lib/libmongocrypt.so
144+
elif [ -f "${BASE}/lib/libmongocrypt.dylib" ]; then
145+
PYMONGOCRYPT_LIB=${BASE}/lib/libmongocrypt.dylib
146+
elif [ -f "${BASE}/bin/mongocrypt.dll" ]; then
147+
PYMONGOCRYPT_LIB=${BASE}/bin/mongocrypt.dll
148+
# libmongocrypt's windows dll is not marked executable.
149+
chmod +x $PYMONGOCRYPT_LIB
150+
PYMONGOCRYPT_LIB=$(cygpath -m $PYMONGOCRYPT_LIB)
151+
elif [ -f "${BASE}/lib64/libmongocrypt.so" ]; then
152+
PYMONGOCRYPT_LIB=${BASE}/lib64/libmongocrypt.so
153+
else
154+
echo "Cannot find libmongocrypt shared object file"
155+
exit 1
156+
fi
157+
export PYMONGOCRYPT_LIB
158+
159+
# TODO: Test with 'pip install pymongocrypt'
160+
if [ ! -d "libmongocrypt_git" ]; then
161+
git clone https://github.com/mongodb/libmongocrypt.git libmongocrypt_git
162+
fi
163+
python -m pip install -U setuptools
164+
python -m pip install ./libmongocrypt_git/bindings/python
165+
python -c "import pymongocrypt; print('pymongocrypt version: '+pymongocrypt.__version__)"
166+
python -c "import pymongocrypt; print('libmongocrypt version: '+pymongocrypt.libmongocrypt_version())"
167+
# PATH is updated by PREPARE_SHELL for access to mongocryptd.
168168
fi
169169

170170
if [ -n "$TEST_ENCRYPTION" ]; then

pymongo/asynchronous/encryption.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ async def kms_request(self, kms_context: MongoCryptKmsContext) -> None:
213213
if not data:
214214
raise OSError("KMS connection closed")
215215
kms_context.feed(data)
216+
# Async raises an OSError instead of returning empty bytes
217+
except OSError as err:
218+
raise OSError("KMS connection closed") from err
216219
except MongoCryptError:
217220
raise # Propagate MongoCryptError errors directly.
218221
except Exception as exc:

pymongo/synchronous/encryption.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ def kms_request(self, kms_context: MongoCryptKmsContext) -> None:
213213
if not data:
214214
raise OSError("KMS connection closed")
215215
kms_context.feed(data)
216+
# Async raises an OSError instead of returning empty bytes
217+
except OSError as err:
218+
raise OSError("KMS connection closed") from err
216219
except MongoCryptError:
217220
raise # Propagate MongoCryptError errors directly.
218221
except Exception as exc:

0 commit comments

Comments
 (0)