Skip to content

Commit 47416f4

Browse files
committed
ref(transport): Fix event loop check in make_transport
Fix the event loop check in make_transport so that it does not throw a runtime error but rather falls back correctly. GH-4582
1 parent 90346a5 commit 47416f4

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

sentry_sdk/transport.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,11 +1021,14 @@ def make_transport(options: Dict[str, Any]) -> Optional[Transport]:
10211021
use_http2_transport = options.get("_experiments", {}).get("transport_http2", False)
10221022
use_async_transport = options.get("_experiments", {}).get("transport_async", False)
10231023
# By default, we use the http transport class
1024-
if use_async_transport and asyncio.get_running_loop() is not None:
1025-
transport_cls: Type[Transport] = AsyncHttpTransport
1026-
else:
1027-
use_http2 = use_http2_transport
1028-
transport_cls = Http2Transport if use_http2 else HttpTransport
1024+
if use_async_transport:
1025+
try:
1026+
asyncio.get_running_loop()
1027+
transport_cls: Type[Transport] = AsyncHttpTransport
1028+
except RuntimeError:
1029+
# No event loop running, fall back to sync transport
1030+
logger.warning("No event loop running, falling back to sync transport.")
1031+
transport_cls = Http2Transport if use_http2_transport else HttpTransport
10291032

10301033
if isinstance(ref_transport, Transport):
10311034
return ref_transport

0 commit comments

Comments
 (0)