close ws channel if can't accept

This commit is contained in:
Timothy Pogue 2022-11-24 11:35:50 -07:00
parent 48242ca02b
commit 101dec461e

View File

@ -125,9 +125,14 @@ class WebSocketChannel:
async def accept(self): async def accept(self):
""" """
Accept the underlying websocket connection Accept the underlying websocket connection,
if the connection has been closed before we can
accept, just close the channel.
""" """
try:
return await self._websocket.accept() return await self._websocket.accept()
except RuntimeError:
await self.close()
async def close(self): async def close(self):
""" """
@ -172,6 +177,7 @@ class WebSocketChannel:
:param **kwargs: Any extra kwargs to pass to gather :param **kwargs: Any extra kwargs to pass to gather
""" """
if not self.is_closed():
# Wrap the coros into tasks if they aren't already # Wrap the coros into tasks if they aren't already
self._channel_tasks = [ self._channel_tasks = [
task if isinstance(task, asyncio.Task) else asyncio.create_task(task) task if isinstance(task, asyncio.Task) else asyncio.create_task(task)