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
Cleanup gevent resources on thread destruction #1601
Comments
Thanks for the report!
Deliberately running gevent hubs in multiple native threads, while something that is expected to work, isn't something that is heavily tested. Most usage of gevent that I'm aware of, and that the gevent test cases execute, uses a single native thread to run a hub (CPU processing may be moved to background threads). So this is a little bit "here there be dragons." And you've definitely found a dragon. Your sample code is basically correct as far as cleaning up all resources go (I would just add This image is the object graph this code was originally producing. It doesn't tell the whole story (parts of the story are in C), but it does start to point in the right direction. I think I have a way to make this better, if the backwards compatibility issues work out. Preliminary results are looking good with the following script (here, I use import gc
import gevent
import threading
import objgraph
import io
# Collect original object counts and discard the output
objgraph.show_growth(file=io.StringIO() if str is not bytes else io.BytesIO())
def thread_main():
g = gevent.Greenlet(run=lambda: 0)
g.start()
g.join()
hub = gevent.get_hub()
hub.join()
hub.destroy(destroy_loop=True)
del hub
def tester():
t = threading.Thread(target=thread_main)
t.start()
t.join()
while gc.collect():
pass
# Show anything that has still leaked
objgraph.show_growth(limit=15)
for _ in range(50):
tester() With the changes I'm working on, there's no output. Previously there would be growth on every iteration:
Like the doctor said when the patient said "it hurts when I do this": "Well then don't do that." |
Addressing #1601. This solution works, but breaks anything that tries to use the hub again after joining it once, because the hub greenlet dies. So that can't be right.
Closed in #1604 |
woutdenolf commentedMay 4, 2020
Two questions:
Here is a minimal working example. Destroying the hub does not seem to help:
The text was updated successfully, but these errors were encountered: