jneight / django-db-geventpool

Another DB pool using gevent
Apache License 2.0
168 stars 29 forks source link

Psycopg2.ProgrammingError: set_session cannot be used inside a transaction #80

Open augustolima opened 5 months ago

augustolima commented 5 months ago

Hello guys...

I'm currently working on a legacy codebase with the following configuration:

python==3.8.17
django==3.1.14
django-db-geventpool==4.0.1
psycopg2==2.9.3
psycogreen==1.0.2

And, since last week's deployment, we have been experiencing the following error, randomly, in all of our endpoints: Psycopg2.ProgrammingError: set_session cannot be used inside a transaction

When checking the bug report, I can see that it breaks in the following piece of code:

# file: django/db/backends/postgresql/base.py in _set_autocommit at line 277
def _set_autocommit(self, autocommit):
        with self.wrap_database_errors:
            self.connection.autocommit = autocommit

# autocommit is True
# self = <django_db_geventpool.backends.postgresql_psycopg2.base.DatabaseWrapper object at 0x7fa249656550>

Could the issue be related to this library/subdependency or Django subdependency?

EDIT 1: We rolled back to the previous version, and the error seems to persist.

jneight commented 5 months ago

Hello, can you paste your database settings?, it's the first time I see this error and there are some projects using this package, so I'm not sure if geventpool is the root cause.

Also, try using latest, if posible, psycopg2==2.9.9 and django-db-geventpool==4.0.2

augustolima commented 5 months ago

Hello, @jneight. Thanks for answering.

Here's the database settings:

DATABASE_URL = os.environ.get("DATABASE_URL")
DATABASES = {
    "default": dj_database_url.parse(
        DATABASE_URL,
        engine="django_db_geventpool.backends.postgresql_psycopg2",
    ),
}
DATABASES["default"]["ATOMIC_REQUESTS"] = True
DATABASES["default"]["OPTIONS"] = {"MAX_CONNS": 60}

Regarding updating to psycopg2 and django-db-geventpool, both have been updated and the error persists.

We have two tasks running at the same time on AWS ECS.

jneight commented 1 month ago

I think the problem is with ATOMIC_REQUEST=True, can you try with the latest release and with that setting True and False?