8000 FIX: make the cache in font_manager._get_font keyed by thread id by tacaswell · Pull Request #19618 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

FIX: make the cache in font_manager._get_font keyed by thread id #19618

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
Mar 6, 2021
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.
Loading
Diff view
Diff view
< 8000 div class="js-expand-full-wrapper d-inline-block">
13 changes: 11 additions & 2 deletions lib/matplotlib/font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
import subprocess
import sys
try:
import threading
from threading import Timer
except ImportError:
import dummy_threading as threading
from dummy_threading import Timer

import matplotlib as mpl
Expand Down Expand Up @@ -1394,7 +1396,12 @@ def is_opentype_cff_font(filename):
return False


_get_font = lru_cache(64)(ft2font.FT2Font)
@lru_cache(64)
def _get_font(filename, hinting_factor, *, _kerning_factor, thread_id):
return ft2font.FT2Font(
filename, hinting_factor, _kerning_factor=_kerning_factor)


# 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
Expand All @@ -1409,8 +1416,10 @@ def get_font(filename, hinting_factor=None):
filename = _cached_realpath(filename)
if hinting_factor is None:
hinting_factor = rcParams['text.hinting_factor']
# also key on the thread ID to prevent segfaults with multi-threading
return _get_font(filename, hinting_factor,
_kerning_factor=rcParams['text.kerning_factor'])
_kerning_factor=rcParams['text.kerning_factor'],
thread_id=threading.get_ident())


def _load_fontmanager(*, try_read_cache=True):
Expand Down
40 changes: 40 additions & 0 deletions lib/matplotlib/tests/test_font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
from pathlib import Path
import shutil
import subprocess
import sys
import warnings

Expand Down Expand Up @@ -215,3 +216,42 @@ def test_missing_family(caplog):
"findfont: Generic family 'sans' not found because none of the "
"following families were found: this-font-does-not-exist",
]


def _test_threading():
import threading
from matplotlib.ft2font import LOAD_NO_HINTING
import matplotlib.font_manager as fm

N = 10
b = threading.Barrier(N)

def bad_idea(n):
b.wait()
for j in range(100):
font = fm.get_font(fm.findfont("DejaVu Sans"))
font.set_text(str(n), 0.0, flags=LOAD_NO_HINTING)

threads = [
threading.Thread(target=bad_idea, name=f"bad_thread_{j}", args=(j,))
for j in range(N)
]

for t in threads:
t.start()

for t in threads:
t.join()


def test_fontcache_thread_safe():
pytest.importorskip('threading')
import inspect

proc = subprocess.run(
[sys.executable, "-c",
inspect.getsource(_test_threading) + '\n_test_threading()']
)
if proc.returncode:
pytest.fail("The subprocess returned with non-zero exit status "
f"{proc.returncode}.")
0