
    i                         d Z ddlZd Zd Zy)zQ
Utility functions for cleaning up async HTTP clients to prevent resource leaks.
    Nc                    K   ddl } ddlm} t        | j                  di       }|j                         D ]  \  }}t        ||      r&t        |d      r	 |j                          d{    8t        |d      r|j                  }t        |d      r9t        |j                  d      r#	 |j                  j                          d{    t        |d      s|j                  r	 |j                          d{    t        |d      s	 |j                          d{     t        | d	      rAt        | d	d      }t        ||      r't        |d      r	 |j                          d{    yyyy7 # t        $ r Y Nw xY w7 # t        $ r Y w xY w7 # t        $ r Y qw xY w7 # t        $ r Y w xY w7 P# t        $ r Y yw xY ww)
a)  
    Close all cached async HTTP clients to prevent resource leaks.

    This function iterates through all cached clients in litellm's in-memory cache
    and closes any aiohttp client sessions that are still open. Also closes the
    global base_llm_aiohttp_handler instance (issue #12443).
    r   N)BaseLLMAIOHTTPHandler
cache_dictcloseclient
_transportaclosebase_llm_aiohttp_handler)litellm)litellm.llms.custom_httpx.aiohttp_handlerr   getattrin_memory_llm_clients_cacheitems
isinstancehasattrr   	Exceptionr   r   r	   	is_closed)r   r   r   keyhandlerr   base_handlers          /Users/manta/Documents/Projects/TheRoad-I1/.venv/lib/python3.12/site-packages/litellm/llms/custom_httpx/async_client_cleanup.pyclose_litellm_async_clientsr      s     O<<lBOJ"((*Wg45''7:Smmo%% Wh'^^Fv|,9J9JH1U ++22444
 vx(1A1A --/)) Wh'nn&&&= +J w23w(BDIl$9:w'@
"((***	@
: 4C &  5   *   '  + s  AGE6&E3'E6+;G'FFF	GG$F7F8F<GF+F)F+#3GF= *F;+F= /G3E66	F?GFGF	FGFGF	F&"G%F&&G)F++	F84G7F88G;F= =	G	GG		Gc                  4    ddl } d }| j                  |       y)z
    Register the async client cleanup function to run at exit.

    This ensures that all async HTTP clients are properly closed when the program exits.
    r   Nc                      	 t        j                         } t        j                  |        	 | j                  t	                      | j                          y# | j                          w xY w# t        $ r Y yw xY w)z
        Cleanup wrapper that creates a fresh event loop for atexit cleanup.

        At exit time, the main event loop is often already closed. Creating a new
        event loop ensures cleanup runs successfully (fixes issue #12443).
        N)asyncionew_event_loopset_event_looprun_until_completer   r   r   )loops    r   cleanup_wrapperz6register_async_client_cleanup.<locals>.cleanup_wrapperN   sb    	 ))+D""4(''(C(EF 



 		s(   )A+ A A+ A((A+ +	A76A7)atexitregister)r!   r    s     r   register_async_client_cleanupr#   F   s     * OOO$    )__doc__r   r   r#    r$   r   <module>r'      s    <~%r$   