Skip to content

Commit e23efd7

Browse files
committed
ref(transport): Make client work with sync flush changes
GH-4601
1 parent 4b0d09b commit e23efd7

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

sentry_sdk/client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,11 @@ async def _flush_async(
10231023
self.session_flusher.flush()
10241024
if self.log_batcher is not None:
10251025
self.log_batcher.flush()
1026-
await self.transport.flush_async(timeout=timeout, callback=callback) # type: ignore
1026+
1027+
# For async transport, flush() returns a Task that needs to be awaited
1028+
flush_task = self.transport.flush(timeout=timeout, callback=callback) # type: ignore
1029+
if flush_task is not None:
1030+
await flush_task
10271031

10281032
def __enter__(self) -> _Client:
10291033
return self

sentry_sdk/transport.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ class AsyncHttpTransport(HttpTransportCore):
581581
def __init__(self: Self, options: Dict[str, Any]) -> None:
582582
super().__init__(options)
583583
# Requires event loop at init time
584-
self._loop = asyncio.get_running_loop()
584+
self.loop = asyncio.get_running_loop()
585585
self.background_tasks: set[asyncio.Task[None]] = set()
586586

587587
def _get_header_value(self: Self, response: Any, header: str) -> Optional[str]:
@@ -679,10 +679,10 @@ def capture_envelope(self: Self, envelope: Envelope) -> None:
679679
except RuntimeError:
680680
# We are in a background thread, not running an event loop,
681681
# have to launch the task on the loop in a threadsafe way.
682-
if self._loop and self._loop.is_running():
682+
if self.loop and self.loop.is_running():
683683
asyncio.run_coroutine_threadsafe(
684684
self._capture_envelope(envelope),
685-
self._loop,
685+
self.loop,
686686
)
687687
else:
688688
# The event loop is no longer running
@@ -793,7 +793,7 @@ def kill(self: Self) -> Optional[asyncio.Task[None]]: # type: ignore
793793
self.background_tasks.clear()
794794
try:
795795
# Return the pool cleanup task so caller can await it if needed
796-
return self._loop.create_task(self._pool.aclose()) # type: ignore
796+
return self.loop.create_task(self._pool.aclose()) # type: ignore
797797
except RuntimeError:
798798
logger.warning("Event loop not running, aborting kill.")
799799
return None

tests/test_transport.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,47 @@ def close(self):
763763
assert seen == ["status_500"]
764764

765765

766+
def test_handle_request_error_basic_coverage(make_client, monkeypatch):
767+
client = make_client()
768+
transport = client.transport
769+
770+
monkeypatch.setattr(transport._worker, "submit", lambda fn: fn() or True)
771+
772+
# Track method calls
773+
calls = []
774+
775+
def mock_on_dropped_event(reason):
776+
calls.append(("on_dropped_event", reason))
777+
778+
def mock_record_lost_event(reason, data_category=None, item=None):
779+
calls.append(("record_lost_event", reason, data_category, item))
780+
781+
monkeypatch.setattr(transport, "on_dropped_event", mock_on_dropped_event)
782+
monkeypatch.setattr(transport, "record_lost_event", mock_record_lost_event)
783+
784+
# Test case 1: envelope is None
785+
transport._handle_request_error(envelope=None, loss_reason="test_reason")
786+
787+
assert len(calls) == 2
788+
assert calls[0] == ("on_dropped_event", "test_reason")
789+
assert calls[1] == ("record_lost_event", "network_error", "error", None)
790+
791+
# Reset
792+
calls.clear()
793+
794+
# Test case 2: envelope with items
795+
envelope = Envelope()
796+
envelope.add_item(mock.MagicMock()) # Simple mock item
797+
envelope.add_item(mock.MagicMock()) # Another mock item
798+
799+
transport._handle_request_error(envelope=envelope, loss_reason="connection_error")
800+
801+
assert len(calls) == 3
802+
assert calls[0] == ("on_dropped_event", "connection_error")
803+
assert calls[1][0:2] == ("record_lost_event", "network_error")
804+
assert calls[2][0:2] == ("record_lost_event", "network_error")
805+
806+
766807
@pytest.mark.asyncio
767808
@pytest.mark.skipif(not PY38, reason="Async transport requires Python 3.8+")
768809
async def test_async_transport_background_thread_capture(

0 commit comments

Comments
 (0)