
    h                         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{     y7 # t        $ r Y w xY w7 r# t        $ r Y {w xY w7 P# t        $ r Y "w xY w7 ;# t        $ r Y 4w xY ww)	z
    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.
    r   N)BaseLLMAIOHTTPHandler
cache_dictcloseclient
_transportaclose)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   s         j/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/llms/custom_httpx/async_client_cleanup.pyclose_litellm_async_clientsr      sP     O<<lBOJ"((*Wg45''7:Smmo%% Wh'^^Fv|,9J9JH1U ++22444
 vx(1A1A --/)) Wh'nn&&&= + &  5   *   ' s   AE,D(&D&'D(+;E,'D9D7D9	E,E,$E
7E8E
<E,EEE#E,&D((	D41E,3D44E,7D99	EE,EE,E

	EE,EE,E	E)%E,(E))E,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                  n   	 t        j                         } | j                         r| j                  t	                      y | j                  t	                      y # t        $ rQ 	 t        j                         } | j                  t	                      | j                          Y y # t        $ r Y Y y w xY ww xY w)N)	asyncioget_event_loop
is_runningcreate_taskr   run_until_completer   new_event_loopr   )loops    r   cleanup_wrapperz6register_async_client_cleanup.<locals>.cleanup_wrapper@   s    	))+D   !<!>? ''(C(EF 	--/''(C(EF

 	s/   =A  A 	B4$=B##	B0,B4/B00B4)atexitregister)r!   r    s     r   register_async_client_cleanupr#   8   s     & OOO$    )__doc__r   r   r#    r$   r   <module>r'      s    .b%r$   