Frequent connection closing for python app connection to postgres via wireguard (on windows)

I’m working on a python app which talks to postgres. I’m trying to run it locally to test out some of my changes, but connection to the production DB. I’ve done this in the past using Wireguard, and it’s worked fine. Lately it’s been giving me a lot of issues.

Specifically, I’ll run the app, which connects to the postgress app, and it’ll fail with something like this

  File "C:\Users\moowi\Documents\GitHub\bpl-backend\.venv\lib\site-packages\sqlalchemy\dialects\postgresql\", line 388, in _prepare_and_execute
    prepared_stmt, attributes = await adapt_connection._prepare(
  File "C:\Users\moowi\Documents\GitHub\bpl-backend\.venv\lib\site-packages\sqlalchemy\dialects\postgresql\", line 630, in _prepare
    prepared_stmt = await self._connection.prepare(operation)
  File "C:\Users\moowi\Documents\GitHub\bpl-backend\.venv\lib\site-packages\asyncpg\", line 548, in prepare
    return await self._prepare(
  File "C:\Users\moowi\Documents\GitHub\bpl-backend\.venv\lib\site-packages\asyncpg\", line 564, in _prepare
    stmt = await self._get_statement(
  File "C:\Users\moowi\Documents\GitHub\bpl-backend\.venv\lib\site-packages\asyncpg\", line 408, in _get_statement
    types, intro_stmt = await self._introspect_types(
  File "C:\Users\moowi\Documents\GitHub\bpl-backend\.venv\lib\site-packages\asyncpg\", line 454, in _introspect_types
    return await self.__execute(
  File "C:\Users\moowi\Documents\GitHub\bpl-backend\.venv\lib\site-packages\asyncpg\", line 1664, in __execute
    return await self._do_execute(
  File "C:\Users\moowi\Documents\GitHub\bpl-backend\.venv\lib\site-packages\asyncpg\", line 1711, in _do_execute
    result = await executor(stmt, None)
  File "asyncpg\protocol\protocol.pyx", line 201, in bind_execute
asyncpg.exceptions.ConnectionDoesNotExistError: connection was closed in the middle of operation

(full exception at Invalidate connection <sqlalchemy.dialects.postgresql.asyncpg.AsyncAdapt_asyncpg -

This seems to mostly just happen when I’m doing something which takes a few seconds, and runs a bunch of concurrent queries. I can run something which basically only does one query (maybe a handful but I think just one, I’d need to double check) and it seems to work fine, although it very rarely fails with the connection does not exist error.

Is there some setting I can change somewhere that can fix this? How can I debug this issue?

I fiddled around some more. I think the problem was that my postgres app was running on a shared CPU with 256 MB of RAM. I scaled it up to 2 GB of ram, and the connection stopped being dropped.

I thought about this because I used Wireshark to inspect the traffic my app was sending to the postgres server, and noticed that the server was saying that some processes crashed. I then figured it might be due to the small instance size and upscaled it, and things seemed to fix itself.