Skip to content

Commit 2caa1c4

Browse files
yinghsienwucopybara-github
authored andcommitted
chore: internal
PiperOrigin-RevId: 827676000
1 parent b469597 commit 2caa1c4

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

google/genai/_api_client.py

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ def __init__(
630630
self.api_key = None
631631

632632
if not self.location and not self.api_key:
633-
self.location = 'global'
633+
self.location = 'global'
634634

635635
self.custom_base_url = (
636636
validated_http_options.base_url
@@ -728,10 +728,57 @@ def __init__(
728728

729729
async def _get_aiohttp_session(self) -> 'aiohttp.ClientSession':
730730
"""Returns the aiohttp client session."""
731-
if self._aiohttp_session is None or self._aiohttp_session.closed:
731+
new_session_per_request = False
732+
if (
733+
self._http_options
734+
and self._http_options.async_client_args
735+
and 'new_session_per_request' in self._http_options.async_client_args
736+
):
737+
# Experimental, may result in memory leaks.
738+
new_session_per_request = True
739+
740+
if (
741+
self._aiohttp_session is None
742+
or self._aiohttp_session.closed
743+
or new_session_per_request
744+
):
745+
if new_session_per_request and self._aiohttp_session is not None:
746+
# If new_session_per_request is True, close the session if exists.
747+
asyncio.get_running_loop().create_task(self._aiohttp_session.close())
748+
732749
# Initialize the aiohttp client session if it's not set up or closed.
733-
self._aiohttp_session = aiohttp.ClientSession(
734-
connector=aiohttp.TCPConnector(limit=0),
750+
751+
class AiohttpClientSession(aiohttp.ClientSession):
752+
753+
def __del__(self, _warn=warnings) -> None:
754+
if not self.closed:
755+
context = {
756+
'client_session': self,
757+
'message': 'Unclosed client session',
758+
}
759+
if self._source_traceback is not None:
760+
context['source_traceback'] = self._source_traceback
761+
# Remove this self._loop.call_exception_handler(context)
762+
763+
class AiohttpTCPConnector(aiohttp.TCPConnector):
764+
765+
def __del__(self, _warn=warnings) -> None:
766+
if self._closed:
767+
return
768+
if not self._conns:
769+
return
770+
conns = [repr(c) for c in self._conns.values()]
771+
self._close()
772+
context = {
773+
'connector': self,
774+
'connections': conns,
775+
'message': 'Unclosed connector',
776+
}
777+
if self._source_traceback is not None:
778+
context['source_traceback'] = self._source_traceback
779+
# Remove this self._loop.call_exception_handler(context)
780+
self._aiohttp_session = AiohttpClientSession(
781+
connector=AiohttpTCPConnector(limit=0),
735782
trust_env=True,
736783
read_bufsize=READ_BUFFER_SIZE,
737784
)

0 commit comments

Comments
 (0)