Skip to content

Backport PR #28541 on branch v3.9.x (MNT: be more careful about disk I/O failures when writing font cache) #28552

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
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
8 changes: 4 additions & 4 deletions lib/matplotlib/font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,11 +965,11 @@ def json_dump(data, filename):
This function temporarily locks the output file to prevent multiple
processes from overwriting one another's output.
"""
with cbook._lock_path(filename), open(filename, 'w') as fh:
try:
try:
with cbook._lock_path(filename), open(filename, 'w') as fh:
json.dump(data, fh, cls=_JSONEncoder, indent=2)
except OSError as e:
_log.warning('Could not save font_manager cache %s', e)
except OSError as e:
_log.warning('Could not save font_manager cache %s', e)


def json_load(filename):
Expand Down
24 changes: 23 additions & 1 deletion lib/matplotlib/tests/test_font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
json_dump, json_load, get_font, is_opentype_cff_font,
MSUserFontDirectories, _get_fontconfig_fonts, ttfFontProperty)
from matplotlib import cbook, ft2font, pyplot as plt, rc_context, figure as mfigure
from matplotlib.testing import subprocess_run_helper
from matplotlib.testing import subprocess_run_helper, subprocess_run_for_testing


has_fclist = shutil.which('fc-list') is not None
Expand Down Expand Up @@ -280,6 +280,28 @@ def test_fontcache_thread_safe():
subprocess_run_helper(_test_threading, timeout=10)


def test_lockfilefailure(tmp_path):
# The logic here:
# 1. get a temp directory from pytest
# 2. import matplotlib which makes sure it exists
# 3. get the cache dir (where we check it is writable)
# 4. make it not writable
# 5. try to write into it via font manager
proc = subprocess_run_for_testing(
[
sys.executable,
"-c",
"import matplotlib;"
"import os;"
"p = matplotlib.get_cachedir();"
"os.chmod(p, 0o555);"
"import matplotlib.font_manager;"
],
env={**os.environ, 'MPLCONFIGDIR': str(tmp_path)},
check=True
)


def test_fontentry_dataclass():
fontent = FontEntry(name='font-name')

Expand Down
Loading