|
5 | 5 |
|
6 | 6 | import functools |
7 | 7 | import types |
| 8 | +import weakref |
8 | 9 | import zlib |
9 | 10 | from typing import TYPE_CHECKING, Any, Collection, Mapping |
10 | 11 |
|
@@ -128,19 +129,25 @@ def build_response( # type: ignore[override] |
128 | 129 | response._fp = CallbackFileWrapper( # type: ignore[assignment] |
129 | 130 | response._fp, # type: ignore[arg-type] |
130 | 131 | functools.partial( |
131 | | - self.controller.cache_response, request, response |
| 132 | + self.controller.cache_response, request, weakref.ref(response) |
132 | 133 | ), |
133 | 134 | ) |
134 | 135 | if response.chunked: |
135 | | - super_update_chunk_length = response._update_chunk_length |
| 136 | + super_update_chunk_length = response.__class__._update_chunk_length |
136 | 137 |
|
137 | | - def _update_chunk_length(self: HTTPResponse) -> None: |
138 | | - super_update_chunk_length() |
| 138 | + def _update_chunk_length( |
| 139 | + weak_self: weakref.ReferenceType[HTTPResponse], |
| 140 | + ) -> None: |
| 141 | + self = weak_self() |
| 142 | + if self is None: |
| 143 | + return |
| 144 | + |
| 145 | + super_update_chunk_length(self) |
139 | 146 | if self.chunk_left == 0: |
140 | 147 | self._fp._close() # type: ignore[union-attr] |
141 | 148 |
|
142 | | - response._update_chunk_length = types.MethodType( # type: ignore[method-assign] |
143 | | - _update_chunk_length, response |
| 149 | + response._update_chunk_length = functools.partial( # type: ignore[method-assign] |
| 150 | + _update_chunk_length, weakref.ref(response) |
144 | 151 | ) |
145 | 152 |
|
146 | 153 | resp: Response = super().build_response(request, response) |
|
0 commit comments