Skip to content

Commit

Permalink
Fix concurrency issue
Browse files Browse the repository at this point in the history
When multiple threads are running, they may both
cleanup the cache directory at the same time and
one thread will provoke FileNotFound errors in the
other thread.
  • Loading branch information
mbarkhau committed Apr 21, 2024
1 parent b18a1dc commit 278aee4
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/markdown_katex/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,17 @@ def tex2html(tex: str, options: MaybeOptions = None) -> str:
def _cleanup_cache_dir() -> None:
min_mtime = time.time() - 24 * 60 * 60
for fpath in CACHE_DIR.iterdir():
if fpath.is_file():
try:
if not fpath.is_file():
continue

mtime = fpath.stat().st_mtime
if mtime < min_mtime:
fpath.unlink()
if mtime > min_mtime:
continue

fpath.unlink()
except FileNotFoundError:
pass # concurrent thread deleted file before we did


# NOTE: in order to not have to update the code
Expand Down

0 comments on commit 278aee4

Please sign in to comment.