encode / uvicorn

An ASGI web server, for Python. 🦄
https://www.uvicorn.org/
BSD 3-Clause "New" or "Revised" License
8.57k stars 745 forks source link

Unexcepted behavior while reloading #2294

Closed TahaAhvazi closed 5 months ago

TahaAhvazi commented 7 months ago

Initial Checks

Description

Hello, in the uvicorn version 0.29.0 which is the latest version on 3/30/2024 when I wrote my code updated it, and saved the changes, I saw unexpected information in the terminal that I mentioned below in the code example, where this error is not occurring in the previous versions of uvicorn like:0.27.1

Example Code

INFO:     Shutting down
INFO:     Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO:     Finished server process [3608]
Process SpawnProcess-2:
Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python310\lib\multiprocessing\process.py", line 314, in _bootstrap
    self.run()
  File "C:\Users\AppData\Local\Programs\Python\Python310\lib\multiprocessing\process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\backend\env\lib\site-packages\uvicorn\_subprocess.py", line 78, in subprocess_started
    target(sockets=sockets)
  File "C:\-backend\env\lib\site-packages\uvicorn\server.py", line 65, in run
    return asyncio.run(self.serve(sockets=sockets))
  File "C:\Users\AppData\Local\Programs\Python\Python310\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 633, in run_until_complete
    self.run_forever()
  File "C:\Users\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 600, in run_forever
    self._run_once()
  File "C:\Users\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 1896, in _run_once
    handle._run()
  File "C:\UsersAppData\Local\Programs\Python\Python310\lib\asyncio\events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "C:\-backend\env\lib\site-packages\uvicorn\server.py", line 68, in serve
    with self.capture_signals():
  File "C:\Users\AppData\Local\Programs\Python\Python310\lib\contextlib.py", line 142, in __exit__
    next(self.gen)
  File "C:\backend\env\lib\site-packages\uvicorn\server.py", line 328, in capture_signals
    signal.raise_signal(captured_signal)
KeyboardInterrupt
INFO:     Started server process [8232]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

Python, Uvicorn & OS Version

Windows11 Enterprise 64-bit (10.0, Build 22000)
Python 3.10.6
uvicorn 0.29.0

[!IMPORTANT]

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.

Fund with Polar

judeVector commented 7 months ago

Exactly I have been experiencing this since whenever I restart my FastAPI application, it doesnt restart gracefully but logs this exact message like yours

Kludex commented 7 months ago

I need a computer with Windows.

judeVector commented 7 months ago

Am having the same error and am using Windows

TahaAhvazi commented 7 months ago

Exactly I have been experiencing this since whenever I restart my FastAPI application, it doesnt restart gracefully but logs this exact message like yours

Yes exactly, and its just appears in the latest version of uvicorn I donwgraded mmy uvicorn version to 0.27.1 and its totally fine

anderer455 commented 7 months ago

Just started learning and experimenting since yesterday using FastAPI for the first time. I have same problem.

Every Ctrl+S or any file save in the repo (folder being watched for changes) reloads the app with no problem, but the terminal is cluttered with this exception every single time.

Btw the reload of app is being done on file save even when there were no changes the file (which I didnt experience with saving a file without changes in Angular app for example)

TahaAhvazi commented 7 months ago

Just started learning and experimenting since yesterday using FastAPI for the first time. I have same problem.

Every Ctrl+S or any file save in the repo (folder being watched for changes) reloads the app with no problem, but the terminal is cluttered with this exception every single time.

Btw the reload of app is being done on file save even when there were no changes the file (which I didnt experience with saving a file without changes in Angular app for example)

Hi, i had the same problem. for now,you can use the previous version of uvicorn and solve the problem you can use version : 0.27.1

johanboekhoven commented 7 months ago

Just to add my me too here, me too. Windows machine, PyEnv en Poetry, tried 3 python versions 3.9/3.11/3.12 all same issue. Rolling back to uvicorn 0.27.1 fixed the issue with --reload

FAbrahamDev commented 7 months ago

+1

johanboekhoven commented 7 months ago

Maybe a clue is that when I run it in command it behaves better than running it in powershell.

sotasan commented 7 months ago

The error occurs only in 0.29.0, but 0.28.1 just works fine.

maxfischer2781 commented 6 months ago

