File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44from collections .abc import AsyncIterable
55from contextlib import asynccontextmanager
66from dataclasses import dataclass
7- from datetime import timedelta
7+ from datetime import datetime , timedelta
88from typing import (
99 Any ,
1010 AsyncGenerator ,
8383STREAM_CLOSED_BIT : STREAM_CLOSED_BIT_TYPE = 0b01000
8484
8585
86+ SESSION_CLOSE_TIMEOUT_SEC = 2
87+
8688_BackpressuredWaiter : TypeAlias = Callable [[], Awaitable [None ]]
8789
8890
@@ -403,7 +405,15 @@ async def close(
403405 ) -> None :
404406 """Close the session and all associated streams."""
405407 if (current_state or self ._state ) in TerminalStates :
408+ start = datetime .now ()
406409 while (current_state or self ._state ) != SessionState .CLOSED :
410+ elapsed = (datetime .now () - start ).total_seconds ()
411+ if elapsed >= SESSION_CLOSE_TIMEOUT_SEC :
412+ logger .warning (
413+ f"Session took longer than { SESSION_CLOSE_TIMEOUT_SEC } "
414+ "seconds to close, leaking" ,
415+ )
416+ break
407417 logger .debug ("Session already closing, waiting..." )
408418 await asyncio .sleep (0.2 )
409419 # already closing
You can’t perform that action at this time.
0 commit comments