haystack icon indicating copy to clipboard operation
haystack copied to clipboard

PipelineStreamHandle.aclose swallows the caller’s request timeout

Open resonance-null opened this issue 3 weeks ago • 0 comments

Describe the bug

When a request deadline expires while its task is awaiting PipelineStreamHandle.aclose(), the task can keep executing past that await and return normally. The reproduction uses one event loop and an async component with an awaited cleanup step.

aclose() suppresses BaseException around asyncio.wait_for(). That scope also suppresses cancellation of the task calling aclose().

Expected behavior

An expired asyncio.timeout() should raise TimeoutError in the caller. Cancellation of the pipeline task during cleanup can be handled separately from cancellation of the caller.

To reproduce

The component signals when cleanup starts. The caller's deadline is then set to expire while the caller is inside aclose().

import asyncio
import os

os.environ["HAYSTACK_TELEMETRY_ENABLED"] = "false"

from haystack import Pipeline, component


@component
class AsyncComponent:
    def __init__(self):
        self.started = asyncio.Event()
        self.cleanup_started = asyncio.Event()
        self.release_cleanup = asyncio.Event()

    @component.output_types(value=str)
    def run(self) -> dict:
        return {"value": "done"}

    @component.output_types(value=str)
    async def run_async(self) -> dict:
        self.started.set()
        try:
            await asyncio.Future()
        finally:
            self.cleanup_started.set()
            await self.release_cleanup.wait()


async def main():
    pipe = Pipeline()
    instance = AsyncComponent()
    pipe.add_component("worker", instance)
    handle = pipe.stream({})
    await asyncio.wait_for(instance.started.wait(), timeout=2)

    loop = asyncio.get_running_loop()
    deadline = loop.create_future()

    async def request():
        async with asyncio.timeout(None) as timeout:
            deadline.set_result(timeout)
            await handle.aclose()
            print("Continued after the request deadline:", timeout.expired())

    caller = asyncio.create_task(request())
    await asyncio.wait_for(instance.cleanup_started.wait(), timeout=2)
    deadline.result().reschedule(loop.time() - 1)
    await asyncio.sleep(0)
    instance.release_cleanup.set()

    try:
        await asyncio.wait_for(caller, timeout=2)
    except TimeoutError:
        print("Request raised TimeoutError")
    else:
        print("Request returned normally")


asyncio.run(main())

Observed output:

Continued after the request deadline: True
Request returned normally

System

Linux x86-64, Python 3.13.7. Reproduced on Haystack 3.5.0 and on main at bbe4efe50c716d6d05a0637980db3b641236e613. The reproduction was prepared and verified locally with AI assistance. I checked the FAQ and searched the existing issues.

resonance-null avatar Nov 11 '26 17:11 resonance-null