Repository: tornadoweb/tornado

Issue:
Unhandled exception in open coroutine of websockethandler does not close connection
I tested the following app with version 5.1.1 of tornado:

```
import tornado
import tornado.websocket
import tornado.ioloop
from tornado import gen


class MySocket(tornado.websocket.WebSocketHandler):
    def open(self):
        print 'socket opened'
        raise Exception('bad')

    @gen.coroutine
    def on_message(self, message):
        print message

    def on_close(self):
        print 'closed'


def create_application():
    return tornado.web.Application([
        (r"^/?$", MySocket),
    ])


application = create_application()
application.listen(8000)
tornado.ioloop.IOLoop.current().start()
```

If I connect with a client, I get the following (correct) output on the server side:

```
socket opened
ERROR:tornado.application:Uncaught exception GET / (127.0.0.1)
HTTPServerRequest(protocol='http', host='localhost:8000', method='GET', uri='/', version='HTTP/1.1', remote_ip='127.0.0.1')
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/tornado/websocket.py", line 546, in _run_callback
    result = callback(*args, **kwargs)
  File "./test.py", line 10, in open
    raise Exception('bad')
Exception: bad
closed
```

However, if I add the gen.coroutine annotation to ``open`` and run the same test, I get this output instead:

```
socket opened
ERROR:tornado.application:Exception in callback <functools.partial object at 0x7fea30c31c00>
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/tornado/ioloop.py", line 758, in _run_callback
    ret = callback()
  File "/usr/local/lib/python2.7/site-packages/tornado/stack_context.py", line 300, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/tornado/websocket.py", line 553, in <lambda>
    self.stream.io_loop.add_future(result, lambda f: f.result())
  File "/usr/local/lib/python2.7/site-packages/tornado/concurrent.py", line 261, in result
    raise_exc_info(self._exc_info)
  File "/usr/local/lib/python2.7/site-packages/tornado/gen.py", line 307, in wrapper
    result = func(*args, **kwargs)
  File "./test.py", line 11, in open
    raise Exception('bad')
Exception: bad
ERROR:tornado.application:Future exception was never retrieved: Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/tornado/gen.py", line 1141, in run
    yielded = self.gen.throw(*exc_info)
  File "/usr/local/lib/python2.7/site-packages/tornado/websocket.py", line 757, in _accept_connection
    yield open_result
  File "/usr/local/lib/python2.7/site-packages/tornado/gen.py", line 1133, in run
    value = future.result()
  File "/usr/local/lib/python2.7/site-packages/tornado/concurrent.py", line 261, in result
    raise_exc_info(self._exc_info)
  File "/usr/local/lib/python2.7/site-packages/tornado/gen.py", line 307, in wrapper
    result = func(*args, **kwargs)
  File "./test.py", line 11, in open
    raise Exception('bad')
Exception: bad
```

Note that there is no "closed" message. On the client side, the client still thinks the connection is still open and I can send messages without error. Without the coroutine, the client gets a broken pipe error the next time it tries to send a message.

Discussion:

Patch:
diff --git a/tornado/websocket.py b/tornado/websocket.py
index 50665c69..ef13751a 100644
--- a/tornado/websocket.py
+++ b/tornado/websocket.py
@@ -948,11 +948,15 @@ class WebSocketProtocol13(WebSocketProtocol):
         self.stream = handler._detach_stream()
 
         self.start_pinging()
-        open_result = self._run_callback(
-            handler.open, *handler.open_args, **handler.open_kwargs
-        )
-        if open_result is not None:
-            await open_result
+        try:
+            open_result = handler.open(*handler.open_args, **handler.open_kwargs)
+            if open_result is not None:
+                await open_result
+        except Exception:
+            handler.log_exception(*sys.exc_info())
+            self._abort()
+            return
+
         await self._receive_frame_loop()
 
     def _parse_extensions_header(


Test patch:
diff --git a/tornado/test/websocket_test.py b/tornado/test/websocket_test.py
index 1a5a2728..715ecf1e 100644
--- a/tornado/test/websocket_test.py
+++ b/tornado/test/websocket_test.py
@@ -187,6 +187,17 @@ class OpenCoroutineHandler(TestWebSocketHandler):
         self.write_message("ok")
 
 
+class ErrorInOpenHandler(TestWebSocketHandler):
+    def open(self):
+        raise Exception("boom")
+
+
+class ErrorInAsyncOpenHandler(TestWebSocketHandler):
+    async def open(self):
+        await asyncio.sleep(0)
+        raise Exception("boom")
+
+
 class WebSocketBaseTestCase(AsyncHTTPTestCase):
     @gen.coroutine
     def ws_connect(self, path, **kwargs):
@@ -245,6 +256,8 @@ class WebSocketTest(WebSocketBaseTestCase):
                     OpenCoroutineHandler,
                     dict(close_future=self.close_future, test=self),
                 ),
+                ("/error_in_open", ErrorInOpenHandler),
+                ("/error_in_async_open", ErrorInAsyncOpenHandler),
             ],
             template_loader=DictLoader({"message.html": "<b>{{ message }}</b>"}),
         )
@@ -535,6 +548,20 @@ class WebSocketTest(WebSocketBaseTestCase):
         res = yield ws.read_message()
         self.assertEqual(res, "ok")
 
+    @gen_test
+    def test_error_in_open(self):
+        with ExpectLog(app_log, "Uncaught exception"):
+            ws = yield self.ws_connect("/error_in_open")
+            res = yield ws.read_message()
+        self.assertIsNone(res)
+
+    @gen_test
+    def test_error_in_async_open(self):
+        with ExpectLog(app_log, "Uncaught exception"):
+            ws = yield self.ws_connect("/error_in_async_open")
+            res = yield ws.read_message()
+        self.assertIsNone(res)
+
 
 class NativeCoroutineOnMessageHandler(TestWebSocketHandler):
     def initialize(self, **kwargs):