Skip to content

Commit f4ac157

Browse files
committed
ref(client): Moved close done callback into async task
Moved the logic of the done callback directly into the async task to provide a cleaner and simpler control flow. GH-4601
1 parent 9dd546c commit f4ac157

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

sentry_sdk/client.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -935,21 +935,24 @@ def close( # type: ignore[override]
935935
Close the client and shut down the transport. Arguments have the same
936936
semantics as :py:meth:`Client.flush`. When using the async transport, close needs to be awaited to block.
937937
"""
938+
939+
async def _flush_and_close(
940+
timeout: Optional[float], callback: Optional[Callable[[int, float], None]]
941+
) -> None:
942+
await self._flush_async(timeout=timeout, callback=callback)
943+
self._close_components()
944+
938945
if self.transport is not None:
939946
if isinstance(self.transport, AsyncHttpTransport):
940947

941-
def _on_flush_done(_: asyncio.Task[None]) -> None:
942-
self._close_components()
943-
944948
try:
945949
flush_task = self.transport.loop.create_task(
946-
self._flush_async(timeout, callback)
950+
_flush_and_close(timeout, callback)
947951
)
948952
except RuntimeError:
949953
logger.warning("Event loop not running, aborting close.")
950954
return None
951955
# Enforce flush before shutdown
952-
flush_task.add_done_callback(_on_flush_done)
953956
return flush_task
954957
else:
955958
self.flush(timeout=timeout, callback=callback)

0 commit comments

Comments
 (0)