#2177·gevent

Possible deadlock between gevent 25.8.1+ async close and multiprocessing.resource_tracker

Author: gurobokumCreated May 6, 2026Updated Jul 13, 2026
  • gevent gevent==25.9.1 from PyPI
  • Python version 3.13.3
  • Operating System: Ubuntu 22.04.5 LTS

Description:

I'm using gevent with gunicorn but the issue seems to be more general than gunicorn itself. Starting with gevent 25.8.1 os.close() appears to be patched/handled asynchronously . I use multiprocessing.shared_memory.SharedMemory, which causes python to start a multiprocessing.resource_tracker child process. During shutdown the gevent worker closes file descriptors (async) and then waits resource_tracker process to exit. However resource_tracker exits only after receiving EOF on its pipe from the parent process and it waits for that EOF synchronously.

As a result:

  1. the worker waits in waitpid() for resource_tracker;
  2. resource_tracker waits in read() for EOF from the worker;
  3. the worker hangs until Gunicorn kills it by timeout.

This looks like a deadlock between gevent’s patched close/waitpid behavior and Python’s synchronous resource_tracker shutdown protocol.

Parent (Gevent)

munmap(0xffff5d830000, 1048576)         = 0
munmap(0xffff5d730000, 1048576)         = 0
munmap(0xffff5d330000, 1048576)         = 0
munmap(0xffff5d230000, 1048576)         = 0
fcntl(38, F_GETFD)                      = 0x1 (flags FD_CLOEXEC)
fstat(38, {st_mode=S_IFIFO|0600, st_size=0, ...}) = 0
wait4(31909,
# ls -al /proc/31865/fd/38
l-wx------ 1 root root 64 May  6 16:28 /proc/31865/fd/38 -> 'pipe:[4403576]'
# lsof | grep 4403576
...
gunicorn  31865 31888 rdk:broke  root   38w     FIFO               0,12        0t0 4403576 pipe
gunicorn  31865 32167 gunicorn   root   38w     FIFO               0,12        0t0 4403576 pipe
python    31909                  root   37r     FIFO               0,12        0t0 4403576 pipe

Child (resource_tracker)

strace: Process 31909 attached
read(37, "REGISTER:/CB_features:shared_mem"..., 4096) = 36
read(37, "UNREGISTER:/CB_features:shared_m"..., 4096) = 38
read(37,

How to reproduce

from multiprocessing import shared_memory

sm = shared_memory.SharedMemory(create=True, size=1024)

def app(environ, start_response):
    data = b"Hello, World!"
    start_response("200 OK", [
        ("Content-Type", "text/plain"),
        ("Content-Length", str(len(data)))
    ])
    return [data]
workers = 1
worker_class = "gevent"

loglevel = "debug"

bind = "0.0.0.0:8080"
graceful_timeout = 30
daemon = False

Start

bash
gunicorn main:app
bash
# ps -efH
vagrant     3130    3092  4 16:46 pts/3    00:00:00       /opt/app/.venv/bin/python /opt/app/.venv/bin/gunicorn main:app
vagrant     3131    3130  1 16:46 pts/3    00:00:00         /opt/app/.venv/bin/python /opt/app/.venv/bin/gunicorn main:app
vagrant     3132    3131  1 16:46 pts/3    00:00:00           /opt/app/.venv/bin/python -c from multiprocessing.resource_tracker import main;main(13)

Stop

bash
kill 3131

Cannot close fd

bash
[2026-05-06 16:48:45 +0000] [3134] [INFO] Worker exiting (pid: 3131)
[2026-05-06 16:49:14 +0000] [3130] [CRITICAL] WORKER TIMEOUT (pid:3131)
[2026-05-06 16:49:16 +0000] [3130] [ERROR] Worker (pid:3134) was sent SIGKILL! Perhaps out of memory?