asyncio:Future exception was never retrieved

2,042 views
Skip to first unread message

Roman S

unread,
Jun 13, 2018, 8:34:50 AM6/13/18
to Tornado Web Server
hi all,

I am new to tornado.

# my application
here is very simple description
## websocket server
### on_open

* create tornado queue
* spawn a loop in callback - there messages will be processed by a blocking function
### on_close
* put None to queue to signal an exit from callback loop
### on message
* put message to queue
### loop in callback
* while True
* get message from queue
* if message is None, then "return" from loop
* if message is text, pass text to blocking function. Once result is available, "self.write_message(...)"

It works perfect also for concurrent clients.

In one of test cases, client opens connection, sends data and closes connection. On the server, after the first message was processed, the callback loop tries to send answer but the connection is already closed. The "close" event will arrive few ms later...
So I had to wrap "self.write_message(...)" as:

    def send_event(self, event):
       
try:
           
if self.ws_connection is not None and self.state < DecoderState.CLOSE:
               
self.write_message(json.dumps(event), binary=False)
       
# except tornado.websocket.WebSocketClosedError as e:
       
except tornado.websocket.WebSocketClosedError:
           
self.set_state(DecoderState.CLOSE)
           
print()
           
print('*** Connection to client is closed')
           
#print('  |_ %s :: %s' % (type(e), e))
           
print('  |_ drop event')
            pprint
.pprint(event)
           
print()

I try to recognize the closed connection with "set_state()" and drop message from queue. So everything works well, also cleanups.

BUT, I get error dump on the server (just a print of an error):

ERROR
:asyncio:Future exception was never retrieved
future
: <Future finished exception=WebSocketClosedError()>
Traceback (most recent call last):
 
File "/home/madkote/tornado_app/venv/lib/python3.5/site-packages/tornado/websocket.py", line 808, in wrapper
   
yield fut
 
File "/home/madkote/tornado_app/venv/lib/python3.5/site-packages/tornado/gen.py", line 1099, in run
    value
= future.result()
 
File "/usr/lib64/python3.5/asyncio/futures.py", line 294, in result
   
raise self._exception
tornado
.iostream.StreamClosedError: Stream is closed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
 
File "/home/madkote/tornado_app/venv/lib/python3.5/site-packages/tornado/gen.py", line 1107, in run
    yielded
= self.gen.throw(*exc_info)
 
File "/home/madkote/tornado_app/venv/lib/python3.5/site-packages/tornado/websocket.py", line 810, in wrapper
   
raise WebSocketClosedError()
tornado
.websocket.WebSocketClosedError


What the error stands for? As to my understanding, I have not missed any message from client, and process each one (even when I have "custom close" state - my blocking function will ignore messages and not send anything to client, which has already closed the connection).

I do suspect, that when I got WebSocketClosedError first time, then the server remembers it...


Thx for any help!

roman

Ben Darnell

unread,
Jun 27, 2018, 9:39:29 PM6/27/18
to python-...@googlegroups.com
write_message() is an asynchronous method: To call it and be assured that you're seeing any exception it raises, you must call it with `await` (or `yield` and `@gen.coroutine` if you're on Python versions older than 3.5). If you don't do this, the exception may be left for the IOLoop where it will be logged like this.

-Ben

--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornad...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages