X Tutup
Skip to content

FT2Font do not close open file, leads to delayed ResourceWarning #7922

@Carreau

Description

@Carreau

In some rare case, FT2Font may forget to close some open font files.
(Mostly during IPython test suite, it was bothering me for some month, so I dug deeper).
More generally simply importing matplotlib.figure can be enough to trigger it.

minimal test case:

$ cat no-close-fd.py
import warnings
warnings.simplefilter('default')
from matplotlib.ft2font import FT2Font
FT2Font('/System/Library/Fonts/Apple Color Emoji.ttf')
 ~ $ python no-close-fd.py
python no-close-fd.py
no-close-fd.py:4: ResourceWarning: unclosed file <_io.BufferedReader name='/System/Library/Fonts/Apple Color Emoji.ttf'>
  FT2Font('/System/Library/Fonts/Apple Color Emoji.ttf')
Traceback (most recent call last):
  File "no-close-fd.py", line 4, in <module>
    FT2Font('/System/Library/Fonts/Apple Color Emoji.ttf')
RuntimeError: In FT2Font: Could not set the fontsize

The RuntimeError is catched in most of the places where Matplotlib uses it, so the "ResourceWarning" actually triggers much later, here at interpreter shutdown. I left the runtime error as the error message help us find where this come from, but the interesting part (for me) is the ResourceWarning.

Note that filtering the ResourceWarning to error will not work as the resource is leaking so the interpreter has no clue where it comes from.

Matplotlib version at least current stable conda-forge build osx python 3.5, I have issue building from source.

Possible Cause:

It seem it comes from here, in the FT2Font Constructor in C++

   ...
    error = FT_Set_Char_Size(face, 12 * 64, 0, 72 * (unsigned int)hinting_factor, 72);
    if (error) {
        // Oops I throw during creation, I should likely `FT_Done_Face(face)` ??
        throw "Could not set the fontsize";
    }
   ...

But I have not touched C/C++ in a loooong time... so I let you judge.

This other file trigger the leak: /Library/Fonts/NISC18030.ttf

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup