#34432 closed Bug (needsinfo)

autoreloader does not reload when a Thread is running and there is no way to notify the Thread to stop

Reported by: David Greaves Owned by: nobody
Component: Utilities Version: 4.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

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 :)

Change History (1)

comment:1 by Carlton Gibson, 20 months ago

Resolution: needsinfo
Status: newclosed

Closing pending the discussion on forum.

Note: See TracTickets for help on using tickets.
Back to Top