Skip to content

MNT: be more careful about disk I/O failures when writing font cache #28541

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
Jul 12, 2024
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 @@ -287,6 +287,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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# 5. try to write into it via font manager
# 5. try to write into it via font manager
# 6. Check that we do not raise even though 5. cannot work

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: Check that the log warning is emitted.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to pass on adding a check for the logging. I don't think we do that systematically currently and because the log emission happens in the sub-process it would take a fair amount of work to check (I think something like the tests for blocking where we print something and them search for the string in stdout).

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