Skip to content

Commit 8e86b91

Browse files
Patch preloaded SSLContext in Requests
Co-authored-by: Seth Michael Larson <[email protected]>
1 parent 911ceab commit 8e86b91

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ jobs:
8484
compileall:
8585
# Run 'python -m compileall' on an old Python version
8686
# to ensure that pip can vendor truststore successfully.
87-
runs-on: ubuntu-latest
87+
runs-on: ubuntu-22.04 # pin to 22.04, as with 24.04 Python 3.7 is no longer available
8888
name: compileall
8989
steps:
9090
- uses: actions/checkout@v4

src/truststore/_api.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sys
66
import typing
77

8-
import _ssl # type: ignore[import-not-found]
8+
import _ssl
99

1010
from ._ssl_constants import (
1111
_original_SSLContext,
@@ -43,6 +43,23 @@ def inject_into_ssl() -> None:
4343
except ImportError:
4444
pass
4545

46+
# requests starting with 2.32.0 added a preloaded SSL context to improve concurrent performance;
47+
# this unfortunately leads to a RecursionError, which can be avoided by patching the preloaded SSL context with
48+
# the truststore patched instance
49+
# also see https://github.com/psf/requests/pull/6667
50+
try:
51+
import requests.adapters
52+
53+
preloaded_context = getattr(requests.adapters, "_preloaded_ssl_context", None)
54+
if preloaded_context is not None:
55+
setattr(
56+
requests.adapters,
57+
"_preloaded_ssl_context",
58+
SSLContext(ssl.PROTOCOL_TLS_CLIENT),
59+
)
60+
except ImportError:
61+
pass
62+
4663

4764
def extract_from_ssl() -> None:
4865
"""Restores the :class:`ssl.SSLContext` class to its original state"""

0 commit comments

Comments
 (0)