Skip to content

Memory leak when GreenThread objects reference themselves #408

@mattbennett

Description

@mattbennett

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:

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions