[Django] #35807: Clarify django.urls.set_urlconf scoping behaviour

21 views
Skip to first unread message

Django

unread,
Oct 2, 2024, 5:26:50 PM10/2/24
to django-...@googlegroups.com
#35807: Clarify django.urls.set_urlconf scoping behaviour
-----------------------------+-----------------------------------------
Reporter: Enrico Zini | Type: Uncategorized
Status: new | Component: Documentation
Version: 5.0 | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+-----------------------------------------
`django.urls.set_urlconf`
[https://github.com/django/django/blob/main/django/urls/base.py#L128
docstring] mentions setting the urlconf for the current thread. However,
this is backed by `asgiref.local.Local`, which is supposed to provide
scoping features related to asyncio tasks as well. This becomes relevant,
for example, when doing multi-tenancy with more than one urlconf and
trying to call `django.urls.reverse` in an ASGI application.

I have been trying to infer what is the expected behaviour in async Django
code by following the current implementation, and I found that
`asgiref.local.Local` behaviour has changed over time (see
https://github.com/django/asgiref/issues/473).

I assume that using `asgiref.local.Local` instead of `threading.local`
hints at an intention is to give `set_urlconf`/`get_urlconf` meaningful
semantics for Channels consumers or ASGI applications.

Whether the intention is to isolate `set_urlconf`/`get_urlconf` across
different asyncio tasks or to only support isolation between threads, I
suppose it would be useful if their behaviour was documented also for the
case of asyncio code, especially given they back `django.urls.reverse`.
--
Ticket URL: <https://code.djangoproject.com/ticket/35807>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 7, 2024, 3:20:00 AM10/7/24
to django-...@googlegroups.com
#35807: Clarify django.urls.set_urlconf scoping behaviour
-------------------------------+--------------------------------------
Reporter: Enrico Zini | Owner: (none)
Type: Uncategorized | Status: closed
Component: Documentation | Version: 5.0
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Sarah Boyce):

* cc: Andrew Godwin, Carlton Gibson (added)
* resolution: => needsinfo
* status: new => closed

Comment:

I'm struggling to follow what this is asking for - can you share an
example of the behavior you're seeing?
From what I can see, both async and sync requests handle the urlconf the
same - it is the ROOT_URLCONF unless set by a middleware
[https://docs.djangoproject.com/en/dev/topics/http/urls/#how-django-
processes-a-request as documented].
--
Ticket URL: <https://code.djangoproject.com/ticket/35807#comment:1>

Django

unread,
Oct 7, 2024, 3:30:45 AM10/7/24
to django-...@googlegroups.com
#35807: Clarify django.urls.set_urlconf scoping behaviour
-------------------------------+--------------------------------------
Reporter: Enrico Zini | Owner: (none)
Type: Uncategorized | Status: closed
Component: Documentation | Version: 5.0
Severity: Normal | Resolution: invalid
Keywords: | 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):

* resolution: needsinfo => invalid

Comment:

Firstly, just for Django, set_urlconf is not public API so you shouldn't
use it. Rather prefer the documented `HttpRequest.urlconf` attribute
([https://docs.djangoproject.com/en/5.1/ref/request-
response/#django.http.HttpRequest.urlconf docs]) to set per-tenant URLs in
a middleware, if that's your strategy.

Secondly, the [https://github.com/django/asgiref/issues/473 linked asgiref
issue (#473)] is in progress, and should be resolved shortly. (Setting
locals has been leaking out of asyncio tasks since the recent v3.8, which
is a regression.) It's not a Django issue.

If you need further advice please see
TicketClosingReasons/UseSupportChannels.
--
Ticket URL: <https://code.djangoproject.com/ticket/35807#comment:2>

Django

unread,
Oct 7, 2024, 3:54:13 AM10/7/24
to django-...@googlegroups.com
#35807: Clarify django.urls.set_urlconf scoping behaviour
-------------------------------+--------------------------------------
Reporter: Enrico Zini | Owner: (none)
Type: Uncategorized | Status: closed
Component: Documentation | Version: 5.0
Severity: Normal | Resolution: invalid
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 Enrico Zini):

From what I could see, "Set the URLconf for the current thread" in
`set_urlconf`'s docstring looks like a leftover from the pre-async times
and could become "Set the URLconf for the current thread or asyncio task".

I understand it's not a public API and indeed I only set `request.urlconf`
in my code. We spotted the docstring while trying to double check the
validity of what we were designing on the async case, and had some extra
digging to do to reassure ourselves we were still doing the right thing,
hence this issue.

I'm surprised the ticket got closed, but fair enough.
--
Ticket URL: <https://code.djangoproject.com/ticket/35807#comment:3>

Django

unread,
Oct 7, 2024, 4:04:33 AM10/7/24
to django-...@googlegroups.com
#35807: Clarify django.urls.set_urlconf scoping behaviour
--------------------------------------+------------------------------------
Reporter: Enrico Zini | Owner: (none)
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Carlton Gibson):

* resolution: invalid =>
* stage: Unreviewed => Accepted
* status: closed => new
* type: Uncategorized => Cleanup/optimization

Comment:

Yes, OK, just changing the docstring would be fine.
--
Ticket URL: <https://code.djangoproject.com/ticket/35807#comment:4>

Django

unread,
Oct 7, 2024, 4:09:13 AM10/7/24
to django-...@googlegroups.com
#35807: Clarify django.urls.set_urlconf scoping behaviour
-------------------------------------+-------------------------------------
Reporter: Enrico Zini | Owner: Carlton
Type: | Gibson
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* has_patch: 0 => 1
* owner: (none) => Carlton Gibson
* status: new => assigned

Comment:

[https://github.com/django/django/pull/18654 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/35807#comment:5>

Django

unread,
Oct 7, 2024, 5:16:32 AM10/7/24
to django-...@googlegroups.com
#35807: Clarify django.urls.set_urlconf scoping behaviour
-------------------------------------+-------------------------------------
Reporter: Enrico Zini | Owner: Carlton
Type: | Gibson
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/35807#comment:6>

Django

unread,
Oct 7, 2024, 5:17:20 AM10/7/24
to django-...@googlegroups.com
#35807: Clarify django.urls.set_urlconf scoping behaviour
-------------------------------------+-------------------------------------
Reporter: Enrico Zini | Owner: Carlton
Type: | Gibson
Cleanup/optimization | Status: closed
Component: Documentation | Version: 5.0
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce <42296566+sarahboyce@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"d876be794f59fd37c8edc33ecde4577d820a9c75" d876be79]:
{{{#!CommitTicketReference repository=""
revision="d876be794f59fd37c8edc33ecde4577d820a9c75"
Fixed #35807 -- Mentioned async case for internal get/set urlconf helpers.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35807#comment:7>
Reply all
Reply to author
Forward
0 new messages