Skip to content

Commit

Permalink
Fix: Watching for updates could be started multiple times
Browse files Browse the repository at this point in the history
This would have been the source of many duplicate asyncio tasks.
  • Loading branch information
hoh committed Aug 16, 2022
1 parent f568134 commit a46af2e
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions vm_supervisor/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class VmExecution:
concurrent_runs: int
runs_done_event: asyncio.Event
expire_task: Optional[asyncio.Task] = None
update_task: Optional[asyncio.Task] = None

@property
def is_running(self):
Expand Down Expand Up @@ -147,6 +148,13 @@ def cancel_expiration(self) -> bool:
else:
return False

def cancel_update(self) -> bool:
if self.update_task:
self.update_task.cancel()
return True
else:
return False

async def stop(self):
if self.times.stopped_at is not None:
logger.debug(f"VM={self.vm.vm_id} already stopped")
Expand All @@ -157,10 +165,12 @@ async def stop(self):
await self.vm.teardown()
self.times.stopped_at = datetime.now()
self.cancel_expiration()
self.cancel_update()

def start_watching_for_updates(self, pubsub: PubSub):
pool = asyncio.get_running_loop()
pool.create_task(self.watch_for_updates(pubsub=pubsub))
if not self.update_task:
loop = asyncio.get_running_loop()
self.update_task = loop.create_task(self.watch_for_updates(pubsub=pubsub))

async def watch_for_updates(self, pubsub: PubSub):
await pubsub.msubscribe(
Expand Down

0 comments on commit a46af2e

Please sign in to comment.