#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:16 Carlton Gibson]:
> Being able to share the context between multiple concurrent
sync_to_async tasks seems like something that's generally useful, and in-
line with the create_task and TaskGroup API. So, that's a useful step
independently I'd think. Thanks!
I've prepared a patch — https://github.com/django/django/pull/20288 — but
ran into a problem 😔
I didn't run the full test suite and overlooked an issue in
`asgi.tests.ASGITest.test_assert_in_listen_for_disconnect`. That test uses
`ApplicationCommunicator`, which internally does:
{{{
self._future = contextvars.Context().run(
asyncio.create_task,
self.application(
self.scope, self.input_queue.get, self.output_queue.put
),
)
}}}
As a result, we hit this error:
{{{
RuntimeError: cannot enter context: <_contextvars.Context object at
0x104939040> is already entered
}}}
So `ApplicationCommunicator` is already setting a context, and we can't
override it using `asyncio.create_task(coro(), context=ctx)`. I'm looking
for a workaround, but it seems like this may require another `asgiref`
release 😔
If you have any ideas on how to handle this, please let me know.
--
Ticket URL: <https://code.djangoproject.com/ticket/36714#comment:20>
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/0107019aa6b27cea-2a299d3c-7e70-4a79-b9dd-c5027fe6e0eb-000000%40eu-central-1.amazonses.com.