Skip to content

Commit 176a1d1

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 23b8ea2 commit 176a1d1

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
@@ -1019,11 +1019,14 @@ def make_transport(options: Dict[str, Any]) -> Optional[Transport]:
10191019
use_http2_transport = options.get("_experiments", {}).get("transport_http2", False)
10201020
use_async_transport = options.get("_experiments", {}).get("transport_async", False)
10211021
# By default, we use the http transport class
1022-
if use_async_transport and asyncio.get_running_loop() is not None:
1023-
transport_cls: Type[Transport] = AsyncHttpTransport
1024-
else:
1025-
use_http2 = use_http2_transport
1026-
transport_cls = Http2Transport if use_http2 else HttpTransport
1022+
if use_async_transport:
1023+
try:
1024+
asyncio.get_running_loop()
1025+
transport_cls: Type[Transport] = AsyncHttpTransport
1026+
except RuntimeError:
1027+
# No event loop running, fall back to sync transport
1028+
logger.warning("No event loop running, falling back to sync transport.")
1029+
transport_cls = Http2Transport if use_http2_transport else HttpTransport
10271030

10281031
if isinstance(ref_transport, Transport):
10291032
return ref_transport

0 commit comments

Comments
 (0)