Skip to content

Commit 6df7037

Browse files
committed
ref(client): Adapt client for blocking kill in async transport
Add blocking kill functionality to the client in async transport. GH-4601
1 parent 4c1e99b commit 6df7037

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

sentry_sdk/client.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -915,16 +915,28 @@ def get_integration(
915915

916916
return self.integrations.get(integration_name)
917917

918+
def _close_transport(self) -> Optional[asyncio.Task[None]]:
919+
"""Close transport and return cleanup task if any."""
920+
if self.transport is not None:
921+
cleanup_task = self.transport.kill()
922+
self.transport = None
923+
return cleanup_task
924+
return None
925+
918926
def _close_components(self) -> None:
919927
"""Kill all client components in the correct order."""
920928
self.session_flusher.kill()
921929
if self.log_batcher is not None:
922930
self.log_batcher.kill()
923931
if self.monitor:
924932
self.monitor.kill()
925-
if self.transport is not None:
926-
self.transport.kill()
927-
self.transport = None
933+
934+
async def _close_components_async(self) -> None:
935+
"""Async version of _close_components that properly awaits transport cleanup."""
936+
self._close_components()
937+
cleanup_task = self._close_transport()
938+
if cleanup_task is not None:
939+
await cleanup_task
928940

929941
def close( # type: ignore[override]
930942
self,
@@ -941,7 +953,7 @@ async def _flush_and_close(
941953
) -> None:
942954

943955
await self._flush_async(timeout=timeout, callback=callback)
944-
self._close_components()
956+
await self._close_components_async()
945957

946958
if self.transport is not None:
947959
if isinstance(self.transport, AsyncHttpTransport):
@@ -959,6 +971,8 @@ async def _flush_and_close(
959971
else:
960972
self.flush(timeout=timeout, callback=callback)
961973
self._close_components()
974+
self._close_transport()
975+
962976
return None
963977

964978
def flush( # type: ignore[override]

sentry_sdk/transport.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ def kill(self: Self) -> Optional[asyncio.Task[None]]: # type: ignore
790790
self.background_tasks.clear()
791791
try:
792792
# Return the pool cleanup task so caller can await it if needed
793-
return self._loop.create_task(self._pool.aclose()) # type: ignore
793+
return self.loop.create_task(self._pool.aclose()) # type: ignore
794794
except RuntimeError:
795795
logger.warning("Event loop not running, aborting kill.")
796796
return None

0 commit comments

Comments
 (0)