From: Ben Darnell Date: Wed, 20 Jul 2011 02:35:22 +0000 (-0700) Subject: Consistently guard against uncaught exceptions leading to multiple callback X-Git-Tag: v2.1.0~71 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=080dab2d7de606bfbdd897d7d3dbd9a175e35ab4;p=thirdparty%2Ftornado.git Consistently guard against uncaught exceptions leading to multiple callback invocations in SimpleAsyncHTTPClient. --- diff --git a/tornado/simple_httpclient.py b/tornado/simple_httpclient.py index 71508e5c2..a2bf95059 100644 --- a/tornado/simple_httpclient.py +++ b/tornado/simple_httpclient.py @@ -264,23 +264,24 @@ class _HTTPConnection(object): self.stream.write(self.request.body) self.stream.read_until(b("\r\n\r\n"), self._on_headers) + def _run_callback(self, response): + if self.callback is not None: + callback = self.callback + self.callback = None + callback(response) + @contextlib.contextmanager def cleanup(self): try: yield except Exception, e: logging.warning("uncaught exception", exc_info=True) - if self.callback is not None: - callback = self.callback - self.callback = None - callback(HTTPResponse(self.request, 599, error=e)) + self._run_callback(HTTPResponse(self.request, 599, error=e)) def _on_close(self): - if self.callback is not None: - callback = self.callback - self.callback = None - callback(HTTPResponse(self.request, 599, - error=HTTPError(599, "Connection closed"))) + self._run_callback(HTTPResponse( + self.request, 599, + error=HTTPError(599, "Connection closed"))) def _on_headers(self, data): data = native_str(data.decode("latin1")) @@ -338,9 +339,8 @@ class _HTTPConnection(object): self.code, headers=self.headers, buffer=buffer, effective_url=self.request.url) - self.callback(response) + self._run_callback(response) self.stream.close() - self.callback = None def _on_chunk_length(self, data): # TODO: "chunk extensions" http://tools.ietf.org/html/rfc2616#section-3.6.1