websockets: socket.send() raised exception
Hi,
I have an issue with running a simple example (paraphrased from https://websockets.readthedocs.org/en/stable/intro.html). I’m testing with Python 3.4.2 (Debian Jessie default) and websockets from master. Consider the following code:
# server.py
import asyncio
import websockets
@asyncio.coroutine
def send(websocket, path):
while True:
if not websocket.open:
return
yield from websocket.send('message')
start_server = websockets.serve(send, 'localhost', 8765)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
# client.py
import asyncio
import websockets
@asyncio.coroutine
def recv(uri):
websocket = yield from websockets.connect(uri)
while True:
message = yield from websocket.recv()
print(message)
asyncio.get_event_loop().run_until_complete(recv('ws://localhost:8765'))
I run python server.py
in one terminal and python client.py
in another. After I ^C
the client, the server starts printing socket.send() raised exception.
and does not accept new connections.
After I ^C
the server I get the following output:
socket.send() raised exception.
socket.send() raised exception.
socket.send() raised exception.
socket.send() raised ex^C
Traceback (most recent call last):
File "server.py", line 13, in <module>
asyncio.get_event_loop().run_forever()
File "/usr/lib/python3.4/asyncio/base_events.py", line 244, in run_forever
self._run_once()
File "/usr/lib/python3.4/asyncio/base_events.py", line 1081, in _run_once
handle._run()
File "/usr/lib/python3.4/asyncio/events.py", line 120, in _run
self._callback(*self._args)
File "/usr/lib/python3.4/asyncio/tasks.py", line 291, in _wakeup
self._step(value, None)
File "/usr/lib/python3.4/asyncio/tasks.py", line 235, in _step
result = coro.send(value)
File "/env/lib/python3.4/site-packages/websockets/server.py", line 77, in handler
yield from self.ws_handler(self, path)
File "server.py", line 9, in send
yield from websocket.send('message')
File "/env/lib/python3.4/site-packages/websockets/protocol.py", line 261, in send
yield from self.write_frame(opcode, data)
File "/env/lib/python3.4/site-packages/websockets/protocol.py", line 430, in write_frame
write_frame(frame, self.writer.write, is_masked)
File "/env/lib/python3.4/site-packages/websockets/framing.py", line 149, in write_frame
writer(output.getvalue())
File "/usr/lib/python3.4/asyncio/streams.py", line 269, in write
self._transport.write(data)
File "/usr/lib/python3.4/asyncio/selector_events.py", line 613, in write
logger.warning('socket.send() raised exception.')
File "/usr/lib/python3.4/logging/__init__.py", line 1286, in warning
self._log(WARNING, msg, args, **kwargs)
File "/usr/lib/python3.4/logging/__init__.py", line 1409, in _log
self.handle(record)
File "/usr/lib/python3.4/logging/__init__.py", line 1419, in handle
self.callHandlers(record)
File "/usr/lib/python3.4/logging/__init__.py", line 1489, in callHandlers
lastResort.handle(record)
File "/usr/lib/python3.4/logging/__init__.py", line 853, in handle
self.emit(record)
File "/usr/lib/python3.4/logging/__init__.py", line 981, in emit
stream.write(self.terminator)
KeyboardInterrupt
--- Logging error ---
Traceback (most recent call last):
--- Logging error ---
Traceback (most recent call last):
Exception ignored in: <bound method Task.__del__ of <Task finished coro=<handler() done, defined at /env/lib/python3.4/site-packages/websockets/server.py:54> exception=KeyboardInterrupt()>>
Traceback (most recent call last):
File "/usr/lib/python3.4/asyncio/tasks.py", line 93, in __del__
File "/usr/lib/python3.4/asyncio/futures.py", line 218, in __del__
File "/usr/lib/python3.4/asyncio/base_events.py", line 950, in call_exception_handler
File "/usr/lib/python3.4/logging/__init__.py", line 1303, in error
File "/usr/lib/python3.4/logging/__init__.py", line 1409, in _log
File "/usr/lib/python3.4/logging/__init__.py", line 1419, in handle
File "/usr/lib/python3.4/logging/__init__.py", line 1489, in callHandlers
File "/usr/lib/python3.4/logging/__init__.py", line 853, in handle
File "/usr/lib/python3.4/logging/__init__.py", line 984, in emit
File "/usr/lib/python3.4/logging/__init__.py", line 906, in handleError
File "/usr/lib/python3.4/traceback.py", line 169, in print_exception
File "/usr/lib/python3.4/traceback.py", line 153, in _format_exception_iter
File "/usr/lib/python3.4/traceback.py", line 18, in _format_list_iter
File "/usr/lib/python3.4/traceback.py", line 65, in _extract_tb_or_stack_iter
File "/env/lib/python3.4/linecache.py", line 15, in getline
File "/env/lib/python3.4/linecache.py", line 41, in getlines
File "/env/lib/python3.4/linecache.py", line 126, in updatecache
File "/env/lib/python3.4/tokenize.py", line 437, in open
AttributeError: 'module' object has no attribute 'open'
Task was destroyed but it is pending!
task: <Task pending coro=<run() running at /env/lib/python3.4/site-packages/websockets/protocol.py:299> wait_for=<Future finished result=True>>
Is this an expected behaviour? Perhaps there is some additional error handling required in my code?
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 20 (9 by maintainers)
Commits related to this issue
- Avoid busy loop during connection termination. Refs #84. Refs https://github.com/python/asyncio/pull/280. — committed to python-websockets/websockets by aaugustin 9 years ago
- Close with code 1006 on all connection errors. Thanks @neuroid for the report and @SzieberthAdam for the analysis. Fix #84. — committed to python-websockets/websockets by aaugustin 9 years ago
- Avoid busy loop during connection termination. Refs #84. Refs https://github.com/python/asyncio/pull/280. — committed to python-websockets/websockets by aaugustin 9 years ago
- Avoid busy loop during connection termination. Refs #84. Refs https://github.com/python/asyncio/pull/280. — committed to python-websockets/websockets by aaugustin 9 years ago
Thank you, and yes, it’s all my fault. In my project, at first it won’t throw
websockets.ConnectionClosed
exception andwebsocket.open
will always beTrue
withwebsocket.closed
alwaysFalse
, only print outevery time when calling
ws.send(...)
After debug, I have to add
await asyncio.sleep(0)
afterwebsocket.send(...)
, then all works like expected. So I think it’s because the thread havingws.send(...)
took all cpu time and websocket status never got changed.I can confirm that
connection_lost
is NOT getting called.I also tested this example with Python 3.4.3 and 3.5.0. Same results.