Skip to content

Commit 646f92f

Browse files
committed
Delete HTTPConnection._close_callback in between requests.
Previously the close callback would cause a reference to the previous RequestHandler to be retained while waiting for the next request.
1 parent e00e4c6 commit 646f92f

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

tornado/httpserver.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,21 @@ def __init__(self, stream, address, request_callback, no_keep_alive=False,
182182
self.protocol = protocol
183183
self._request = None
184184
self._request_finished = False
185+
self._write_callback = None
186+
self._close_callback = None
185187
# Save stack context here, outside of any request. This keeps
186188
# contexts from one request from leaking into the next.
187189
self._header_callback = stack_context.wrap(self._on_headers)
188190
self.stream.read_until(b"\r\n\r\n", self._header_callback)
191+
192+
def _clear_callbacks(self):
193+
"""Clears the per-request callbacks.
194+
195+
This is run in between requests to allow the previous handler
196+
to be garbage collected (and prevent spurious close callbacks),
197+
and when the connection is closed (to break up cycles and
198+
facilitate garbage collection in cpython).
199+
"""
189200
self._write_callback = None
190201
self._close_callback = None
191202

@@ -205,13 +216,15 @@ def _on_connection_close(self):
205216
self._close_callback = None
206217
callback()
207218
# Delete any unfinished callbacks to break up reference cycles.
208-
self._write_callback = None
219+
self._header_callback = None
220+
self._clear_callbacks()
209221

210222
def close(self):
211223
self.stream.close()
212224
# Remove this reference to self, which would otherwise cause a
213225
# cycle and delay garbage collection of this connection.
214226
self._header_callback = None
227+
self._clear_callbacks()
215228

216229
def write(self, chunk, callback=None):
217230
"""Writes a chunk of output to the stream."""
@@ -258,6 +271,7 @@ def _finish_request(self):
258271
disconnect = True
259272
self._request = None
260273
self._request_finished = False
274+
self._clear_callbacks()
261275
if disconnect:
262276
self.close()
263277
return

0 commit comments

Comments
 (0)