Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

1.6 KB
Raw Blame
import asyncio
import threading
from collections.abc import AsyncGenerator, Callable, Generator
from queue import Queue
from typing import Any

import structlog

logger = structlog.get_logger(__name__)


def async_to_sync(async_handler: Callable[[], AsyncGenerator[Any]]) -> Generator[Any]:
    """Converts an async iterator to a sync generator."""

    q: Queue[Any] = Queue(maxsize=5000)
    sentinel = object()
    thread_exception = None

    async def runner():
        try:
            async for event in async_handler():
                q.put(event)
        except Exception as e:
            nonlocal thread_exception
            thread_exception = e
            q.put(sentinel)
        else:
            q.put(sentinel)

    def run_event_loop():
        asyncio.run(runner())

    # Use non-daemon thread with explicit cleanup
    thread = threading.Thread(target=run_event_loop, daemon=False)
    thread.start()

    try:
        # Yield items progressively as they arrive
        while True:
            item = q.get(timeout=60)  # Add timeout to prevent indefinite blocking. Matches max request TTL
            if item is sentinel:
                q.task_done()
                break

            yield item
            q.task_done()
    finally:
        # Ensure thread cleanup
        if thread.is_alive():
            # Signal thread to stop if possible, or wait briefly
            thread.join(timeout=1)
            if thread.is_alive():
                logger.warning("Thread did not terminate cleanly")

        if thread_exception:
            logger.error("Exception in async thread", exc_info=thread_exception)