Note that this isn't an error per-se. This is a traceback from the updated shutdown procedure which propagates the killing signal if one occurred (as happens automatically for reloading, or on pressing [Ctrl]+[C], etc.). It is not an error in the application or server.

Still it seems sensible to suppress this in situations in which the propagation is expected. That should be when the server is not run programmatically, i.e. via Server.run inside main.
Since the goal is just to suppress the traceback on graceful shutdown (i.e. KeyboardInterrupt) it should be sufficient to catch KeyboardInterrupt, disable sys.excepthook, and then re-raise the exception. Turns out this happens deeply inside multiprocessing. I've addressed this in #2317.

maxfischer2781 commented 6 months ago

Can someone check whether #2317 fixes this issues on Windows? I was only able to test this on MacOS and Linux.

johanboekhoven commented 6 months ago

@maxfischer2781 I've run both 0.29.0 and https://github.com/maxfischer2781/uvicorn/tree/suppress-signal-traceback via poetry and your basic test.py.

Result is your fix works fine on this sample (first output below) and the 0.29.0 fails.

Jhn in F:\_test_projects\uvicorn λ poetry update uvicorn
Updating dependencies
Resolving dependencies... (16.3s)

Package operations: 0 installs, 1 update, 0 removals

  - Updating uvicorn (0.29.0 -> 0.29.0 cf88d65)

Writing lock file
Jhn in F:\_test_projects\uvicorn λ poetry run python -m uvicorn test:dummy_app --reload
INFO:     Will watch for changes in these directories: ['F:\\_test_projects\\uvicorn']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [57072] using StatReload
INFO:     Started server process [61392]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Shutting down
INFO:     Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO:     Finished server process [61392]
INFO:     Stopping reloader process [57072]
Jhn in F:\_test_projects\uvicorn λ poetry update uvicorn
Updating dependencies
Resolving dependencies... (0.1s)

Package operations: 0 installs, 1 update, 0 removals

  - Updating uvicorn (0.29.0 cf88d65 -> 0.29.0)

Writing lock file
Jhn in F:\_test_projects\uvicorn λ poetry run python -m uvicorn test:dummy_app --reload
INFO:     Will watch for changes in these directories: ['F:\\_test_projects\\uvicorn']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [54420] using StatReload
INFO:     Started server process [44236]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Shutting down
INFO:     Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO:     Finished server process [44236]
Process SpawnProcess-1:
Traceback (most recent call last):
  File "C:\Users\Jhn\.pyenv\pyenv-win\versions\3.9.10\lib\multiprocessing\process.py", line 315, in _bootstrap
    self.run()
  File "C:\Users\Jhn\.pyenv\pyenv-win\versions\3.9.10\lib\multiprocessing\process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "F:\_test_projects\uvicorn\.venv\lib\site-packages\uvicorn\_subprocess.py", line 78, in subprocess_started
    target(sockets=sockets)
  File "F:\_test_projects\uvicorn\.venv\lib\site-packages\uvicorn\server.py", line 65, in run
    return asyncio.run(self.serve(sockets=sockets))
  File "C:\Users\Jhn\.pyenv\pyenv-win\versions\3.9.10\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\Jhn\.pyenv\pyenv-win\versions\3.9.10\lib\asyncio\base_events.py", line 629, in run_until_complete
    self.run_forever()
  File "C:\Users\Jhn\.pyenv\pyenv-win\versions\3.9.10\lib\asyncio\base_events.py", line 596, in run_forever
    self._run_once()
  File "C:\Users\Jhn\.pyenv\pyenv-win\versions\3.9.10\lib\asyncio\base_events.py", line 1890, in _run_once
    handle._run()
  File "C:\Users\Jhn\.pyenv\pyenv-win\versions\3.9.10\lib\asyncio\events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "F:\_test_projects\uvicorn\.venv\lib\site-packages\uvicorn\server.py", line 69, in serve
    await self._serve(sockets)
  File "C:\Users\Jhn\.pyenv\pyenv-win\versions\3.9.10\lib\contextlib.py", line 126, in __exit__
    next(self.gen)
  File "F:\_test_projects\uvicorn\.venv\lib\site-packages\uvicorn\server.py", line 328, in capture_signals
    signal.raise_signal(captured_signal)
KeyboardInterrupt
INFO:     Stopping reloader process [54420]
Jhn in F:\_test_projects\uvicorn λ