a year ago
I am running a Discord bot SSApp on Railway, and when I execute SQL to a Postgres DB, I get this error.
After that, the following error occurs and access to the DB becomes impossible.
psycopg2.InterfaceError: connection already closed
Project ID : 8153aade-2cb8-4e3e-a2c6-36ad01e8f5fd
0 Replies
a year ago
make sure you aren't keeping idle connections open
Regarding psycopg2, I changed the description in requirements.txt from psycopg2-binary to psycopg2 and this error no longer occurs.
Instead, the following error occurs periodically. What is the cause of this?
discord.errors.ConnectionClosed: Shard ID None WebSocket closed with 1000
I think it is a normal implementation, though I use psycopg2.connect to connect when the bot starts and close when the bot exits.
a year ago
you are keeping closed connections around
After changing the Postgres operation from psycopg2 to asyncpg, the first error does not seem to occur now. However, the following errors occur periodically
Traceback (most recent call last):
File "/opt/venv/lib/python3.9/site-packages/discord/client.py", line 659, in connect
await self.ws.poll_event()
File "/opt/venv/lib/python3.9/site-packages/discord/gateway.py", line 646, in poll_event
raise ConnectionClosed(self.socket, shard_id=self.shard_id, code=code) from None
discord.errors.ConnectionClosed: Shard ID None WebSocket closed with 1000
a year ago
make sure you aren't keeping idle connections open
I have tried connecting only when DB operations are required and disconnecting when done, but the error still occurs
hi @たけ , I had that issue under circumstances very similar to yours and this seems to have fixed it:
adding 'NullPool' to the engine: engine = createengine(DATABASEURL, echo=True, future=True, poolpreping=True, poolclass=NullPool)
using a sessionmaker to do all db-related operations inside context managers with session objects instantiated from sessionmaker
hope this helps
Status changed to Solved railway[bot] • about 1 year ago