8000 Invalidate FT2Font cache when fork()ing. by anntzer · Pull Request #14535 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Invalidate FT2Font cache when fork()ing. #14535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
10000
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/matplotlib/font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1264,12 +1264,20 @@ def is_opentype_cff_font(filename):
return False


_get_font = lru_cache(64)(ft2font.FT2Font)
_fmcache = os.path.join(
mpl.get_cachedir(), 'fontlist-v{}.json'.format(FontManager.__version__))
fontManager = None


_get_font = lru_cache(64)(ft2font.FT2Font)
# FT2Font objects cannot be used across fork()s because they reference the same
# FT_Library object. While invalidating *all* existing FT2Fonts after a fork
# would be too complicated to be worth it, the main way FT2Fonts get reused is
# via the cache of _get_font, which we can empty upon forking (in Py3.7+).
if hasattr(os, "register_at_fork"):
os.register_at_fork(after_in_child=_get_font.cache_clear)


def get_font(filename, hinting_factor=None):
if hinting_factor is None:
hinting_factor = rcParams['text.hinting_factor']
Expand Down
9EB0 15 changes: 15 additions & 0 deletions lib/matplotlib/tests/test_font_manager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from io import BytesIO
import multiprocessing
import os
from pathlib import Path
import shutil
Expand Down Expand Up @@ -184,3 +185,17 @@ def test_user_fonts_win32():
# Now, the font should be available
fonts = findSystemFonts()
assert any(font_test_file in font for font in fonts)


def _model_handler(_):
fig, ax = plt.subplots()
fig.savefig(BytesIO(), format="pdf")
plt.close()


@pytest.mark.skipif(not hasattr(os, "register_at_fork"),
reason="Cannot register at_fork handlers")
def test_fork():
_model_handler(0) # Make sure the font cache is filled.
ctx = multiprocessing.get_context("fork")
ctx.Pool(processes=2).map(_model_handler, range(2))
0