Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reload process behavior when exception is raised #1313

Merged
merged 2 commits into from
Jan 11, 2022
Merged

Conversation

Kludex
Copy link
Sponsor Member

@Kludex Kludex commented Jan 8, 2022

Current master (and latest version [0.16.0]) makes the --reload command to not work as expected.

Considering a simple application:

from fastapi import FastAPI

app = FastAPI()

@app.on_event("startup")
async def startup():
    ...


@app.get("/")
def home():
    ...

When running uvicorn main:app --reload when the code above, and removing one of the lines with ..., the reload process will exit. We don't want that. #1115 was not true for --reload.

The output for it can be seen below (after that, the process exits):

~/Development via 🐍 v3.9.7 via 🅒 uvicorn3.9 on ☁️  marcelotryle@gmail.com took 9suvicorn main:app --reload
INFO:     Will watch for changes in these directories: ['/home/marcelo/Development']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [328680] using watchgod
INFO:     Started server process [328682]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
WARNING:  WatchGodReload detected file change in '['/home/marcelo/Development/main.py']'. Reloading...
INFO:     Shutting down
INFO:     Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO:     Finished server process [328682]
Process SpawnProcess-2:
Traceback (most recent call last):
  File "/home/marcelo/anaconda3/envs/uvicorn3.9/lib/python3.9/multiprocessing/process.py", line 315, in _bootstrap
    self.run()
  File "/home/marcelo/anaconda3/envs/uvicorn3.9/lib/python3.9/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/home/marcelo/Development/encode/uvicorn/uvicorn/subprocess.py", line 76, in subprocess_started
    target(sockets=sockets)
  File "/home/marcelo/Development/encode/uvicorn/uvicorn/server.py", line 67, in run
    return asyncio.run(self.serve(sockets=sockets))
  File "/home/marcelo/anaconda3/envs/uvicorn3.9/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "uvloop/loop.pyx", line 1501, in uvloop.loop.Loop.run_until_complete
  File "/home/marcelo/Development/encode/uvicorn/uvicorn/server.py", line 74, in serve
    config.load()
  File "/home/marcelo/Development/encode/uvicorn/uvicorn/config.py", line 458, in load
    self.loaded_app = import_from_string(self.app)
  File "/home/marcelo/Development/encode/uvicorn/uvicorn/importer.py", line 21, in import_from_string
    module = importlib.import_module(module_str)
  File "/home/marcelo/anaconda3/envs/uvicorn3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 846, in exec_module
  File "<frozen importlib._bootstrap_external>", line 983, in get_code
  File "<frozen importlib._bootstrap_external>", line 913, in source_to_code
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/home/marcelo/Development/./main.py", line 11
    def home():
               ^
IndentationError: expected an indented block
INFO:     Stopping reloader process [328680]

The changes proposed here makes the reload process to work as expected i.e. reload the uvicorn server, and not exit when modifications that are not able to run are made.

@abersheeran
Copy link
Member

abersheeran commented Jan 11, 2022

Although this PR solves the problem, I prefer this one #1205 😀

@Kludex
Copy link
Sponsor Member Author

Kludex commented Jan 11, 2022

That one will take more time to be considered/merge. This one just fixes the broken behavior.

Thanks @abersheeran :)

@Kludex Kludex merged commit c6a7db5 into master Jan 11, 2022
@Kludex Kludex deleted the fix/reload branch January 11, 2022 09:12
Kludex added a commit to sephioh/uvicorn that referenced this pull request Oct 29, 2022
* Fix reload process behavior when exception is raised

* Remove test introduced by change
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants