Skip to content

Commit c46fb6f

Browse files
BYKclaude
andcommitted
fix: Cancel _target task in AsyncWorker.kill() and improve sync close()
- AsyncWorker.kill() now calls self._task.cancel() before clearing the reference, preventing duplicate consumers if submit() is called later - close() with AsyncHttpTransport now does best-effort sync cleanup (kill transport, close components) instead of silently returning - flush()/close() log warnings instead of debug when async transport used - Add __aenter__/__aexit__ to _Client for 'async with' support Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 4b77519 commit c46fb6f

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

sentry_sdk/client.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,11 +1042,13 @@ def close(
10421042
if isinstance(self.transport, AsyncHttpTransport) and hasattr(
10431043
self.transport, "loop"
10441044
):
1045-
logger.debug(
1046-
"close() used with AsyncHttpTransport, aborting. Please use close_async() instead."
1045+
logger.warning(
1046+
"close() used with AsyncHttpTransport. "
1047+
"Prefer close_async() for graceful async shutdown. "
1048+
"Performing synchronous best-effort cleanup."
10471049
)
1048-
return
1049-
self.flush(timeout=timeout, callback=callback)
1050+
else:
1051+
self.flush(timeout=timeout, callback=callback)
10501052
self._close_components()
10511053
self.transport.kill()
10521054
self.transport = None
@@ -1092,8 +1094,8 @@ def flush(
10921094
if isinstance(self.transport, AsyncHttpTransport) and hasattr(
10931095
self.transport, "loop"
10941096
):
1095-
logger.debug(
1096-
"flush() used with AsyncHttpTransport, aborting. Please use flush_async() instead."
1097+
logger.warning(
1098+
"flush() used with AsyncHttpTransport. Please use flush_async() instead."
10971099
)
10981100
return
10991101
if timeout is None:
@@ -1136,6 +1138,12 @@ def __enter__(self) -> "_Client":
11361138
def __exit__(self, exc_type: "Any", exc_value: "Any", tb: "Any") -> None:
11371139
self.close()
11381140

1141+
async def __aenter__(self) -> "_Client":
1142+
return self
1143+
1144+
async def __aexit__(self, exc_type: "Any", exc_value: "Any", tb: "Any") -> None:
1145+
await self.close_async()
1146+
11391147

11401148
from typing import TYPE_CHECKING
11411149

sentry_sdk/worker.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ def is_alive(self) -> bool:
213213

214214
def kill(self) -> None:
215215
if self._task:
216+
# Cancel the main consumer task to prevent duplicate consumers
217+
self._task.cancel()
216218
if self._queue is not None:
217219
try:
218220
self._queue.put_nowait(_TERMINATOR)

0 commit comments

Comments
 (0)