Repository: tornadoweb/tornado

Issue:
RequestHandler.finish() future not resolving
Just tried Tornado 5.1 for the first time and am wondering if I'm doing something wrong when waiting for the future of the `finish` method:

```python
import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    async def get(self):
        print('before')
        await self.finish("Hello, world")
        print('after')

def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ])

if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()
```

It seems like the future is never resolved. `after` is not printed and interrupting the application outputs:

```
KeyboardInterrupt
ERROR:asyncio:Task was destroyed but it is pending!
task: <Task pending coro=<MainHandler.get() running at main.py:7> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f0cb0b25e28>()]> cb=[IOLoop.add_future.<locals>.<lambda>() at /home/sebastian/.virtualenvs/tempenv-2fc3210213e1f/lib/python3.6/site-packages/tornado/ioloop.py:719]>
```

Python 3.6, Ubuntu

Discussion:

Patch:
diff --git a/tornado/http1connection.py b/tornado/http1connection.py
index 10402b4b..0413fd9a 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:


Test patch:
diff --git a/tornado/test/web_test.py b/tornado/test/web_test.py
index a6f5d1bd..d43d46bc 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)