uiuc-mrdc / cogs

Containerized Game Services
3 stars 0 forks source link

Django fails on code reload due to async #17

Closed mgale456 closed 4 years ago

mgale456 commented 4 years ago

Django normally restarts if you update a python file.

Now the restart function seems to crash due to an async error.

This is very very very annoying for development.

lpulley commented 4 years ago

What exactly happens / how to reproduce?

mgale456 commented 4 years ago

Sorry.

Start both containers normally Edit a .py file in Django Save it Django crashes in the same way every time no matter what file you edit

Additional info: Django checks for updates to the files normally, and is supposed to restart the server if there are any changes. When you start up the Django server, you can see it says Watching for file changes with StatReloader and when you save a file, it says /path/to/file.py changed, reloading. This is where it crashes now.

It does not check the .html templates, since it always serves whatever is there, and it may skip some of the .py files, but it probably checks them all

lpulley commented 4 years ago

Sorry it took me so long to get back to you on this. This doesn't happen to me on master, is this a new thing in a dev branch?

mgale456 commented 4 years ago

I get this on master too, but I noticed it on the dev branches

Here's the exact steps I took:

closed old containers with: docker-compose down -v --rmi all switched to master restored any edited files listed in git status followed standard startup: docker-compose up -d --build docker-compose restart docker-compose exec web python manage.py migrate docker logs -f cogs_web_1

re-saved a .py file (deleted and replaced one character, so no overall change)

below are the error messages from the logs:

/code/team_management/views.py changed, reloading.
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 60, in execute
    super().execute(*args, **options)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.8/site-packages/channels/management/commands/runserver.py", line 59, in handle
    super().handle(*args, **options)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 95, in handle
    self.run(**options)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 102, in run
    autoreload.run_with_reloader(self.inner_run, **options)
  File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 599, in run_with_reloader
    start_django(reloader, main_func, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 584, in start_django
    reloader.run(django_main_thread)
  File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 299, in run
    self.run_loop()
  File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 305, in run_loop
    next(ticker)
  File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 353, in tick
    self.notify_file_changed(filepath)
  File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 328, in notify_file_changed
    trigger_reload(path)
  File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 223, in trigger_reload
    sys.exit(3)
SystemExit: 3

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 341, in run_from_argv
    connections.close_all()
  File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 230, in close_all
    connection.close()
  File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 22, in inner
    raise SynchronousOnlyOperation(message)
django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.
lpulley commented 4 years ago

I still am not able to reproduce this on master. I did exactly the steps you listed. What's your environment? OS, Docker version? Are you certain you have the latest code from git pull? Maybe try git reset --hard to make sure you've discarded all changes?

For the record, I'm on Ubuntu 18.04.3 with Docker 19.03.6.

lpulley commented 4 years ago

image

Here is the successful output I got several times, on different files, including the views.py you tested.

lpulley commented 4 years ago

It's also possible that you ran docker-compose restart to soon and the database didn't get a chance to finish setting up? Try starting for the first time without the -d flag so that you can wait until the database says it's open on port 3306.

mgale456 commented 4 years ago

I still get this error using Docker Engine 19.03.5 on Windows 10.

I will try it in an Ubuntu VM in VirtualBox

mgale456 commented 4 years ago

Worked in my Ubuntu VM, and I noticed a lot more output when building web from requirements.txt.

I used docker system prune to remove stuff, including the build cache, and I think that may have fixed it. Maybe one of the Python packages was outdated. I guess it could have been using Django 2 still or something. I don't know.

It works on Windows now Closing. Thanks for the help