#32049: Middleware behaviour inconsistent between runserver and daphne
-------------------------------------+-------------------------------------
     Reporter:  Hugo Osvaldo         |                    Owner:  nobody
  Barrera                            |
         Type:  Bug                  |                   Status:  closed
    Component:  HTTP handling        |                  Version:  3.1
     Severity:  Normal               |               Resolution:  invalid
     Keywords:  ASGI                 |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

 * cc: Andrew Godwin (added)
 * keywords:   => ASGI
 * status:  new => closed
 * resolution:   => invalid


Comment:

 This is an interesting one. The issue is the way the ASGIHandler runs your
 code in separate threads. (The bottom-line will be that what you're trying
 to do isn't supported: you're going to have to find a thread-safe way of
 doing this if you want to use ASGI here, now.)

 Your middleware sets `connection.tenant` in Thread 1. Via the
 `DefaultConnectionProxy` this fetches and sets the property on the
 `"default"` connection for Thread 1.
 Your view is then run on Thread 2. Attempting to get `connection.tenant`
 the  `DefaultConnectionProxy` fetches the `"default"` connection for
 Thread 2.
 Since DB connections are not shared between thread, this is not the same
 connection, so the `tenant` attribute you set doesn't exist.

 I'll cc Andrew since it's an interesting case, but it's expected
 behaviour. ASGI/async implies/requires much more thought about what thread
 you're going to be running on here, and hence how you pass this kind of
 info around.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32049#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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/068.b6741238537718811790a0c2fb764c1e%40djangoproject.com.

Reply via email to