Open malikovss opened 3 years ago
@long2ice can you help me please?
I have the same issue. @long2ice, can you please check?
I have the same issue. postgresql 13.4, python 3.9.7, aerich 0.5.8
I found a temporary workaround. You can simply uninstall uvloop.
But, I think this problem must be fixed.
I've tried different versions of uvloop, but it didn't solve the problem. The problem appeared even when I installed uvloop as a part of tortoise-orm, by using "[accel]" extra flag in the installation command.
I'm not having this with aerich migrations. However it does happen when using aerich queries inside a background_task from fastapi. Uninstalling uvloop (not putting it in requirements.txt) does not help however :(.
Everything runs fine in the background_task but then the first following regular request throws this error:
INFO: 172.21.0.1:45402 - "GET /sip_profiles HTTP/1.1" 500 Internal Server Error
fit-backend-backend-1 | ERROR: Exception in ASGI application
fit-backend-backend-1 | Traceback (most recent call last):
fit-backend-backend-1 | File "/usr/local/lib/python3.9/site-packages/uvicorn/protocols/http/httptools_impl.py", line 435, in run_asgi
fit-backend-backend-1 | result = await app( # type: ignore[func-returns-value]
fit-backend-backend-1 | File "/usr/local/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
fit-backend-backend-1 | return await self.app(scope, receive, send)
fit-backend-backend-1 | File "/usr/local/lib/python3.9/site-packages/fastapi/applications.py", line 208, in __call__
fit-backend-backend-1 | await super().__call__(scope, receive, send)
fit-backend-backend-1 | File "/usr/local/lib/python3.9/site-packages/starlette/applications.py", line 112, in __call__
fit-backend-backend-1 | await self.middleware_stack(scope, receive, send)
fit-backend-backend-1 | File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 181, in __call__
fit-backend-backend-1 | raise exc from None
fit-backend-backend-1 | File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 159, in __call__
fit-backend-backend-1 | await self.app(scope, receive, _send)
fit-backend-backend-1 | File "/usr/local/lib/python3.9/site-packages/starlette/middleware/cors.py", line 86, in __call__
fit-backend-backend-1 | await self.simple_response(scope, receive, send, request_headers=headers)
fit-backend-backend-1 | File "/usr/local/lib/python3.9/site-packages/starlette/middleware/cors.py", line 142, in simple_response
fit-backend-backend-1 | await self.app(scope, receive, send)
fit-backend-backend-1 | File "/usr/local/lib/python3.9/site-packages/starlette/exceptions.py", line 82, in __call__
fit-backend-backend-1 | raise exc from None
fit-backend-backend-1 | File "/usr/local/lib/python3.9/site-packages/starlette/exceptions.py", line 71, in __call__
fit-backend-backend-1 | await self.app(scope, receive, sender)
fit-backend-backend-1 | File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 580, in __call__
fit-backend-backend-1 | await route.handle(scope, receive, send)
fit-backend-backend-1 | File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 241, in handle
fit-backend-backend-1 | await self.app(scope, receive, send)
fit-backend-backend-1 | File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 52, in app
fit-backend-backend-1 | response = await func(request)
fit-backend-backend-1 | File "/usr/local/lib/python3.9/site-packages/fastapi/routing.py", line 209, in app
fit-backend-backend-1 | solved_result = await solve_dependencies(
fit-backend-backend-1 | File "/usr/local/lib/python3.9/site-packages/fastapi/dependencies/utils.py", line 550, in solve_dependencies
fit-backend-backend-1 | solved = await call(**sub_values)
fit-backend-backend-1 | File "/app/src/auth/jwthandler.py", line 94, in get_current_user
fit-backend-backend-1 | user = await UserOutSchema.from_queryset_single(
fit-backend-backend-1 | File "/usr/local/lib/python3.9/site-packages/tortoise/contrib/pydantic/base.py", line 100, in from_queryset_single
fit-backend-backend-1 | return cls.from_orm(await queryset.prefetch_related(*fetch_fields))
fit-backend-backend-1 | File "/usr/local/lib/python3.9/site-packages/tortoise/queryset.py", line 1008, in _execute
fit-backend-backend-1 | instance_list = await self._db.executor_class(
fit-backend-backend-1 | File "/usr/local/lib/python3.9/site-packages/tortoise/backends/base/executor.py", line 131, in execute_select
fit-backend-backend-1 | _, raw_results = await self.db.execute_query(query.get_sql())
fit-backend-backend-1 | File "/usr/local/lib/python3.9/site-packages/tortoise/backends/base_postgres/client.py", line 34, in _translate_exceptions
fit-backend-backend-1 | return await self._translate_exceptions(func, *args, **kwargs)
fit-backend-backend-1 | File "/usr/local/lib/python3.9/site-packages/tortoise/backends/asyncpg/client.py", line 82, in _translate_exceptions
fit-backend-backend-1 | return await func(self, *args, **kwargs)
fit-backend-backend-1 | File "/usr/local/lib/python3.9/site-packages/tortoise/backends/asyncpg/client.py", line 129, in execute_query
fit-backend-backend-1 | async with self.acquire_connection() as connection:
fit-backend-backend-1 | File "/usr/local/lib/python3.9/site-packages/tortoise/backends/base/client.py", line 328, in __aenter__
fit-backend-backend-1 | self.connection = await self.pool.acquire()
fit-backend-backend-1 | File "/usr/local/lib/python3.9/site-packages/asyncpg/pool.py", line 621, in _acquire
fit-backend-backend-1 | raise exceptions.InterfaceError('pool is closing')
fit-backend-backend-1 | asyncpg.exceptions._base.InterfaceError: pool is closing
After that requests return to normal. Is there some way to fix this by putting something like asyncpg close explicitly somewhere?
Ok found a workaround. For anyone also bumping into this. Don't use .save() on a model as this gets you into this pool is closing exception happening.
batch = await Batch.get(id=batch_id)
sip_creation = await BatchStatus.get(status="sip_creation")
batch.status_id = sip_creation.id
await batch.save()
Replaced with new code:
sip_creation = await BatchStatus.get(status="sip_creation")
await Batch.filter(id=batch_id).update(
status_id=sip_creation.id
)
Both make an update in the database however the second one works perfectly in a background task. The first one does not. Also we need to use multiple uvicorn/gunicorn workers.
Nevermind. When the task is larger, it still happens :(
Basically whenever a background task is still using the database while a new request comes in we get this:
self.connection = await self.pool.acquire()
File "/usr/local/lib/python3.9/site-packages/asyncpg/pool.py", line 621, in _acquire
raise exceptions.InterfaceError('pool is closing')
asyncpg.exceptions._base.InterfaceError: pool is closing
I'm facing error with aerich. aerich==0.5.7 asyncpg==0.24.0
Another error when aerich upgrade