unable to perform operation on <TCPTransport closed=True reading=False 0x55709d84a6c0> for sqlalchemy

105 views Asked by At

I'm encountering an error here about unable to perform operation on <TCPTransport closed=True reading=False 0x55709d84a6c0>.

This error normally doesn't occur in my project. it only occurs when server has not been active for a period of 8-12 hours. I'm using sqlalchemy orm with AsyncSession and mysql database and gunicorn with 1 uvicorn worker and fastapi .

error log:

ERROR 2023-08-17 11:02:32.933 - sqlalchemy.pool.base:_finalize_fairy - Exception during reset or similar

Traceback (most recent call last):


File "/src/.venv/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 1816, in close

self._close_impl(invalidate=False)

│ └ <function Session._close_impl at 0x7f1214019af0>

└ <sqlalchemy.orm.session.Session object at 0x7f120034f940>

File "/src/.venv/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 1858, in _close_impl

transaction.close(invalidate)

│ │ └ False

│ └ <function SessionTransaction.close at 0x7f12140175e0>

└ <sqlalchemy.orm.session.SessionTransaction object at 0x7f120034f910>

File "/src/.venv/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 928, in close

connection.close()

│ └ <function Connection.close at 0x7f12140f5ca0>

└ <sqlalchemy.future.engine.Connection object at 0x7f120034f160>

File "/src/.venv/lib/python3.9/site-packages/sqlalchemy/future/engine.py", line 246, in close

super(Connection, self).close()

│ └ <sqlalchemy.future.engine.Connection object at 0x7f120034f160>

└ <class 'sqlalchemy.future.engine.Connection'>

File "/src/.venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1251, in close

conn.close()

│ └ <function _ConnectionFairy.close at 0x7f12144edaf0>

└ <sqlalchemy.pool.base._ConnectionFairy object at 0x7f120034f190>

File "/src/.venv/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 1166, in close

self._checkin()

│ └ <function _ConnectionFairy._checkin at 0x7f12144ed550>

└ <sqlalchemy.pool.base._ConnectionFairy object at 0x7f120034f190>

File "/src/.venv/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 1008, in _checkin

_finalize_fairy(

└ <function _finalize_fairy at 0x7f12144d7670>

> File "/src/.venv/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 763, in _finalize_fairy

fairy._reset(pool, transaction_was_reset)

│ │ │ └ False

│ │ └ <sqlalchemy.pool.impl.AsyncAdaptedQueuePool object at 0x7f12133e1ac0>

│ └ <function _ConnectionFairy._reset at 0x7f12144ed5e0>

└ <sqlalchemy.pool.base._ConnectionFairy object at 0x7f120034f190>

File "/src/.venv/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 1038, in _reset

pool._dialect.do_rollback(self)

│ │ │ └ <sqlalchemy.pool.base._ConnectionFairy object at 0x7f120034f190>

│ │ └ <function DefaultDialect.do_rollback at 0x7f12141f6af0>

│ └ <sqlalchemy.dialects.mysql.aiomysql.MySQLDialect_aiomysql object at 0x7f12133d1a30>

└ <sqlalchemy.pool.impl.AsyncAdaptedQueuePool object at 0x7f12133e1ac0>

File "/src/.venv/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 683, in do_rollback

dbapi_connection.rollback()

└ <sqlalchemy.pool.base._ConnectionFairy object at 0x7f120034f190>

File "/src/.venv/lib/python3.9/site-packages/sqlalchemy/dialects/mysql/aiomysql.py", line 205, in rollback

self.await_(self._connection.rollback())

│ │ │ └ <member '_connection' of 'AsyncAdapt_aiomysql_connection' objects>

│ │ └ <AdaptedConnection <aiomysql.connection.Connection object at 0x7f1200440550>>

│ └ <staticmethod object at 0x7f121335fd30>

└ <AdaptedConnection <aiomysql.connection.Connection object at 0x7f1200440550>>

File "/src/.venv/lib/python3.9/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 68, in await_only

return current.driver.switch(awaitable)

│ │ │ └ <coroutine object Connection.rollback at 0x7f11e6e824c0>

│ │ └ <method 'switch' of 'greenlet.greenlet' objects>

│ └ <greenlet.greenlet object at 0x7f1214cb8d00 (otid=0x7f1214efe2a0) suspended active started main>

└ <_AsyncIoGreenlet object at 0x7f1200467040 (otid=0x7f1214efe2a0) current active started>

File "/src/.venv/lib/python3.9/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 121, in greenlet_spawn

value = await result

└ <coroutine object Connection.rollback at 0x7f11e6e824c0>

File "/src/.venv/lib/python3.9/site-packages/aiomysql/connection.py", line 357, in rollback

await self._execute_command(COMMAND.COM_QUERY, "ROLLBACK")

│ │ │ └ 3

│ │ └ <module 'pymysql.constants.COMMAND' from '/src/.venv/lib/python3.9/site-packages/pymysql/constants/COMMAND.py'>

│ └ <function Connection._execute_command at 0x7f12133035e0>

└ <aiomysql.connection.Connection object at 0x7f1200440550>

File "/src/.venv/lib/python3.9/site-packages/aiomysql/connection.py", line 661, in _execute_command

self._write_bytes(prelude + sql[:chunk_size - 1])

│ │ │ │ └ 9

│ │ │ └ b'ROLLBACK'

│ │ └ b'\t\x00\x00\x00\x03'

│ └ <function Connection._write_bytes at 0x7f1213303310>

└ <aiomysql.connection.Connection object at 0x7f1200440550>

File "/src/.venv/lib/python3.9/site-packages/aiomysql/connection.py", line 606, in _write_bytes

return self._writer.write(data)

│ │ │ └ b'\t\x00\x00\x00\x03ROLLBACK'

│ │ └ <function StreamWriter.write at 0x7f121716dc10>

│ └ <StreamWriter transport=<TCPTransport closed=True reading=False 0x55709d84a6c0> reader=<StreamReader 141 bytes eof exception=...

└ <aiomysql.connection.Connection object at 0x7f1200440550>

File "/usr/local/lib/python3.9/asyncio/streams.py", line 341, in write

self._transport.write(data)

│ │ │ └ b'\t\x00\x00\x00\x03ROLLBACK'

│ │ └ <method 'write' of 'uvloop.loop.UVStream' objects>

│ └ <TCPTransport closed=True reading=False 0x55709d84a6c0>

└ <StreamWriter transport=<TCPTransport closed=True reading=False 0x55709d84a6c0> reader=<StreamReader 141 bytes eof exception=...

File "uvloop/handles/stream.pyx", line 674, in uvloop.loop.UVStream.write

self._ensure_alive()

File "uvloop/handles/handle.pyx", line 159, in uvloop.loop.UVHandle._ensure_alive

raise RuntimeError(


RuntimeError: unable to perform operation on <TCPTransport closed=True reading=False 0x55709d84a6c0>; the handler is closed

sqlalchemy version: SQLAlchemy 1.4.49

aiomysql version: aiomysql 0.0.22

pymysql version: pymysql 0.9.3

I even activated pool_pre_ping config to check stale sessions but it resulted in some major errors with some queries that never occurred before. so I deactivated it.

now i'm stuck. I don't know what's the reason of this error.

any insight to help on this issue?

0

There are 0 answers