Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gevent gets stuck when gevent.threadpool is used inside a fork hook #2023

Open
sumerc opened this issue Feb 20, 2024 · 1 comment
Open

gevent gets stuck when gevent.threadpool is used inside a fork hook #2023

sumerc opened this issue Feb 20, 2024 · 1 comment

Comments

@sumerc
Copy link

sumerc commented Feb 20, 2024

Description

subprocess.Popen gets stuck inside the fork hook if gevent.threadpool is used. The weird thing is if the print(gevent.threadpool) line is removed, issue does not happen.

Reproducer

import gevent.monkey
gevent.monkey.patch_all(
        subprocess=True,
    )

import os, gevent, subprocess, time

def hook():
    print("inside fork hook")
    gevent.get_hub().threadpool.apply(lambda: print("run with threadpool!"))
    

os.register_at_fork(after_in_child=hook)
print(gevent.get_hub().threadpool, " >>> HUB.threadpool before hook") # --> if you remove this line, this works!!
r = subprocess.Popen("pwd", close_fds=False)

time.sleep(1.0)
print("ended!", r)
  • Issue only happens when the print(gevent.get_hub().threadpool, " >>> HUB.threadpool before hook") is there. Somehow, reading threadpool before the fork hook causes the problem.
  • Issue happens both on Linux and MacOsx.
  • Issue only happens with Python 3.11 and above. 3.10 works just fine.
@sumerc sumerc changed the title gevent stuck when gevent.threadpool is used inside a fork hook gevent subprocess.POpen gets stuck when gevent.threadpool is used inside a fork hook Feb 20, 2024
@sumerc sumerc changed the title gevent subprocess.POpen gets stuck when gevent.threadpool is used inside a fork hook gevent gets stuck when gevent.threadpool is used inside a fork hook Feb 20, 2024
@ddorian
Copy link

ddorian commented Mar 25, 2024

I can see the same thing with tqdm too. For me it happens on python 3.11.8, gevent 24.2.1, tqdm 4.66.2. Disabling thread monkey patching fixes it.

import gevent.monkey

gevent.monkey.patch_all(thread=True)


def tttqqqdddmmm():
    import tqdm

    def do_it():
        for i in tqdm.tqdm(range(5)):
            print(i)

    global_threadpool = gevent.get_hub().threadpool
    global_threadpool.spawn(do_it).get()
    exit("tqdm_exit")


tttqqqdddmmm()

Output:

tqdm_exit
0
1
2
3
4
# here it's stuck

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants