Skip to content

Commit d7f7429

Browse files
2xBjaraco
authored andcommitted
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 40bb485 commit d7f7429

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
@@ -803,6 +803,7 @@ class FastPath:
803803
True
804804
"""
805805

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

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

847852
class Lookup:
848853
"""

0 commit comments

Comments
 (0)