#37256 new Bug

StreamingHttpResponse does not call close when streaming iteration stops

Reported by: Enrico Zini Owned by:
Component: Uncategorized Version: 6.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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'...

Attachments (1)

reproducer.tar.gz (2.7 KB ) - added by Enrico Zini 68 minutes ago.

Download all attachments as: .zip

Change History (1)

by Enrico Zini, 68 minutes ago

Attachment: reproducer.tar.gz added
Note: See TracTickets for help on using tickets.
Back to Top