Django PostgreSQL used session on Github Actions test error

If you’re using the PostgreSQL service on Github Actions as a backend database for your Django tests, you might encounter the following error on any particular test:

django.db.utils.OperationalError: database "test_name" is being accessed by other users
DETAIL:  There is 1 other session using the database.

And the following at the end of your test run:

RuntimeError: generator didn't stop after throw()

The problem is that Django reuses the database connection, and while locally my tests were passing, on Github Actions even though the all tests were passing, somehow a connection to the database created by the tests was still open, and I was getting the error above.

The solution is simple, set CONN_MAX_AGE to 0 (or conn_max_age if you use dj_database_url) on your DATABASES settings when running on Github Actions.





One response to “Django PostgreSQL used session on Github Actions test error”

  1. J.J.Mendes Avatar

    Então o criador desse tech blog foi quem criou o “Quem Perturba”?

    Vi agora que o serviço está fora do ar e redirecionando pra cá.

    Se for, valeu enquanto durou! Sucesso nos futuros projetos!