3434import sys
3535import threading
3636import time
37- from typing import Any , AsyncIterator , Iterator , Optional , TYPE_CHECKING , Tuple , Union , cast , overload
37+ from typing import Any , AsyncIterator , Callable , Coroutine , Iterator , Optional , TYPE_CHECKING , Tuple , Union , cast , overload
3838from urllib .parse import urlparse
3939from urllib .parse import urlunparse
4040import warnings
@@ -615,6 +615,38 @@ def __del__(self) -> None:
615615 pass
616616
617617
618+ # Strong references to close tasks scheduled from `__del__`, so they are not
619+ # garbage collected (and destroyed while pending) before they finish.
620+ _background_close_tasks : set ['asyncio.Task[Any]' ] = set ()
621+
622+
623+ def _spawn_close_task (
624+ loop : asyncio .AbstractEventLoop , close : Callable [[], Coroutine [Any , Any , Any ]]
625+ ) -> None :
626+ """Runs `close()` as a task on `loop`. Must be called from `loop`'s thread."""
627+ if loop .is_closed ():
628+ return
629+ task : 'asyncio.Task[Any]' = loop .create_task (close ())
630+ _background_close_tasks .add (task )
631+ task .add_done_callback (_background_close_tasks .discard )
632+
633+
634+ def _schedule_close_on_loop (
635+ loop : asyncio .AbstractEventLoop , close : Callable [[], Coroutine [Any , Any , Any ]]
636+ ) -> None :
637+ """Schedules `close()` on `loop`, which may belong to another thread."""
638+ try :
639+ current_loop : Optional [asyncio .AbstractEventLoop ] = (
640+ asyncio .get_running_loop ()
641+ )
642+ except RuntimeError :
643+ current_loop = None
644+ if loop is current_loop :
645+ _spawn_close_task (loop , close )
646+ elif loop .is_running ():
647+ loop .call_soon_threadsafe (_spawn_close_task , loop , close )
648+
649+
618650class AsyncHttpxClient (httpx .AsyncClient ):
619651 """Async httpx client."""
620652
@@ -630,7 +662,7 @@ def __del__(self) -> None:
630662 except Exception :
631663 pass
632664 try :
633- asyncio .get_running_loop (). create_task ( self .aclose () )
665+ _spawn_close_task ( asyncio .get_running_loop (), self .aclose )
634666 except Exception :
635667 pass
636668
@@ -2315,7 +2347,7 @@ async def _async_upload_fd(
23152347 break
23162348 delay_seconds = INITIAL_RETRY_DELAY * (DELAY_MULTIPLIER ** retry_count )
23172349 retry_count += 1
2318- time .sleep (delay_seconds )
2350+ await asyncio .sleep (delay_seconds )
23192351
23202352 offset += chunk_size
23212353 if (
@@ -2535,8 +2567,25 @@ def __del__(self) -> None:
25352567 except Exception : # pylint: disable=broad-except
25362568 pass
25372569
2570+ # Each aiohttp session must be closed on the event loop that created it,
2571+ # which is not necessarily the loop (if any) running in the thread that
2572+ # garbage collects this client.
2573+ try :
2574+ if (
2575+ not self ._http_options .httpx_async_client
2576+ and self ._async_httpx_client
2577+ ):
2578+ _spawn_close_task (
2579+ asyncio .get_running_loop (), self ._async_httpx_client .aclose
2580+ )
2581+ except Exception : # pylint: disable=broad-except
2582+ pass
2583+
25382584 try :
2539- asyncio .get_running_loop ().create_task (self .aclose ())
2585+ if self ._aiohttp_sessions and not self ._http_options .aiohttp_client :
2586+ for loop , session in list (self ._aiohttp_sessions .items ()):
2587+ if not self ._is_session_closed (session ):
2588+ _schedule_close_on_loop (loop , session .close )
25402589 except Exception : # pylint: disable=broad-except
25412590 pass
25422591
0 commit comments