Skip to content

Commit

Permalink
🐛 refactor IndyVdrLedgerPool close_task
Browse files Browse the repository at this point in the history
Replace Future with Task to fix `RuntimeWarning: coroutine 'IndyVdrLedgerPool.context_close.<locals>.closer' was never awaited`

Signed-off-by: ff137 <[email protected]>
  • Loading branch information
ff137 committed Oct 29, 2024
1 parent 6d6c75a commit 95b03eb
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions acapy_agent/ledger/indy_vdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def __init__(
self.ref_count = 0
self.ref_lock = asyncio.Lock()
self.keepalive = keepalive
self.close_task: asyncio.Future = None
self.close_task: asyncio.Task = None
self.cache = cache
self.cache_duration: int = cache_duration
self.handle: Optional[Pool] = None
Expand Down Expand Up @@ -215,8 +215,9 @@ async def close(self):
async def context_open(self):
"""Open the ledger if necessary and increase the number of active references."""
async with self.ref_lock:
if self.close_task:
if self.close_task and not self.close_task.done():
self.close_task.cancel()
self.close_task = None # Clear the reference after cancellation
if not self.handle:
LOGGER.debug("Opening the pool ledger")
await self.open()
Expand All @@ -227,17 +228,24 @@ async def context_close(self):

async def closer(timeout: int):
"""Close the pool ledger after a timeout."""
await asyncio.sleep(timeout)
async with self.ref_lock:
if not self.ref_count:
LOGGER.debug("Closing pool ledger after timeout")
await self.close()
try:
await asyncio.sleep(timeout)
async with self.ref_lock:
if not self.ref_count:
LOGGER.debug("Closing pool ledger after timeout")
await self.close()
except asyncio.CancelledError:
LOGGER.debug("Closing task cancelled")
except Exception:
LOGGER.exception("Exception when closing pool ledger")

async with self.ref_lock:
self.ref_count -= 1
if not self.ref_count:
if self.keepalive:
self.close_task = asyncio.ensure_future(closer(self.keepalive))
if self.close_task and not self.close_task.done():
self.close_task.cancel()
self.close_task = asyncio.create_task(closer(self.keepalive))
else:
await self.close()

Expand Down

0 comments on commit 95b03eb

Please sign in to comment.