pytest-dev / pytest-dev/pytest-django

Brutal connection cleanup in teardown of django_db_setup

Open
#429 14 comments 13 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.5k
Forks
367
PR merge metrics
No merged PRs in 30d

Description

We are having a few projects that suffer from hanging connections to the DB, can be from external processes or threads that holds an connection to the database, and causes tests to fail from time to time.

Below is the fixture we have in all our conftests.py to make the cleanup more brutal, to assure that there are no hanging connections around that can cause the tests to fail...

Are we alone having this problems? It is not a big deal having this specific extension of django_db_setup, but if there are plenty of people doing similar hacks, it should be considered :)

@pytest.yield_fixture(scope='session')
def django_db_setup(django_db_setup, django_db_blocker):
    """
    Fixture that will clean up remaining connections, that might be hanging
    from threads or external processes. Extending pytest_django.django_db_setup
    """

    yield

    with django_db_blocker.unblock():
        from django.db import connections

        conn = connections['default']
        cursor = conn.cursor()
        cursor.execute("""SELECT * FROM pg_stat_activity;""")
        print('current connections')
        for r in cursor.fetchall():
            print(r)

        terminate_sql = """
            SELECT pg_terminate_backend(pg_stat_activity.pid)
            FROM pg_stat_activity
            WHERE pg_stat_activity.datname = '%s'
                AND pid <> pg_backend_pid();
        """ % conn.settings_dict['NAME']
        print('Terminate SQL: ', terminate_sql)
        cursor.execute(terminate_sql)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the django_db_setup entry point and the conftests.py override shown in the report; review how session teardown handles database connections. Determine whether PostgreSQL-specific cleanup belongs in the plugin and define an agreed, supported behavior for cleaning up hanging connections.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, postgresql, python
Domain
databases, testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.