Skip to content

Loading…

Remove timeout callback on errors during connect or request. #684

Merged
merged 1 commit into from

2 participants

@vkosh

Force gc to collect _HTTPConnection object in case of exception (e.g. connection error) without waiting for timeout.
A simple code demonstrates the reference to connection object after connection refused error:
https://gist.github.com/vkosh/4990611
backref

@bdarnell bdarnell merged commit 88e94c8 into tornadoweb:master

1 check passed

Details default The Travis build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 6 additions and 4 deletions.
  1. +6 −4 tornado/simple_httpclient.py
View
10 tornado/simple_httpclient.py
@@ -213,10 +213,13 @@ def _on_timeout(self):
if self.final_callback is not None:
raise HTTPError(599, "Timeout")
- def _on_connect(self):
+ def _remove_timeout(self):
if self._timeout is not None:
self.io_loop.remove_timeout(self._timeout)
self._timeout = None
+
+ def _on_connect(self):
+ self._remove_timeout()
if self.request.request_timeout:
self._timeout = self.io_loop.add_timeout(
self.start_time + self.request.request_timeout,
@@ -290,6 +293,7 @@ def _run_callback(self, response):
def _handle_exception(self, typ, value, tb):
if self.final_callback:
+ self._remove_timeout()
gen_log.warning("uncaught exception", exc_info=(typ, value, tb))
self._run_callback(HTTPResponse(self.request, 599, error=value,
request_time=self.io_loop.time() - self.start_time,
@@ -377,9 +381,7 @@ def _on_headers(self, data):
self.stream.read_until_close(self._on_body)
def _on_body(self, data):
- if self._timeout is not None:
- self.io_loop.remove_timeout(self._timeout)
- self._timeout = None
+ self._remove_timeout()
original_request = getattr(self.request, "original_request",
self.request)
if (self.request.follow_redirects and
Something went wrong with that request. Please try again.