#36714: Async signals lose ContextVar state due to use of asyncio.gather
----------------------------------+------------------------------------
     Reporter:  Mykhailo Havelia  |                    Owner:  (none)
         Type:  Bug               |                   Status:  new
    Component:  HTTP handling     |                  Version:  dev
     Severity:  Normal            |               Resolution:
     Keywords:  asyncio, signals  |             Triage Stage:  Accepted
    Has patch:  0                 |      Needs documentation:  0
  Needs tests:  0                 |  Patch needs improvement:  0
Easy pickings:  0                 |                    UI/UX:  0
----------------------------------+------------------------------------
Comment (by Mykhailo Havelia):

 Replying to [comment:8 Carlton Gibson]:
 > I reviewed #36315 and marked it as Ready for checkin, so hopefully that
 can progress to clear the path for work here.
 >
 Test MR: https://github.com/Arfey/django/pull/3/files

 I used the latest changes and prepared a fix for context sharing. At this
 point, we have:

 - Successful context sharing between async handlers for asend/send
 - Successful context sharing between sync handlers for asend/send
 - Successful cross-sharing between async/sync handlers for send

 Current issue:

 - Cross-sharing between async/sync handlers for asend

 The difference is in how send and asend execute handlers. `send` runs sync
 handlers first, and then runs async handlers in parallel and this works
 fine. `asend` tries to run all handlers "in parallel". In this case,
 `sync_to_async` copies the context instead of using the existing one, so
 context sharing breaks. I propose splitting the execution of sync and
 async handlers for asend as well.

 Current code:

 {{{
 await _gather(
     sync_send(),
     _gather(
         *(
             receiver(signal=self, sender=sender, **named)
             for receiver in async_receivers
         )
     ),
 )
 }}}

 Proposed adjustment:


 {{{

 await sync_send()
 await _gather(
     *(
         receiver(signal=self, sender=sender, **named)
         for receiver in async_receivers
     )
 )
 }}}

 What do you think?
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36714#comment:9>
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 [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/0107019a6af5eab2-26b85bb5-0e60-4c25-ac0d-1fc54256df07-000000%40eu-central-1.amazonses.com.

Reply via email to