Skip to content

Commit 98181d8

Browse files
fix(django): django asgi support (when using debug mode) (#4445) (#4473)
This PR mitigates the following issue: #4305. Accessing ‘request.user’ is not supported in django async context. Support for ddtrace user tags in Django ASGI should be added in a follow up PR. Co-authored-by: Kyle Verhoog <[email protected]> (cherry picked from commit e95ab2d) Co-authored-by: Munir Abdinur <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 39748b2 commit 98181d8

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

ddtrace/contrib/django/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ def _after_request_tags(pin, span, request, response):
291291
# Note: getattr calls to user / user_is_authenticated may result in ImproperlyConfigured exceptions from
292292
# Django's get_user_model():
293293
# https://github.com/django/django/blob/a464ead29db8bf6a27a5291cad9eb3f0f3f0472b/django/contrib/auth/__init__.py
294+
#
295+
# FIXME: getattr calls to user fail in async contexts.
296+
# Sample Error: django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context
297+
# - use a thread or sync_to_async.
294298
try:
295299
if hasattr(user, "is_authenticated"):
296300
span._set_str_tag("django.user.is_authenticated", str(user_is_authenticated(user)))
@@ -304,7 +308,7 @@ def _after_request_tags(pin, span, request, response):
304308
if username:
305309
span._set_str_tag("django.user.name", username)
306310
except Exception:
307-
log.debug("Error retrieving authentication information for user %r", user, exc_info=True)
311+
log.debug("Error retrieving authentication information for user", exc_info=True)
308312

309313
if response:
310314
status = response.status_code
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
django: avoid ``SynchronousOnlyOperation`` when failing to retrieve user information.

0 commit comments

Comments
 (0)