Skip to content

Commit

Permalink
Fix deadlock for Sanic apps with generate_schema=True and > 1 worke…
Browse files Browse the repository at this point in the history
…rs (#1696)

* Fix deadlock for Sanic apps with `generate_schema=True`

* Add changelog entry

* Fix linter errors

* Run `make style`

* Remove unused import
  • Loading branch information
Pirulax authored Aug 17, 2024
1 parent 1ca1cad commit 60c225e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Changelog
Fixed
^^^^^
- Fix bug in `pydantic_model_creator` when a foreign key is not included in `include` param. (#1430)
- Fix bug in `contrib.sanic.register_tortoise` causing a deadlock when using asyncpg and > 1 workers (#1696)

0.21.5 <../0.21.5>`_ - 2024-07-18
------
Expand Down
2 changes: 1 addition & 1 deletion examples/fastapi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from typing import AsyncGenerator

from fastapi import FastAPI
from routers import router as users_router

from examples.fastapi.config import register_orm
from routers import router as users_router
from tortoise import Tortoise, generate_config
from tortoise.contrib.fastapi import RegisterTortoise

Expand Down
18 changes: 14 additions & 4 deletions tortoise/contrib/sanic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,24 @@ def register_tortoise(
For any configuration error
"""

@app.listener("before_server_start")
async def init_orm(app, loop): # pylint: disable=W0612
async def tortoise_init() -> None:
await Tortoise.init(config=config, config_file=config_file, db_url=db_url, modules=modules)
logger.info("Tortoise-ORM started, %s, %s", connections._get_storage(), Tortoise.apps)
if generate_schemas:
logger.info(
"Tortoise-ORM started, %s, %s", connections._get_storage(), Tortoise.apps
) # pylint: disable=W0212

if generate_schemas:

@app.listener("main_process_start")
async def init_orm_main(app, loop): # pylint: disable=W0612
await tortoise_init()
logger.info("Tortoise-ORM generating schema")
await Tortoise.generate_schemas()

@app.listener("before_server_start")
async def init_orm(app, loop): # pylint: disable=W0612
await tortoise_init()

@app.listener("after_server_stop")
async def close_orm(app, loop): # pylint: disable=W0612
await connections.close_all()
Expand Down

0 comments on commit 60c225e

Please sign in to comment.