Tortoise-orm: Uvicorn error with the fastapi example provided in the docs

Created on 21 Oct 2020  路  3Comments  路  Source: tortoise/tortoise-orm

Describe the bug
uvicorn error when you run the fastapi example in the docs.

WARNING:  Detected file change in 'main.py'. Reloading...
INFO:     Shutting down
INFO:uvicorn.error:Shutting down
INFO:     Waiting for application shutdown.
INFO:uvicorn.error:Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO:uvicorn.error:Application shutdown complete.
INFO:     Finished server process [141]
INFO:uvicorn.error:Finished server process [141]
INFO:     Started server process [145]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:uvicorn.error:Application startup complete.

To Reproduce
Code taken from the fastapi example in the docs.
models.py
main.py
server started with docker using

CMD ["uvicorn", "main:app", "--reload", "--workers", "1", "--host", "0.0.0.0", "--port", "8000"]

Expected behavior
No uvicorn.error during start/shutdown/restart phase

Additional context
I suspect starting up, getting and closing the connections to the db is causing the issue.
Possible issue would be how tortoise handles the on startup and shutdown events of the app.

Most helpful comment

While it is technically not an error, it is more the issue of the Tortoise's FastAPI integration:

https://github.com/tortoise/tortoise-orm/blob/767cd5d563a36e7795f0331082f6ee51297b661d/tortoise/contrib/fastapi/__init__.py#L93-L96

It uses root logger which creates handler with NOSET level. Neither FastAPI nor uvicorn set up the root logger, but uvicorn logger by default propagates log records to root one repeating the messages.

The workaround is either disable propagation for uvicorn loggers or (quick and dirty) monkey patch Tortoise's FastAPI contrib module with custom logger like

import logging
from tortoise.contrib import fastapi
fastapi.logging = logging.getLogger('uvicorn')

However it would be more appropriate to use non-root logger for FastAPI integration as it looks to be more FastAPI/Starlette/uvicorn-way.

All 3 comments

There is no error and log come from uvicorn not tortoise.

@long2ice oh okay. I'll make a thread there then. Thanks for the help :)

While it is technically not an error, it is more the issue of the Tortoise's FastAPI integration:

https://github.com/tortoise/tortoise-orm/blob/767cd5d563a36e7795f0331082f6ee51297b661d/tortoise/contrib/fastapi/__init__.py#L93-L96

It uses root logger which creates handler with NOSET level. Neither FastAPI nor uvicorn set up the root logger, but uvicorn logger by default propagates log records to root one repeating the messages.

The workaround is either disable propagation for uvicorn loggers or (quick and dirty) monkey patch Tortoise's FastAPI contrib module with custom logger like

import logging
from tortoise.contrib import fastapi
fastapi.logging = logging.getLogger('uvicorn')

However it would be more appropriate to use non-root logger for FastAPI integration as it looks to be more FastAPI/Starlette/uvicorn-way.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yinkh picture yinkh  路  5Comments

theblackturtle picture theblackturtle  路  5Comments

usernein picture usernein  路  4Comments

andrzejpiasecki picture andrzejpiasecki  路  5Comments

mengyyy picture mengyyy  路  6Comments