Using psycopg connection pooling ("pool": True) causes too eagerly discarded connections

I’ve been trying out the somewhat new feature of django to use psycopg connection pooling (see
Databases | Django documentation | Django). We’ve had occasional (fixable) hickups with connections to our postgres, like e.g. “sorry too many clients already” (see OperationalError: sorry, too many clients already), so I was hoping this would put the whole topic on more solid ground.

However, we are encountering new errors. Sometimes connections are discarded with a warning, but with no specific reason given. I couldn’t find any default config or similar which would explain that behavior. Is there anything specific I need to address to make this work?

In my test case it’s just a single django-q2 worker which has this issue every minute or so with every tenth task

Example log:

WARNING 2024-12-31 15:44:55,185 pool 16 140085573299072 discarding closed connection: <psycopg.Connection [BAD] at 0x7f682e22aea0>
ERROR 2024-12-31 15:44:55,186 monitor 16 140085573299072 Could not save task result
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/django/db/backends/utils.py", line 105, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/psycopg/cursor.py", line 97, in execute
    raise ex.with_traceback(None)
psycopg.DatabaseError: insufficient data in "D" message
lost synchronization with server: got message type "u", length 1513240693