﻿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
37256	StreamingHttpResponse does not call close when streaming iteration stops	Enrico Zini		"The attached django project, when running {{{./manage.py test reproducer}}}, gives:
{{{
$ ./manage.py test reproducer
Found 1 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
cleaned up
F
======================================================================
FAIL: test_cleanup (reproducer.tests.Test.test_cleanup)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ""/usr/lib/python3/dist-packages/asgiref/sync.py"", line 254, in __call__
    return call_result.result()
           ~~~~~~~~~~~~~~~~~~^^
  File ""/usr/lib/python3.13/concurrent/futures/_base.py"", line 449, in result
    return self.__get_result()
           ~~~~~~~~~~~~~~~~~^^
  File ""/usr/lib/python3.13/concurrent/futures/_base.py"", line 401, in __get_result
    raise self._exception
  File ""/usr/lib/python3/dist-packages/asgiref/sync.py"", line 331, in main_wrap
    result = await self.awaitable(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ""/home/enrico/lavori/freexian/django-shutdown-iterators/trixie/reproducer/reproducer/tests.py"", line 13, in test_cleanup
    self.assertTrue(views.cleaned_up)
    ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
AssertionError: False is not true

----------------------------------------------------------------------
Ran 1 test in 0.011s

FAILED (failures=1)
Destroying test database for alias 'default'...
}}}

You can see that cleanup is called, but after the test method has completed.

Adding {{{contextlib.aclosing}}} to {{{django.http.response.StreamingHttpResponse.streaming_content}}} and {{{django.test.client.aclosing_iterator_wrapper}}} gives deterministic cleanup:

{{{
$ ./manage.py test reproducer
Found 1 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
cleaned up
.
----------------------------------------------------------------------
Ran 1 test in 0.009s

OK
Destroying test database for alias 'default'...
}}}"	Bug	new	Uncategorized	6.1	Normal				Unreviewed	0	0	0	0	0	0
