Skip to content

Commit 5506c2c

Browse files
authored
Fix potential meta path race condition (#38)
Removing the ZipFinder by index has the potential to remove a meta path finder that was added while the capsule module is initializing. Using .remove ensures we remove the exact ZipFinder we inserted.
1 parent 1dfc4ca commit 5506c2c

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

vcap/vcap/loading/capsule_loading.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ def load_capsule_from_bytes(data: bytes,
114114
capsule_module = ModuleType(module_name)
115115

116116
# Allow the capsule.py to import other files in the capsule
117-
sys.meta_path.insert(
118-
1, ZipFinder(capsule_file, source_path, module_name))
117+
zip_finder = ZipFinder(capsule_file, source_path, module_name)
118+
sys.meta_path.insert(1, zip_finder)
119119

120120
try:
121121
# Run the capsule
@@ -127,7 +127,7 @@ def load_capsule_from_bytes(data: bytes,
127127
f"Error: {e}")
128128
finally:
129129
# Remove custom import code
130-
sys.meta_path.pop(1)
130+
sys.meta_path.remove(zip_finder)
131131

132132
# noinspection PyUnresolvedReferences
133133
new_capsule: BaseCapsule = capsule_module.Capsule(

0 commit comments

Comments
 (0)