Skip to content

Commit

Permalink
Fix: A VM could be prepared in parallel
Browse files Browse the repository at this point in the history
Problem: The method `prepare()` of a VM could be launched multiple times in parallel.

Solution: Use a lock and skip the preparation if it has already been done.
  • Loading branch information
hoh committed Nov 21, 2023
1 parent 01b6eb3 commit 68b6bf6
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/aleph/vm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def __init__(
self.ready_event = asyncio.Event()
self.concurrent_runs = 0
self.runs_done_event = asyncio.Event()
self.preparation_pending_lock = asyncio.Lock()
self.stop_pending_lock = asyncio.Lock()
self.snapshot_manager = snapshot_manager

Expand All @@ -126,17 +127,22 @@ def to_json(self, indent: Optional[int] = None) -> str:

async def prepare(self):
"""Download VM required files"""
self.times.preparing_at = datetime.now(tz=timezone.utc)
if self.is_program:
resources = AlephProgramResources(self.message, namespace=self.vm_hash)
elif self.is_instance:
resources = AlephInstanceResources(self.message, namespace=self.vm_hash)
else:
msg = "Unknown executable message type"
raise ValueError(msg)
await resources.download_all()
self.times.prepared_at = datetime.now(tz=timezone.utc)
self.resources = resources
async with self.preparation_pending_lock:
if self.resources:
# Already prepared
return

self.times.preparing_at = datetime.now(tz=timezone.utc)
if self.is_program:
resources = AlephProgramResources(self.message, namespace=self.vm_hash)
elif self.is_instance:
resources = AlephInstanceResources(self.message, namespace=self.vm_hash)
else:
msg = "Unknown executable message type"
raise ValueError(msg)
await resources.download_all()
self.times.prepared_at = datetime.now(tz=timezone.utc)
self.resources = resources

async def create(self, vm_id: int, tap_interface: Optional[TapInterface] = None) -> AlephFirecrackerExecutable:
if not self.resources:
Expand Down

0 comments on commit 68b6bf6

Please sign in to comment.