Repository: redis/redis-py
File: redis/auth/token_manager.py (Python)

Code before:
            # Use threading.Event to signal when loop is ready
            loop_ready = threading.Event()

            def start_loop():
                asyncio.set_event_loop(loop)
                loop_ready.set()  # Signal that loop is ready
                loop.run_forever()

            thread = threading.Thread(target=start_loop, daemon=True)
            thread.start()

            # Wait for the loop to be ready before scheduling
            loop_ready.wait()

        # Use thread-safe Event for cross-thread synchronization
        init_done = threading.Event()

        def renew_with_callback():
            try:
                self._renew_token(skip_initial)
            finally:
                init_done.set()

        # Schedule using call_soon_threadsafe for thread-safe scheduling
        loop.call_soon_threadsafe(renew_with_callback)
        logger.info("Token manager started")

        # Blocks using thread-safe Event
        init_done.wait()
        return self.stop

    async def start_async(
        self,
        listener: CredentialsListener,
        block_for_initial: bool = False,
        initial_delay_in_ms: float = 0,
        skip_initial: bool = False,
    ) -> Callable[[], None]:
        self._listener = listener

        loop = asyncio.get_running_loop()
        init_event = asyncio.Event()

        # Wraps the async callback with async wrapper to schedule with loop.call_later()
        wrapped = _async_to_sync_wrapper(
            loop, self._renew_token_async, skip_initial, init_event
        )
        self._init_timer = loop.call_later(initial_delay_in_ms / 1000, wrapped)
        logger.info("Token manager started")

        if block_for_initial:

Review comment (bug):
The inline function `renew_with_callback` lacks documentation explaining its purpose. Consider adding a docstring or comment to clarify that this wrapper ensures the `init_done` event is set regardless of whether `_renew_token` succeeds or fails, enabling proper synchronization with the calling thread.

Code after:
            # Use threading.Event to signal when loop is ready
            loop_ready = threading.Event()

            def start_loop():
                # This runs in the background thread. First, bind the event loop to
                # this thread, then signal that the loop is ready so the calling
                # thread can safely schedule work (via call_soon_threadsafe) before
                # we block in run_forever().
                asyncio.set_event_loop(loop)
                loop_ready.set()  # Signal that loop is ready for cross-thread use
                loop.run_forever()

            thread = threading.Thread(target=start_loop, daemon=True)
            thread.start()

            # Wait for the loop to be ready before scheduling
            loop_ready.wait()

        # Use thread-safe Event for cross-thread synchronization
        init_done = threading.Event()

        def renew_with_callback():
            try:
                self._renew_token(skip_initial)
            finally:
                init_done.set()

        # Schedule using call_soon_threadsafe for thread-safe scheduling
        self._init_timer = loop.call_soon_threadsafe(renew_with_callback)
        logger.info("Token manager started")

        # Blocks using thread-safe Event
        init_done.wait()
        return self.stop

    async def start_async(
        self,
        listener: CredentialsListener,
        block_for_initial: bool = False,
        initial_delay_in_ms: float = 0,
        skip_initial: bool = False,
    ) -> Callable[[], None]:
        self._listener = listener

        loop = asyncio.get_running_loop()
        init_event = asyncio.Event()

        # Wraps the async callback with async wrapper to schedule with loop.call_later()
        wrapped = _async_to_sync_wrapper(
            loop, self._renew_token_async, skip_initial, init_event
        )