Skip to content

ResourceWarning: unclosed file in create_thumbnail #244

Open
@cbporch

Description

@cbporch

The File orig and ContentFile thumb_file variables created in Avatar.create_thumbnail do not appear to be closed before the method returns: https://github.com/jazzband/django-avatar/blob/main/avatar/models.py#L142

Django: 4.1.13
django-avatar: 8.0.0
Python 3.10

While upgrading Django, I noted the following error while running pytest with -Wa flags enabled & PYTHONWARNINGS=always in my env:

/usr/local/lib/python3.10/dist-packages/avatar/models.py:201: ResourceWarning: unclosed file <_io.BufferedReader name='/tmp/tmphkurztys/img/avatar/1/29711aa648bad2dca40e2463dc8761cb.gif'>
  instance.create_thumbnail(size, size)
Enable tracemalloc to get traceback where the object was allocated.
See https://docs.pytest.org/en/stable/how-to/capture-warnings.html#resource-warnings for more info.

Hopping into a debugger, I can see that by the bottom of the function both have a closed property of False:
Screenshot 2024-01-04 at 1 57 05 PM
Screenshot 2024-01-04 at 1 57 10 PM

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions