﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
31240	django.db.utils.InterfaceError in test when creating FileResponse with temporary file	Oskar Persson	nobody	"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
}}}
"	Bug	new	Uncategorized	3.0	Normal				Unreviewed	0	0	0	0	0	0
