﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
34432	autoreloader does not reload when a Thread is running and there is no way to notify the Thread to stop	David Greaves	nobody	"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 :)
"	Bug	closed	Utilities	4.1	Normal	needsinfo			Unreviewed	1	0	0	0	1	0
