Skip to content

Commit

Permalink
Fix potential meta path race condition (#38)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
velovix authored Jul 14, 2021
1 parent 1dfc4ca commit 5506c2c
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions vcap/vcap/loading/capsule_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ def load_capsule_from_bytes(data: bytes,
capsule_module = ModuleType(module_name)

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

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

# noinspection PyUnresolvedReferences
new_capsule: BaseCapsule = capsule_module.Capsule(
Expand Down

0 comments on commit 5506c2c

Please sign in to comment.