Skip to content

Commit 8dd2937

Browse files
committed
Decouple clear_after_fork from lru_cache and then compose.
1 parent 1da3f45 commit 8dd2937

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

importlib_metadata/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -787,18 +787,17 @@ def find_distributions(self, context=Context()) -> Iterable[Distribution]:
787787
"""
788788

789789

790-
def _clear_lru_cache_after_fork(func):
791-
"""Wrap ``func`` with ``functools.lru_cache`` and clear it after ``fork``.
790+
def _clear_after_fork(cached):
791+
"""Ensure ``func`` clears cached state after ``fork`` when supported.
792792
793-
``FastPath`` caches zip-backed ``pathlib.Path`` objects that keep a
793+
``FastPath`` caches zip-backed ``pathlib.Path`` objects that retain a
794794
reference to the parent's open ``ZipFile`` handle. Re-using a cached
795795
instance in a forked child can therefore resurrect invalid file pointers
796796
and trigger ``BadZipFile``/``OSError`` failures (python/importlib_metadata#520).
797-
Registering ``cache_clear`` with ``os.register_at_fork`` ensures every
798-
process gets a pristine cache and opens its own archive handles.
797+
Registering ``cache_clear`` with ``os.register_at_fork`` keeps each process
798+
on its own cache.
799799
"""
800800

801-
cached = functools.lru_cache()(func)
802801
register = getattr(os, 'register_at_fork', None)
803802
if register is not None:
804803
register(after_in_child=cached.cache_clear)
@@ -821,7 +820,8 @@ class FastPath:
821820
True
822821
"""
823822

824-
@_clear_lru_cache_after_fork
823+
@_clear_after_fork
824+
@functools.lru_cache()
825825
def __new__(cls, root):
826826
return super().__new__(cls)
827827

0 commit comments

Comments
 (0)