Opened 5 years ago

Last modified 5 years ago

#31240 closed Bug

django.db.utils.InterfaceError in test when creating FileResponse with temporary file — at Version 2

Reported by: Oskar Persson Owned by: nobody
Component: HTTP handling Version: 3.0
Severity: Release blocker Keywords:
Cc: Carlton Gibson, Chris Jerdonek, Florian Apolloner Triage Stage: Ready for checkin
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Oskar Persson)

I think I might've found a regression in #30565. When I run the following tests (in their defined order) against Postgres I get the error below.

import tempfile
from django.contrib.auth import get_user_model
from django.http import FileResponse
from django.test import TestCase

User = get_user_model()

class MyTests(TestCase):
    def setUp(self):
        self.user = User.objects.create(username='user')

    def test_first(self):
        with tempfile.TemporaryFile() as f:
            return FileResponse(f)

    def test_second(self):
        pass
Running tests...
----------------------------------------------------------------------
.E
======================================================================
ERROR [0.003s]: test_second (responses.test_fileresponse.MyTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tests/django/django/db/backends/base/base.py", line 238, in _cursor
    return self._prepare_cursor(self.create_cursor(name))
  File "/tests/django/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/tests/django/django/db/backends/postgresql/base.py", line 231, in create_cursor
    cursor = self.connection.cursor()
psycopg2.InterfaceError: connection already closed

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/tests/django/tests/responses/test_fileresponse.py", line 19, in setUp
    self.user = User.objects.create(username='user')
  File "/tests/django/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/tests/django/django/db/models/query.py", line 433, in create
    obj.save(force_insert=True, using=self.db)
  File "/tests/django/django/contrib/auth/base_user.py", line 66, in save
    super().save(*args, **kwargs)
  File "/tests/django/django/db/models/base.py", line 746, in save
    force_update=force_update, update_fields=update_fields)
  File "/tests/django/django/db/models/base.py", line 784, in save_base
    force_update, using, update_fields,
  File "/tests/django/django/db/models/base.py", line 887, in _save_table
    results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
  File "/tests/django/django/db/models/base.py", line 926, in _do_insert
    using=using, raw=raw,
  File "/tests/django/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/tests/django/django/db/models/query.py", line 1204, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "/tests/django/django/db/models/sql/compiler.py", line 1382, in execute_sql
    with self.connection.cursor() as cursor:
  File "/tests/django/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/tests/django/django/db/backends/base/base.py", line 260, in cursor
    return self._cursor()
  File "/tests/django/django/db/backends/base/base.py", line 238, in _cursor
    return self._prepare_cursor(self.create_cursor(name))
  File "/tests/django/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/tests/django/django/db/backends/base/base.py", line 238, in _cursor
    return self._prepare_cursor(self.create_cursor(name))
  File "/tests/django/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/tests/django/django/db/backends/postgresql/base.py", line 231, in create_cursor
    cursor = self.connection.cursor()
django.db.utils.InterfaceError: connection already closed

Change History (2)

comment:1 by Mariusz Felisiak, 5 years ago

Oskar, Can you check the ticket number? #30365 is probably not the right one.

comment:2 by Oskar Persson, 5 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top