Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion asab/library/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,26 @@ async def wait_for_library_ready(self, timeout: int = None):
"""
Wait for the library to be ready.

After the ready edge event fires, re-checks `is_ready()`; if another task
moved the library back to not-ready before this coroutine resumed, raises
instead of waiting again.

Args:
timeout (int): The timeout in seconds. If not provided, the default timeout is used.

Raises:
LibraryNotReadyError: If the library is not ready within the timeout.
LibraryNotReadyError: If the library is not ready within the timeout, or if it is
not ready when this coroutine continues after the wait.
"""
if timeout is None:
timeout = self.LibraryReadyTimeout
if self.is_ready():
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fast path is good. It avoids waiting when readiness already holds at entry itself.

return
try:
await asyncio.wait_for(self.LibraryReadyEvent.wait(), timeout=timeout)
except asyncio.TimeoutError:
raise LibraryNotReadyError("Library is not ready yet.")
self._ensure_ready()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still feels like a one-time wait. We wake on the first ready event and then decide immediately from that one wake-up.

If the goal is still “wait until ready or timeout”, I think we probably need to keep waiting when that wake turns out to be stale, instead of treating it as the final truth for readiness.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably do something like this instead, so we keep waiting until self.is_ready() is actually true, while still respecting the original timeout.


async def wait_for_library_ready(self, timeout: int = None):
    if timeout is None:
        timeout = self.LibraryReadyTimeout

    loop = asyncio.get_running_loop()
    deadline = loop.time() + timeout

    while True:
        if self.is_ready():
            return

        remaining = deadline - loop.time()
        if remaining <= 0:
            raise LibraryNotReadyError("Library is not ready yet.")

        try:
            await asyncio.wait_for(self.LibraryReadyEvent.wait(), timeout=remaining)
        except asyncio.TimeoutError:
            raise LibraryNotReadyError("Library is not ready yet.")


async def _set_ready(self, provider):
if len(self.Libraries) == 0:
Expand Down
Loading