question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"InterfaceError: connection already closed" being raised when a test is run

See original GitHub issue

Hello,

First off, congratulations on this awesome app. I am using Python 2.7, Django 1.9 and Postgresql 9.3.

Settings dict for django-q is as follows:

Q_CLUSTER = {
    'name': 'pulsecheck',
    'workers': 2,
    'recycle': 500,
    'compress': True,
    'save_limit': 250,
    'label': 'Task Management',
    'redis': {
        'host': '127.0.0.1',
        'port': 6379,
        'db': 0, }
}

This is what I have in tests.py:

Conf.SYNC = True

...

def test_stuff(self):
    ...
    async('some_stuff', var1, var2)
    ...
    assert ...

When tests are run, this is what I get:

Creating test database for alias 'default'...
16:53:03 [Q] INFO MainProcess ready for work at 30995
16:53:03 [Q] INFO MainProcess processing [stairway-iowa-solar-lactose]
16:53:03 [Q] INFO MainProcess stopped doing work
16:53:03 [Q] INFO MainProcess monitoring at 30995
16:53:03 [Q] ERROR connection already closed
16:53:03 [Q] ERROR Failed [stairway-iowa-solar-lactose] - connection already closed
16:53:03 [Q] INFO MainProcess stopped monitoring results
E
======================================================================
ERROR: test_activation_successful (core.tests.AuthTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/ozgurisil/dev/pulsecheck_env/src/pulsecheck/core/tests.py", line 358, in test_activation_successful
    user = User.objects.get(username='testuser')
  File "/home/ozgurisil/dev/pulsecheck_env/local/lib/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/ozgurisil/dev/pulsecheck_env/local/lib/python2.7/site-packages/django/db/models/query.py", line 381, in get
    num = len(clone)
  File "/home/ozgurisil/dev/pulsecheck_env/local/lib/python2.7/site-packages/django/db/models/query.py", line 240, in __len__
    self._fetch_all()
  File "/home/ozgurisil/dev/pulsecheck_env/local/lib/python2.7/site-packages/django/db/models/query.py", line 1074, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/home/ozgurisil/dev/pulsecheck_env/local/lib/python2.7/site-packages/django/db/models/query.py", line 52, in __iter__
    results = compiler.execute_sql()
  File "/home/ozgurisil/dev/pulsecheck_env/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 846, in execute_sql
    cursor = self.connection.cursor()
  File "/home/ozgurisil/dev/pulsecheck_env/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 233, in cursor
    cursor = self.make_cursor(self._cursor())
  File "/home/ozgurisil/dev/pulsecheck_env/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 206, in _cursor
    return self.create_cursor()
  File "/home/ozgurisil/dev/pulsecheck_env/local/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/ozgurisil/dev/pulsecheck_env/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 206, in _cursor
    return self.create_cursor()
  File "/home/ozgurisil/dev/pulsecheck_env/local/lib/python2.7/site-packages/django/db/backends/postgresql/base.py", line 210, in create_cursor
    cursor = self.connection.cursor()
InterfaceError: connection already closed

----------------------------------------------------------------------
Ran 1 test in 1.382s

PS: The error still occurs if async(..., sync=True) format is used.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

24reactions
khamaileoncommented, Jul 13, 2016

I think I’ve come up with a solution. I’m now using TransactionTestCase class instead of TestCase. This keeps connection alive. Note that I’m using Django Q and not Celery.

12reactions
avalanchycommented, Feb 17, 2017

My tests passes with TransactionTestCase (or in my case pytest.mark.django_db(transaction=True)) with celery_worker fixture. Looks like connection is not dropped anymore.

Read more comments on GitHub >

github_iconTop Results From Across the Web

django.db.utils.InterfaceError: connection already closed ...
It appears to be a bug in pytest-django . ... Check when the connection get closed and by which test (run them individually...
Read more >
Flakey tests and django.db.utils.InterfaceError: connection ...
InterfaceError : connection already closed ... Here is the test module suite that I'm running to test these models, ... raise value.with_traceback(tb)
Read more >
"django.db.utils.InterfaceError: connection already closed" in ...
I found a weird bug in Django that causes database errors when running unittesets, this happens when doing nothing special/funky (see small included ......
Read more >
django.db.utils.InterfaceError: connection already closed
3) An AWS load balancer sends traffic to the Ubuntu instance(k8s cluster), which is handled by Nginx, which forwards on to Django (4.0.3)...
Read more >
1994923 – Internal server error when accessing kickstart ...
InterfaceError : connection already closed Aug 18 09:42:49 ... observed in https://bugzilla.redhat.com/show_bug.cgi?id=1994923#c5 I was able to browse the ks ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found