Skip to content

Commit fd3c321

Browse files
authored
Fix errors with multiprocessing
Before, one could get OSError 22 and BadZipFile errors due to re-used file pointers in forked subprocesses. Fixes #520
1 parent 95ecf2a commit fd3c321

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

importlib_metadata/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,7 @@ class FastPath:
802802
True
803803
"""
804804

805+
# The following cache is cleared at fork, see os.register_at_fork below
805806
@functools.lru_cache() # type: ignore[misc]
806807
def __new__(cls, root):
807808
return super().__new__(cls)
@@ -842,6 +843,10 @@ def mtime(self):
842843
def lookup(self, mtime):
843844
return Lookup(self)
844845

846+
# Clear FastPath.__new__ cache when forked, avoids trying to re-useing open
847+
# file pointers from zipp.Path/zipfile.Path objects in forked process
848+
os.register_at_fork(after_in_child=FastPath.__new__.cache_clear)
849+
845850

846851
class Lookup:
847852
"""

0 commit comments

Comments
 (0)