question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Excessive use of asgiref.local and sync_to_async breaks other context local implementations

See original GitHub issue

I am writing on behalf of my employer, sentry.io.

I am not sure whether this issue should be filed against Django or asgiref. Starting with Django 3.1, signal handlers for got_request_exception are executed with the wrong contextvars context when running as part of an ASGI request. This can be easily demonstrated by wrapping the Django ASGI app in a middleware that stores something on the context, and trying to retreive that same value from that signal handler.

This is how I became aware of Django’s custom context local implementation that is shipped as part of asgiref. I understand why you do the things you do, but consider that this will break a lot of tracing tooling for Python that may only be orthogonal to Django or ASGI. The situation around context locals is already awful enough, it would be terrible if Sentry and tools like it had to implement a codepath specifically for Django 3 applications, or anything that uses asgiref.local.

I can see the following options:

  1. avoid releasing Django 3.1 with whatever changes just happened that made errorhandlers execute in the wrong contextvars context. Considering that Django 3.1 is already in alpha I would at least consider this as a stopgap measure.
  2. modify asgiref.sync_to_async and its inverse to “drive” contextvars if they are available such that contextvars always behave identically to asgiref.local
  3. drop asgiref.local entirely and swap it out for the aiocontextvars package on PyPI, as the primary purpose of implementing asgiref.local from scratch seemed to have been to support Python 3.6. One would still have to implement 2) for this to work, but the advantage here is that aiocontextvars seems to be significantly more popular across frameworks than asgiref.local.

Those options are not mutually exclusive.

Thanks for considering.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:25 (25 by maintainers)

github_iconTop GitHub Comments

2reactions
andrewgodwincommented, Jun 15, 2020

I know, we’ll have to work it out on the Django side. I’m tempted to change the requirement to include a ~=3.2 and a >=3.2.8.

1reaction
andrewgodwincommented, Jun 15, 2020

(also this might encourage me to finally put the Django test suite as part of tests for asgiref on here)

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use the asgiref.sync function in asgiref - Snyk
How to use the asgiref.sync function in asgiref. To help you get started, we've selected a few asgiref examples, based on popular ways...
Read more >
Source code for asgiref.sync - Django documentation
In Python 3.7+, the ThreadSensitiveContext() context manager may be used to ... This uses an asgiref # Local, not a threadlocal, so that...
Read more >
First commit after setting everything up. (905bd0e9) - GitLab
An Executor that actually runs code in the thread it is instantiated in. Passed to other threads running async code, so they can...
Read more >
asgiref - PyPI
Sync-to-async and async-to-sync function wrappers, asgiref.sync ... through from a task-local context to a thread-local context when you use sync_to_async ...
Read more >
Highest scored 'asgi' questions - Page 3 - Stack Overflow
Logging with django channels using thread local storage problem ... I'm building an ASGI application server in python and implementing the HTTP/2 protocol....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found