#2196·gevent

spawning a subprocess poisons the process's TLS connections

Author: ddorianCreated Jul 29, 2026Updated Jul 29, 2026

Description:

An at-fork child handler that yields (OpenTelemetry, Sentry and filelock all register one) runs inside fork() and hands control to the child's copy of the hub, which resumes copies of the parent's greenlets. gevent.subprocess forks and execs in Python, so meanwhile that child holds a copy of every open TLS session: a copy writes a record, the parent reuses its sequence number, and the server answers bad_record_mac. Delete the register_at_fork line for the control.

python
❯ uv run --script gevent_fork_ssl_poisoning_repro.py
gevent 26.7.0 | CPython 3.14.6
Traceback (most recent call last):
  File "src/gevent/greenlet.py", line 912, in gevent._gevent_cgreenlet.Greenlet.run
    result = self._run(*self.args, **self.kwargs)
  File "/home/guru/Desktop/gevent/gevent_fork_ssl_poisoning_repro.py", line 71, in talk
    sock.recv(4)
    ~~~~~~~~~^^^
  File "/home/guru/.cache/uv/environments-v2/gevent-fork-ssl-poisoning-repro-fef66dedca7c4d17/lib/python3.14/site-packages/gevent/ssl.py", line 615, in recv
    return self.read(buflen)
           ~~~~~~~~~^^^^^^^^
  File "/home/guru/.cache/uv/environments-v2/gevent-fork-ssl-poisoning-repro-fef66dedca7c4d17/lib/python3.14/site-packages/gevent/ssl.py", line 439, in read
    return self._sslobj.read(nbytes or 1024)
           ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
ssl.SSLError: [SSL: SSLV3_ALERT_BAD_RECORD_MAC] sslv3 alert bad record mac (_ssl.c:2713)
2026-07-29T18:16:51Z <Greenlet at 0x74e02e604720: talk> failed with SSLError

POISONED: SSLError(1, '[SSL: SSLV3_ALERT_BAD_RECORD_MAC] sslv3 alert bad record mac (_ssl.c:2713)')
python
# /// script
# requires-python = ">=3.10"
# dependencies = ["gevent==26.7.0", "greenlet==3.5.4", "trustme==1.2.1"]
# ///
from gevent import monkey; monkey.patch_all()   # first, before anything it patches

import sys

if sys.argv[1:2] == ['--server']:       # this file re-executed as the TLS peer, in a
    import socket                       # separate process, so no fork of ours reaches it
    import ssl
    ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
    ctx.load_cert_chain(sys.argv[2])
    srv = socket.socket()
    srv.bind(('127.0.0.1', 0))
    srv.listen(1)
    print(srv.getsockname()[1], flush=True)
    conn = ctx.wrap_socket(srv.accept()[0], server_side=True)
    while True:
        try:
            data = conn.recv(4096)
        except OSError:                 # the MAC failure; the alert is already sent
            break
        if not data:
            break
        conn.sendall(data)
    sys.exit()


import os
import socket
import ssl
import subprocess

import gevent
import trustme

print('gevent %s | CPython %s'
      % (gevent.__version__, '.'.join(str(v) for v in sys.version_info[:3])))

ca = trustme.CA()
cert = ca.issue_cert('localhost')
with cert.private_key_and_cert_chain_pem.tempfile() as pem:
    server = subprocess.Popen([sys.executable, os.path.abspath(__file__), '--server', pem],
                              stdout=subprocess.PIPE)
    port = int(server.stdout.readline())

# The least a fork handler can do and still yield. Delete for the control run.
os.register_at_fork(after_in_child=lambda: gevent.sleep(0))

ctx = ssl.create_default_context()
ca.configure_trust(ctx)
sock = ctx.wrap_socket(socket.create_connection(('127.0.0.1', port)),
                       server_hostname='localhost')

def talk():
    while True:
        sock.sendall(b'ping')
        sock.recv(4)
        gevent.sleep(0.002)

talker = gevent.spawn(talk)
gevent.sleep(0.1)

for _ in range(20):
    subprocess.run([sys.executable, '-c', 'pass'], check=False)
    gevent.sleep(0.01)
    if talker.dead:
        break

server.kill()
if talker.dead:
    print('POISONED: %r' % (talker.exception,))
    sys.exit(1)
talker.kill()
print('TLS connection intact')