#32815: Failed to reset ContextVars in sync/async middlewares
-------------------------------------+-------------------------------------
     Reporter:  Michael Manganiello  |                    Owner:  nobody
         Type:  Uncategorized        |                   Status:  new
    Component:  Uncategorized        |                  Version:  3.2
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Michael Manganiello):

 It seems this is not an issue only related to middlewares, but to how
 `sync_to_async` works in general? I am able to reproduce the same issue
 with this simple endpoint (when running Django using Gunicorn with Uvicorn
 workers):

 {{{
 import contextvars

 from asgiref.sync import sync_to_async
 from django.http import HttpResponse
 from django.urls import path

 current_context = contextvars.ContextVar('current_context')


 async def healthcheck(request):
     token = await sync_to_async(current_context.set,
 thread_sensitive=True)(id(request))
     await sync_to_async(current_context.reset,
 thread_sensitive=True)(token)
     return HttpResponse('OK')

 urlpatterns = [path('', healthcheck)]
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32815#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/068.d7c6b97e727831b434ec8bd3c2ff5800%40djangoproject.com.

Reply via email to