Skip to content

Commit f18d02a

Browse files
authored
fix(django): django asgi support (when using debug mode) (backport #4445) (#4472)
This is an automatic backport of pull request #4445 done by [Mergify](https://mergify.com). --- <details> <summary>Mergify commands and options</summary> <br /> More conditions and actions can be found in the [documentation](https://docs.mergify.com/). You can also trigger Mergify actions by commenting on this pull request: - `@Mergifyio refresh` will re-evaluate the rules - `@Mergifyio rebase` will rebase this PR on its base branch - `@Mergifyio update` will merge the base branch into this PR - `@Mergifyio backport <destination>` will backport this PR on `<destination>` branch Additionally, on Mergify [dashboard](https://dashboard.mergify.com/) you can: - look at your merge queues - generate the Mergify configuration with the config editor. Finally, you can contact us on https://mergify.com </details>
1 parent 1c9c621 commit f18d02a

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
@@ -292,6 +292,10 @@ def _after_request_tags(pin, span, request, response):
292292
# Note: getattr calls to user / user_is_authenticated may result in ImproperlyConfigured exceptions from
293293
# Django's get_user_model():
294294
# https://github.com/django/django/blob/a464ead29db8bf6a27a5291cad9eb3f0f3f0472b/django/contrib/auth/__init__.py
295+
#
296+
# FIXME: getattr calls to user fail in async contexts.
297+
# Sample Error: django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context
298+
# - use a thread or sync_to_async.
295299
try:
296300
if hasattr(user, "is_authenticated"):
297301
span.set_tag_str("django.user.is_authenticated", str(user_is_authenticated(user)))
@@ -305,7 +309,7 @@ def _after_request_tags(pin, span, request, response):
305309
if username:
306310
span.set_tag_str("django.user.name", username)
307311
except Exception:
308-
log.debug("Error retrieving authentication information for user %r", user, exc_info=True)
312+
log.debug("Error retrieving authentication information for user", exc_info=True)
309313

310314
if response:
311315
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)