haystack icon indicating copy to clipboard operation
haystack copied to clipboard

Async lifecycle fallbacks block the event loop for sync-only components

Open Harsh23Kashyap opened this issue 2 months ago • 4 comments

Description

QueryExpander.warm_up_async() and close_async() call synchronous lifecycle methods directly when the wrapped chat generator has no async implementation. A slow synchronous warm_up() or close() therefore blocks the serving event loop.

The async execution path already sends a sync-only run() through a worker thread. The lifecycle fallbacks should provide the same guarantee.

Reproduction

import asyncio
import time

from haystack.components.query.query_expander import QueryExpander


class SyncLifecycle:
    def run(self, **kwargs):
        return {"replies": []}

    def warm_up(self):
        time.sleep(0.15)


async def main():
    ticks = []

    async def ticker():
        await asyncio.sleep(0.02)
        ticks.append(round(time.perf_counter() - start, 3))

    expander = QueryExpander(chat_generator=SyncLifecycle())
    global start
    start = time.perf_counter()
    await asyncio.gather(expander.warm_up_async(), ticker())
    print({"ticker_at_seconds": ticks[0], "event_loop_blocked": ticks[0] >= 0.14})


for _ in range(2):
    asyncio.run(main())

Output from current main (3c0dc45b), identical across both runs:

{'ticker_at_seconds': 0.171, 'event_loop_blocked': True}
{'ticker_at_seconds': 0.171, 'event_loop_blocked': True}

Expected behavior

When an async lifecycle method falls back to a sync-only implementation, the synchronous call should run outside the event-loop thread, for example with asyncio.to_thread().

Additional context

The same direct fallback pattern appears in other wrappers, including MultiRetriever, LLMEvaluator, and synchronous close() fallbacks. A shared lifecycle helper could keep this behavior consistent.

Harsh23Kashyap avatar Sep 22 '26 19:09 Harsh23Kashyap

I can take this. I will keep the first patch focused on QueryExpander: dispatch the sync-only warm_up() and close() fallbacks off the event-loop thread, with deterministic regression tests for both lifecycle methods. I will avoid introducing a cross-component helper unless maintainers prefer the broader refactor.

liang0417 avatar Sep 23 '26 07:09 liang0417

Implemented the focused QueryExpander fix in #12868. Sync-only lifecycle fallbacks now run via asyncio.to_thread(), with deterministic regression tests for both warm_up_async() and close_async(). Local validation completed: 51 passed, 3 skipped; Ruff checks passed; mypy passed; release-note lint passed.

liang0417 avatar Sep 23 '26 08:09 liang0417

The issue is valid in principle. However, the current fallback is intentional: some components may initialize async clients in their synchronous warm_up() and it's common practice to create them in the serving loop. Moving these calls to a worker thread could break these components.

Where possible, we prefer implementing warm_up_async() in the component.

For these reasons, I'll close this as not planned. We may revisit this decision in the future.

anakin87 avatar Sep 28 '26 13:09 anakin87

Thanks for the clarification. I understand that the synchronous fallback is intentional because some components may create async clients in the serving event loop, and moving that initialization to a worker thread could break event-loop affinity.

The preferred direction is therefore for individual components to implement their asynchronous lifecycle methods where appropriate. I’ll leave this closed and keep this lifecycle constraint in mind for future contributions. Thanks for taking the time to review it.

liang0417 avatar Sep 29 '26 02:09 liang0417