#2178·gevent

Patching SSL before importing truststore raises RecursionError on Windows and MacOS (but works on Linux)

Author: alexprengereCreated May 13, 2026Updated Jun 3, 2026
  • gevent version: latest (26.4)
  • Python version: CPython 3.10 to CPython3.14
  • Operating System: MacOS (Apple M4) + Linux x86_64 (RHEL) + Windows

Description

I am not sure if this is a gevent or a truststore issue yet, sorry.

I have some code that boils down to creating TCP connections and using them to send queries in parallel with gevent. It works great.
I use ssl when connecting, no issues there either.

Now I want to leverage the system certificates using the truststore library. Doing so is basically about replacing gevent.ssl.SSLContext with truststore.SSLContext.

The "initial" problem is that this blocks parallelism. Here is a reproducer:

python
#!/usr/bin/env python3
# /// script
# requires-python = "==3.14.*"
# dependencies = ["gevent", "truststore"]
# ///
import time
import gevent
import gevent.monkey
from gevent import socket, ssl
# gevent.monkey.patch_ssl()  # testing with/without this later

import truststore

HOST = "example.com"
PORT = 443
N = 20


def fetch_no_truststore(n):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
    ssl_context.load_default_certs()
    sock.connect((HOST, PORT))
    wrapped = ssl_context.wrap_socket(sock, server_hostname=HOST)
    wrapped.send(b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")
    response = wrapped.recv(4096)
    print(f"Response {n} received with length {len(response)}")


def fetch_with_truststore(n):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    ssl_context = truststore.SSLContext(ssl.PROTOCOL_TLS_CLIENT)  # only diff
    ssl_context.load_default_certs()
    sock.connect((HOST, PORT))
    wrapped = ssl_context.wrap_socket(sock, server_hostname=HOST)
    wrapped.send(b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")
    response = wrapped.recv(4096)
    print(f"Response {n} received with length {len(response)}")


print("Fetching without truststore...")
start_time = time.time()
gevent.joinall([gevent.spawn(fetch_no_truststore, i) for i in range(N)])
print(f"Total: {time.time() - start_time:.1f}s")

print("Fetching with truststore...")
start_time = time.time()
gevent.joinall([gevent.spawn(fetch_with_truststore, i) for i in range(N)])
print(f"Total: {time.time() - start_time:.1f}s")

Running this with uv run repro.py, you can see that fetch_no_truststore runs in parallel when fetch_with_truststore does not:

Fetching without truststore...
Response 1 received with length 295
Response 15 received with length 295
Response 4 received with length 295
Response 2 received with length 295
Response 5 received with length 295
Response 6 received with length 295
Response 13 received with length 295
Response 17 received with length 295
Response 0 received with length 295
Response 9 received with length 295
Response 16 received with length 295
Response 18 received with length 295
Response 8 received with length 295
Response 11 received with length 295
Response 19 received with length 295
Response 10 received with length 295
Response 14 received with length 295
Response 3 received with length 295
Response 12 received with length 295
Response 7 received with length 295
Total: 0.3s
Fetching with truststore...
Response 0 received with length 295
Response 1 received with length 295
Response 2 received with length 295
Response 3 received with length 295
Response 4 received with length 295
Response 5 received with length 295
Response 6 received with length 295
Response 7 received with length 295
Response 8 received with length 295
Response 9 received with length 295
Response 10 received with length 295
Response 11 received with length 295
Response 12 received with length 295
Response 13 received with length 295
Response 14 received with length 295
Response 15 received with length 295
Response 16 received with length 295
Response 17 received with length 295
Response 18 received with length 295
Response 19 received with length 295
Total: 2.6s

Now this is for MacOS and Windows: if we enable patch_ssl() (before importing truststore, just un-comment line 10), you get an exception:

python
Traceback (most recent call last):
  File "src/gevent/greenlet.py", line 912, in gevent._gevent_cgreenlet.Greenlet.run
  File "/Users/aprengere/Dev/test/repro.py", line 35, in fetch_with_truststore
    wrapped = ssl_context.wrap_socket(sock, server_hostname=HOST)
  File "/Users/aprengere/.cache/uv/environments-v2/repro-d95a9aaa4e86a8af/lib/python3.14/site-packages/truststore/_api.py", line 119, in wrap_socket
    stack.enter_context(_configure_context(self._ctx))
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.14.5/Frameworks/Python.framework/Versions/3.14/lib/python3.14/contextlib.py", line 530, in enter_context
    result = _enter(cm)
  File "/opt/homebrew/Cellar/[email protected]/3.14.5/Frameworks/Python.framework/Versions/3.14/lib/python3.14/contextlib.py", line 141, in __enter__
    return next(self.gen)
  File "/Users/aprengere/.cache/uv/environments-v2/repro-d95a9aaa4e86a8af/lib/python3.14/site-packages/truststore/_macos.py", line 372, in _configure_context
    _set_ssl_context_verify_mode(ctx, ssl.CERT_NONE)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "/Users/aprengere/.cache/uv/environments-v2/repro-d95a9aaa4e86a8af/lib/python3.14/site-packages/truststore/_ssl_constants.py", line 31, in _set_ssl_context_verify_mode
    _original_super_SSLContext.verify_mode.__set__(ssl_context, verify_mode)  # type: ignore[attr-defined]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.14.5/Frameworks/Python.framework/Versions/3.14/lib/python3.14/ssl.py", line 679, in verify_mode
    super(SSLContext, SSLContext).verify_mode.__set__(self, value)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.14.5/Frameworks/Python.framework/Versions/3.14/lib/python3.14/ssl.py", line 679, in verify_mode
    super(SSLContext, SSLContext).verify_mode.__set__(self, value)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.14.5/Frameworks/Python.framework/Versions/3.14/lib/python3.14/ssl.py", line 679, in verify_mode
    super(SSLContext, SSLContext).verify_mode.__set__(self, value)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  [Previous line repeated 989 more times]
RecursionError: maximum recursion depth exceeded

But! Running the same on Linux (RHEL), everything now works, no exception and things are running in parallel:

Fetching without truststore...
Response 0 received with length 837
Response 11 received with length 837
Response 8 received with length 837
Response 15 received with length 837
Response 13 received with length 837
Response 16 received with length 837
Response 18 received with length 837
Response 4 received with length 837
Response 17 received with length 837
Response 19 received with length 837
Response 2 received with length 837
Response 1 received with length 837
Response 3 received with length 837
Response 9 received with length 837
Response 7 received with length 837
Response 6 received with length 837
Response 14 received with length 837
Response 10 received with length 837
Response 5 received with length 837
Response 12 received with length 837
Total: 0.4s
Fetching with truststore...
Response 0 received with length 837
Response 19 received with length 837
Response 18 received with length 837
Response 17 received with length 837
Response 16 received with length 837
Response 13 received with length 837
Response 12 received with length 837
Response 14 received with length 837
Response 15 received with length 837
Response 10 received with length 837
Response 11 received with length 837
Response 9 received with length 837
Response 8 received with length 837
Response 7 received with length 837
Response 5 received with length 837
Response 6 received with length 837
Response 2 received with length 837
Response 4 received with length 837
Response 3 received with length 837
Response 1 received with length 837
Total: 0.5s

I tested the above on every CPython version from 3.10 to 3.14, and the results are the same (truststore requires 3.10+).

So the summary is this: patching ssl before importing truststore works on Linux, but raises RecursionError on MacOS and Windows. Do you think this is something that may be fixed on gevent side? Or should I open an issue for truststore?

Patching SSL after importing truststore results in a TypeError in the SSL module, on all platforms, for all Python versions, which I think is expected, but the error is not very clear: TypeError: super(type, obj): obj (instance of SSLSocket) is not an instance or subtype of type (SSLSocket).