Skip to content

Fix handler for websockets lib not to ignore pending tasks #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions graphql_ws/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def get_graphql_params(self, *args, **kwargs):
async def _handle(self, ws, request_context=None):
connection_context = AiohttpConnectionContext(ws, request_context)
await self.on_open(connection_context)
pending_tasks = []
pending = set()
while True:
try:
if connection_context.closed:
Expand All @@ -63,18 +63,16 @@ async def _handle(self, ws, request_context=None):
except ConnectionClosedException:
break
finally:
pending_tasks = [t for t in pending_tasks if not t.done()]
if pending:
(_, pending) = await wait(pending, timeout=0, loop=self.loop)

task = ensure_future(
self.on_message(connection_context, message), loop=self.loop)
pending_tasks.append(task)
pending.add(task)

self.on_close(connection_context)
if pending_tasks:
for task in pending_tasks:
if not task.done():
task.cancel()
await wait(pending_tasks, loop=self.loop)
for task in pending:
task.cancel()

async def handle(self, ws, request_context=None):
await shield(self._handle(ws, request_context), loop=self.loop)
Expand Down
29 changes: 22 additions & 7 deletions graphql_ws/websockets_lib.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from inspect import isawaitable, isasyncgen
from inspect import isawaitable

from asyncio import ensure_future
from asyncio import ensure_future, wait, shield
from websockets import ConnectionClosed
from graphql.execution.executors.asyncio import AsyncioExecutor

Expand Down Expand Up @@ -38,25 +38,40 @@ async def close(self, code):


class WsLibSubscriptionServer(BaseSubscriptionServer):
def __init__(self, schema, keep_alive=True, loop=None):
self.loop = loop
super().__init__(schema, keep_alive)

def get_graphql_params(self, *args, **kwargs):
params = super(WsLibSubscriptionServer,
self).get_graphql_params(*args, **kwargs)
return dict(params, return_promise=True, executor=AsyncioExecutor())
return dict(params, return_promise=True, executor=AsyncioExecutor(loop=self.loop))

async def handle(self, ws, request_context=None):
async def _handle(self, ws, request_context):
connection_context = WsLibConnectionContext(ws, request_context)
await self.on_open(connection_context)
pending = set()
while True:
try:
if connection_context.closed:
raise ConnectionClosedException()
message = await connection_context.receive()
except ConnectionClosedException:
self.on_close(connection_context)
return
break
finally:
if pending:
(_, pending) = await wait(pending, timeout=0, loop=self.loop)

task = ensure_future(
self.on_message(connection_context, message), loop=self.loop)
pending.add(task)

ensure_future(self.on_message(connection_context, message))
self.on_close(connection_context)
for task in pending:
task.cancel()

async def handle(self, ws, request_context=None):
await shield(self._handle(ws, request_context), loop=self.loop)

async def on_open(self, connection_context):
pass
Expand Down