-
Notifications
You must be signed in to change notification settings - Fork 332
Memory leak when GreenThread objects reference themselves #408
Description
GreenThreads and their locals do not appear to be garbage collected if one of the locals contains a reference to the thread and the thread results in an exception.
Example:
import eventlet
import eventlet.debug
eventlet.debug.hub_exceptions(False)
import objgraph
import gc
threads = {}
class Foo(object):
def __init__(self, index):
self.thread = threads[index]
def target(index):
foo = Foo(index)
if index % 2 == 0:
raise Exception("boom")
for index in range(10):
gt = eventlet.spawn(target, index)
threads[index] = gt
eventlet.sleep()
del threads
while gc.collect():
pass
print("Foo count: ", objgraph.count('Foo'))
print("GreenThread count: ", objgraph.count('eventlet.greenthread.GreenThread'))I would expect there to be exactly 1 GreenThread (the main thread) and 0 Foo objects. Instead there are 6 and 5 -- one extra for each thread that threw an exception.
Calling gc.get_referrers from one of the leftover Foo objects led me to this line:
eventlet/eventlet/greenthread.py
Line 220 in 54be759
| self._exit_event.send_exception(*sys.exc_info()) |
Is this a bug, or is it unreasonable for a GreenThread to hold a reference to itself?
There is another example at https://gist.github.com/mattbennett/50509277a33c43658fe3fffa445f4a0a#file-growing-py where the number of objects in memory keeps growing.