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:
Here's a "quick fix" for the 5.1 branch:

```diff
--- a/tornado/websocket.py
+++ b/tornado/websocket.py
@@ -754,7 +754,11 @@ class WebSocketProtocol13(WebSocketProtocol):
         open_result = self._run_callback(self.handler.open, *self.handler.open_args,
                                          **self.handler.open_kwargs)
         if open_result is not None:
-            yield open_result
+            try:
+                yield open_result
+            except Exception:
+                self._abort()
+
         yield self._receive_frame_loop()
 
     def _parse_extensions_header(self, headers):
```

It would be proper to open a pull request against the master branch first (6.0 beta has lots of changes), and a proper fix may involve adjusting `_run_callback()` to handle exceptions from coroutines as well ... but I'll just let this sit here for a bit in case Ben has a clear idea of how he wants this case handled, and it's easier for him to just do it :)
I think the right answer at least in 6.0 is to move away from run_callback here. It's a weird hybrid of awaitable use and fire-and-forget and it tends to log errors twice. I'll leave it for the other callbacks for now, but in #2581 I've removed the use of run_callback for open. 

Resolving patch:
diff --git a/tornado/websocket.py b/tornado/websocket.py
--- a/tornado/websocket.py
+++ b/tornado/websocket.py
@@ -751,10 +751,14 @@ def _accept_connection(self):
         self.stream = self.handler.stream
 
         self.start_pinging()
-        open_result = self._run_callback(self.handler.open, *self.handler.open_args,
-                                         **self.handler.open_kwargs)
-        if open_result is not None:
-            yield open_result
+        try:
+            open_result = self.handler.open(*self.handler.open_args, **self.handler.open_kwargs)
+            if open_result is not None:
+                yield open_result
+        except Exception:
+            self.handler.log_exception(*sys.exc_info())
+            self._abort()
+
         yield self._receive_frame_loop()
 
     def _parse_extensions_header(self, headers):


Test patch:
diff --git a/tornado/test/websocket_test.py b/tornado/test/websocket_test.py
--- a/tornado/test/websocket_test.py
+++ b/tornado/test/websocket_test.py
@@ -181,6 +181,18 @@ def on_message(self, message):
         self.write_message('ok')
 
 
+class ErrorInOpenHandler(TestWebSocketHandler):
+    def open(self):
+        raise Exception("boom")
+
+
+class ErrorInAsyncOpenHandler(TestWebSocketHandler):
+    @gen.coroutine
+    def open(self):
+        yield gen.sleep(0.01)
+        raise Exception("boom")
+
+
 class WebSocketBaseTestCase(AsyncHTTPTestCase):
     @gen.coroutine
     def ws_connect(self, path, **kwargs):
@@ -225,6 +237,10 @@ def get_app(self):
              dict(close_future=self.close_future)),
             ('/open_coroutine', OpenCoroutineHandler,
              dict(close_future=self.close_future, test=self)),
+            ("/error_in_open", ErrorInOpenHandler,
+             dict(close_future=self.close_future)),
+            ("/error_in_async_open", ErrorInAsyncOpenHandler,
+             dict(close_future=self.close_future)),
         ], template_loader=DictLoader({
             'message.html': '<b>{{ message }}</b>',
         }))
@@ -511,6 +527,20 @@ def test_open_coroutine(self):
         self.assertEqual(res, 'ok')
         yield self.close(ws)
 
+    @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)
+
 
 if sys.version_info >= (3, 5):
     NativeCoroutineOnMessageHandler = exec_test(globals(), locals(), """