#2202·gevent

A forked gevent child runs copies of the parent's greenlets.

Author: ddorianCreated Aug 18, 2026Updated Aug 18, 2026

Description:

The child does nothing but os._exit(). The at-fork handler below yields, and that alone is enough for the child's hub to resume the parent's greenlets, inside fork(), before it has returned.

What I've run:

python-traceback
❯ uv run repro_fork_child_queued_switches.py
10 of 10 children ran the parent greenlets: [117633, 117634, 117635, 117636, 117637, 117638, 117639, 117640, 117641, 117642]
python
# /// script
# requires-python = ">=3.10"
# dependencies = ["gevent"]
# ///
from gevent import monkey; monkey.patch_all()

import os
import tempfile

import gevent

os.register_at_fork(after_in_child=lambda: gevent.sleep(0))

fileno, path = tempfile.mkstemp()
os.close(fileno)
fileno = os.open(path, os.O_WRONLY | os.O_APPEND)
parent = os.getpid()
stop = []

def worker():
    while not stop:
        os.write(fileno, b'%d\n' % os.getpid())
        gevent.sleep(0)

workers = [gevent.spawn(worker) for _ in range(4)]
gevent.sleep(0.05)
for _ in range(10):
    if os.fork() == 0:
        os._exit(0)
    os.wait()
    gevent.sleep(0.01)
stop.append(1)
gevent.joinall(workers)
os.close(fileno)

with open(path) as f:
    strangers = sorted({int(line) for line in f} - {parent})
os.unlink(path)

print('%d of 10 children ran the parent greenlets: %s' % (len(strangers), strangers))