[Django] #34432: autoreloader does not reload when a Thread is running and there is no way to notify the Thread to stop

11 views
Skip to first unread message

Django

unread,
Mar 23, 2023, 8:11:52 AM3/23/23
to django-...@googlegroups.com
#34432: autoreloader does not reload when a Thread is running and there is no way
to notify the Thread to stop
-----------------------------------------+------------------------
Reporter: David Greaves | Owner: nobody
Type: Bug | Status: new
Component: Utilities | Version: 4.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-----------------------------------------+------------------------
A running Thread prevents the autoloader from restarting.

Follow page 1 of the tutorial to create a polls app.
Add 'polls.apps.PollsConfig' to INSTALLED_APPS

Use this minimal apps.py in mysite/polls/:
{{{
import logging
import threading
import os
from django.apps import AppConfig


class PollsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'polls'

def ready(self):
logging.warning("In polls.ready")

if os.environ.get("RUN_MAIN") == "true":
logging.warning("Starting Thread")
ev = threading.Event()
self._thread = threading.Thread(target=ev.wait)
self._thread.start()
def stopper(**kwargs): # receiver needs **kwargs
logging.warning("Stopping the Thread")
ev.set()
try:
from django.utils.autoreload import autoreload_stopping
autoreload_stopping.connect(stopper)
except ImportError:
pass
}}}
run python manage.py runserver

make a change to apps.py

Result is that runserver hangs like so:

{{{
Quit the server with CONTROL-C.
/everything/devel/ad-hoc/django/mysite/polls/apps.py changed, reloading.
INFO:django.utils.autoreload:/everything/devel/ad-
hoc/django/mysite/polls/apps.py changed, reloading.
}}}

Apply patch:

https://github.com/django/django/pull/16674/commits/c07a1f35ac7bec04243f10a90df52e9ed0b77d0b

Result is correct reloading:
{{{
Quit the server with CONTROL-C.
/everything/devel/ad-hoc/django/mysite/polls/apps.py changed, reloading.
INFO:django.utils.autoreload:/everything/devel/ad-
hoc/django/mysite/polls/apps.py changed, reloading.
WARNING:root:Stopping the Thread
WARNING:root:In polls.ready
WARNING:root:Starting Thread
Watching for file changes with StatReloader
INFO:django.utils.autoreload:Watching for file changes with StatReloader
}}}

This bug is real and is intended to facilitate discussion of the patch :)

--
Ticket URL: <https://code.djangoproject.com/ticket/34432>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 23, 2023, 9:18:38 AM3/23/23
to django-...@googlegroups.com
#34432: autoreloader does not reload when a Thread is running and there is no way
to notify the Thread to stop
-------------------------------+--------------------------------------

Reporter: David Greaves | Owner: nobody
Type: Bug | Status: closed
Component: Utilities | Version: 4.1
Severity: Normal | Resolution: needsinfo

Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Carlton Gibson):

* status: new => closed
* resolution: => needsinfo


Comment:

Closing pending the [https://forum.djangoproject.com/t/id-like-to-add-an-
autoreload-stopping-signal-to-the-autloloader/19741 discussion on forum].

--
Ticket URL: <https://code.djangoproject.com/ticket/34432#comment:1>

Django

unread,
Jan 28, 2025, 5:39:09 PM1/28/25
to django-...@googlegroups.com
#34432: autoreloader does not reload when a Thread is running and there is no way
to notify the Thread to stop
-------------------------------+--------------------------------------
Reporter: David Greaves | Owner: nobody
Type: Bug | Status: closed
Component: Utilities | Version: 4.1
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------+--------------------------------------
Comment (by bohrax):

Just wanted to show support for this issue. I think the use case is very
valid, and that the solution is elegant. I am running similar patched
solution myself.
--
Ticket URL: <https://code.djangoproject.com/ticket/34432#comment:2>
Reply all
Reply to author
Forward
0 new messages