-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Monkey-patching ssl after ssl has already been imported #2796
Comments
create a file called
now add the following argument to your gunicorn. So if you started |
delete --preload |
Maybe I'm missing something? This solution doesn't work for me - I still get the same message about how "Monkey-patching ssl after ssl has already been imported may lead to errors" even when I do the monkey patch in the gunicorn config file. And when I look into gunicorn's code that processes config, it imports ssl, which makes me wonder how this could ever work. python 3.10.6 |
@scmccarthy try to import request later in your app rather than in the main module. This should be enough. |
I don't understand, @benoitc . I'm not importing request in the main module. From what I could tell, gunicorn will import ssl before my own code can do anything. And then if I ever call gevent.monkey.patch_all(), we get that "Monkey-patching ssl after ssl has already been imported may lead to errors" message. I had to just give up on monkey patching. |
Fix: [i] Enter the target: https://*** [*] Processing site input... [*] Validating if https://*** is a WordPress site... [-] Error during validation: maximum recursion depth exceeded 2024-09-02 13:55:10,721 - ERROR - Validation error: maximum recursion depth exceeded on https sites, monkey tries to load the ssl when it is already loaded by requests ref: benoitc/gunicorn#2796
Updating to a gevent worker class for gunicorn caused the app to fail on its api requests. It threw a maximum recursion depth exceeded, and this was a warning produced at the top of the logs as well. Resolved by using the solution referenced here: benoitc/gunicorn#2796 (comment)
Updating to a gevent worker class for gunicorn caused the app to fail on its api requests. It threw a maximum recursion depth exceeded, and this was a warning produced at the top of the logs as well. Resolved by using the solution referenced here: benoitc/gunicorn#2796 (comment)
Recently we encountered a problem, if we use
requests
in aflask
project, it will finally trigger an warning:/Users/buxizhizhoum/.virtualenvs/gunicorn_monkey_patch/lib/python3.6/site-packages/gunicorn/workers/ggevent.py:38: MonkeyPatchWarning: Monkey-patching ssl after ssl has already been imported may lead to errors, including RecursionError on Python 3.6. It may also silently lead to incorrect behaviour on Python 3.7. Please monkey-patch earlier. See https://github.com/gevent/gevent/issues/1016. Modules that had direct imports (NOT patched): ['urllib3.util (/Users/bytedance/.virtualenvs/gunicorn_monkey_patch/lib/python3.6/site-packages/urllib3/util/__init__.py)', 'urllib3.util.ssl_ (/Users/bytedance/.virtualenvs/gunicorn_monkey_patch/lib/python3.6/site-packages/urllib3/util/ssl_.py)'].
This could be reproduced with the code below:
the app.py file
the command to start it:
gunicorn -w 8 --worker-class gevent --preload -b 0.0.0.0:5000 app:app
A simplified start procedure of gunicorn is:
1.parse config
2.load app
3.init worker
gunicorn imported the
ssl
module(config.py
in gevent) when loading config,however the monkey patch is called when init the worker which is definitely after import ofssl
, this is why we get the warning that mentioned above.We tired 3 ways to solve this problem
app.py
GeventWorker.patch
ingevent.ggevent.py
, changemonkey.patch_all()
tomonkey.patch_all(ssl=False)
the method 1 has no use, since import
ssl
is before load appthe method 2 could work, if we regardless of the drawback that we patched 2 times, method 2 is almost the best work around that I could find.
the method 3 could also work, the drawback is
ssl
is blocking now.And besides all of above, I just wonder could we provide a choice that allow users to set
ssl
patch to False, and it could solve the problem, considering the fact that patch ssl after import may cause severe problems, leavessl
not patched might be an solution for someone that could accept the performance lose?And if wen change
app.py
file to lines below, it could triggerRecursionError: maximum recursion depth exceeded while calling a Python object
app.py that could trigger RecursionError
When we start it with gunicorn and call http://127.0.0.1/test it will raise
RecursionError
, which is mentioned at Gunicorn RecursionError with gevent and requests in python 3.6.2The environment
The text was updated successfully, but these errors were encountered: