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

Unexcepted behavior while reloading #2294

Open
1 task done
TahaAhvazi opened this issue Mar 30, 2024 · 13 comments · May be fixed by #2317
Open
1 task done

Unexcepted behavior while reloading #2294

TahaAhvazi opened this issue Mar 30, 2024 · 13 comments · May be fixed by #2317

Comments

@TahaAhvazi
Copy link

TahaAhvazi commented Mar 30, 2024

Initial Checks

  • I'm aware that if I created this issue without a discussion, it may be closed without a response.

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
Copy link

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
Copy link
Sponsor Member

Kludex commented Apr 1, 2024

I need a computer with Windows.

@judevector
Copy link

Am having the same error and am using Windows

@TahaAhvazi
Copy link
Author

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
Copy link

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
Copy link
Author

TahaAhvazi commented Apr 13, 2024

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
Copy link

johanboekhoven commented Apr 17, 2024

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
Copy link

+1

@johanboekhoven
Copy link

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

@sotasan
Copy link

sotasan commented Apr 19, 2024

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

@maxfischer2781
Copy link
Contributor

maxfischer2781 commented Apr 26, 2024

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 maxfischer2781 linked a pull request Apr 26, 2024 that will close this issue
3 tasks
@maxfischer2781
Copy link
Contributor

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

@johanboekhoven
Copy link

johanboekhoven commented May 2, 2024

@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 λ 

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 a pull request may close this issue.

8 participants