From: garenchan <1412950785@qq.com> Date: Mon, 10 Dec 2018 11:16:38 +0000 (+0800) Subject: Use 'future_add_done_callback' to add callback. X-Git-Tag: v6.0.0b1~14^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F2549%2Fhead;p=thirdparty%2Ftornado.git Use 'future_add_done_callback' to add callback. The PR #2449 was incomplete, it omited consideration of another branch of the 'flush' method. In short, the future returned by 'RequestHandler.finish()' may also be the return value of 'HTTP1Connection.write()'. So we should make the same change to 'HTTP1Connection.write()'. Fix #2448. --- diff --git a/tornado/http1connection.py b/tornado/http1connection.py index 10402b4bf..0413fd9a6 100644 --- a/tornado/http1connection.py +++ b/tornado/http1connection.py @@ -492,7 +492,7 @@ class HTTP1Connection(httputil.HTTPConnection): else: future = self._write_future = Future() self._pending_write = self.stream.write(self._format_chunk(chunk)) - self._pending_write.add_done_callback(self._on_write_complete) + future_add_done_callback(self._pending_write, self._on_write_complete) return future def finish(self) -> None: diff --git a/tornado/test/web_test.py b/tornado/test/web_test.py index a6f5d1bd0..d43d46bcf 100644 --- a/tornado/test/web_test.py +++ b/tornado/test/web_test.py @@ -224,6 +224,13 @@ class FinalReturnTest(WebTestCase): test.final_return = self.finish() yield test.final_return + @gen.coroutine + def post(self): + self.write("hello,") + yield self.flush() + test.final_return = self.finish("world") + yield test.final_return + class RenderHandler(RequestHandler): def create_template_loader(self, path): return DictLoader({"foo.html": "hi"}) @@ -243,6 +250,11 @@ class FinalReturnTest(WebTestCase): self.assertIsInstance(self.final_return, Future) self.assertTrue(self.final_return.done()) + response = self.fetch(self.get_url("/finish"), method="POST", body=b"") + self.assertEqual(response.code, 200) + self.assertIsInstance(self.final_return, Future) + self.assertTrue(self.final_return.done()) + def test_render_method_return_future(self): response = self.fetch(self.get_url("/render")) self.assertEqual(response.code, 200)