Skip to content

Memory leak in greendns #810

@jiajunsu

Description

@jiajunsu

Hi, we've found a memory leak in module greendns, when the dns server response query with exception dns.exception.Timeout or dns.exception.DNSException, we got a memory leak with EAI_EAGAIN_ERROR or EAI_NODATA_ERROR.

The root cause of this memory leak is from TracebackType:

  1. In greendns.py, we initialize EAI_*_ERROR as instances of class socket.gaierror, which has __traceback__ attribute as PEP-3134 defined.
  2. The __traceback__ is a TracebackType, when the same instance EAI_NODATA_ERROR being called multiple times, it inserts tb_next to the top of EAI_NODATA_ERROR.__traceback__.tb_next.

To simplify and reproduce the problem, we made a script, the main logic is same with function greendns.resolve.

import socket
EAI_NONAME_ERROR = socket.gaierror(socket.EAI_NONAME, 'Name or service not known')

def query():
    raise EAI_NONAME_ERROR


def step():
    try:
        query()
    except Exception:
        pass


print("Init")
print(EAI_NONAME_ERROR.__traceback__)  # None

step()
print("After first raise")
print(EAI_NONAME_ERROR.__traceback__)  # <traceback object at 0x7f04dd6a3d90>
print(EAI_NONAME_ERROR.__traceback__.tb_next)  # <traceback object at 0x7f04dd6a1770>
print(EAI_NONAME_ERROR.__traceback__.tb_next.tb_next)  # None

step()
print("After second raise")
print(EAI_NONAME_ERROR.__traceback__)  # <traceback object at 0x7f04dd6a17c0>
print(EAI_NONAME_ERROR.__traceback__.tb_next)  # <traceback object at 0x7f04ddf48aa0>
print(EAI_NONAME_ERROR.__traceback__.tb_next.tb_next)  # <traceback object at 0x7f04dd6a3d90>
print(EAI_NONAME_ERROR.__traceback__.tb_next.tb_next.tb_next)  # <traceback object at 0x7f04dd6a1770>
print(EAI_NONAME_ERROR.__traceback__.tb_next.tb_next.tb_next.tb_next)  # None

I tested and reproduced the script with python version 3.7.10, 3.8.8, 3.11.0, and will try to commit a pull request to fix this issue.

IMHO this issue is not related to OS version, my environment info is blow:

uname -a

Linux 3.10.0-862.14.4.el7.x86_64

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions