pytest-dev / pytest-django

A Django plugin for pytest.
https://pytest-django.readthedocs.io/
Other
1.39k stars 342 forks source link

Pytest-django , factory_boy InterfaceError: connection already closed #835

Closed PyB1l closed 4 years ago

PyB1l commented 4 years ago

Failed tests occur when running multiple tests that use @django_db or / and @django_db(transaction=True) complaining that connection is already closed.

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <django.db.backends.postgresql.base.DatabaseWrapper object at 0x7f082a459a90>
name = None

    @async_unsafe
    def create_cursor(self, name=None):
        if name:
            # In autocommit mode, the cursor will be used outside of a
            # transaction, hence use a holdable cursor.
            cursor = self.connection.cursor(name, scrollable=False, withhold=self.connection.autocommit)
        else:
>           cursor = self.connection.cursor()
E           django.db.utils.InterfaceError: connection already closed

On the other sode,running the single test that failed alone, works as expected. Affected version are:

Python 3.7 Django 3.0 pytest 5.4.1
pytest-cov 2.8.1
pytest-django 3.9.0
pytest-factoryboy 2.0.3
pytest-forked 1.1.3

PyB1l commented 4 years ago

I need to mention that not all tests fail. It ONLY breaks a random test with django.db.utils.InterfaceError: connection already closed 8 of 10 times. In some rare cases it passes all tests. Could it be something about Python3.7+ specific issue? I never had any similar issues in python 3.6

bluetech commented 4 years ago

Hi @PyB1l,

Given that you are aware of #824 and it didn't fix the issue, if you can create a repo with a minimal reproduction (i.e. least amount of dependencies and code), that would be helpful.

PyB1l commented 4 years ago

I am inspecting the test suite in Django 3.0.5 right now and it seems to has no issue, there, with the latest plugins. I will do more research and i will come back with results before i create a test project.

PyB1l commented 4 years ago

BTW the issue i run into was detected in Django 3.0.2.

PyB1l commented 4 years ago

Ok it definitely seems to be ok with Django, pytest, pytest-django, factory_boy latest version. I will close this for now and if i find any results i will come back.