Skip to content

Commit 7fe9e06

Browse files
authored
Fix patching of AsgiHandler in Django Channels >= 3.0 (#912)
1 parent 549b7df commit 7fe9e06

File tree

1 file changed

+18
-10
lines changed
  • sentry_sdk/integrations/django

1 file changed

+18
-10
lines changed

sentry_sdk/integrations/django/asgi.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,30 @@ async def sentry_patched_get_response_async(self, request):
5353
def patch_channels_asgi_handler_impl(cls):
5454
# type: (Any) -> None
5555

56+
import channels # type: ignore
5657
from sentry_sdk.integrations.django import DjangoIntegration
5758

58-
old_app = cls.__call__
59+
if channels.__version__ < "3.0.0":
5960

60-
async def sentry_patched_asgi_handler(self, receive, send):
61-
# type: (Any, Any, Any) -> Any
62-
if Hub.current.get_integration(DjangoIntegration) is None:
63-
return await old_app(self, receive, send)
61+
old_app = cls.__call__
6462

65-
middleware = SentryAsgiMiddleware(
66-
lambda _scope: old_app.__get__(self, cls), unsafe_context_data=True
67-
)
63+
async def sentry_patched_asgi_handler(self, receive, send):
64+
# type: (Any, Any, Any) -> Any
65+
if Hub.current.get_integration(DjangoIntegration) is None:
66+
return await old_app(self, receive, send)
6867

69-
return await middleware(self.scope)(receive, send)
68+
middleware = SentryAsgiMiddleware(
69+
lambda _scope: old_app.__get__(self, cls), unsafe_context_data=True
70+
)
7071

71-
cls.__call__ = sentry_patched_asgi_handler
72+
return await middleware(self.scope)(receive, send)
73+
74+
cls.__call__ = sentry_patched_asgi_handler
75+
76+
else:
77+
# The ASGI handler in Channels >= 3 has the same signature as
78+
# the Django handler.
79+
patch_django_asgi_handler_impl(cls)
7280

7381

7482
def wrap_async_view(hub, callback):

0 commit comments

Comments
 (0)