# Buggy code
async def asgi_receive(self):
    if not self.connect_sent:
        self.connect_sent = True
        return {"type": "websocket.connect"}

    await self.handshake_completed_event.wait()

    if self.closed_event.is_set():
        # If the client disconnected: WebSocketServerProtocol set self.close_code.
        # If the handshake failed or the app closed before handshake completion,
        # use 1006 Abnormal Closure.
        code = getattr(self, "close_code", 1006)
        return {"type": "websocket.disconnect", "code": code}

    try:
        data = await self.recv()
    except websockets.ConnectionClosed as exc:
        return {"type": "websocket.disconnect", "code": exc.code}

    msg = {"type": "websocket.receive"}

    if isinstance(data, str):
        msg["text"] = data
    else:
        msg["bytes"] = data

    return msg


# Traceback (TypeError)
Traceback (most recent call last):
File "/home/ossi/src/channels-examples/.venv/lib/python3.7/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 140, in run_asgi
result = await asgi(self.asgi_receive, self.asgi_send)
File "/home/ossi/src/channels-examples/.venv/lib/python3.7/site-packages/channels/sessions.py", line 179, in __call__
return await self.inner(receive, self.send)
File "/home/ossi/src/channels-examples/.venv/lib/python3.7/site-packages/channels/middleware.py", line 41, in coroutine_call
await inner_instance(receive, send)
File "/home/ossi/src/channels-examples/.venv/lib/python3.7/site-packages/channels/consumer.py", line 59, in __call__
[receive, self.channel_receive], self.dispatch
File "/home/ossi/src/channels-examples/.venv/lib/python3.7/site-packages/channels/utils.py", line 59, in await_many_dispatch
await task
File "/home/ossi/src/channels-examples/.venv/lib/python3.7/site-packages/channels/utils.py", line 51, in await_many_dispatch
result = task.result()
File "/home/ossi/src/channels-examples/.venv/lib/python3.7/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 222, in asgi_receive
data = await self.recv()
File "/home/ossi/src/channels-examples/.venv/lib/python3.7/site-packages/websockets/protocol.py", line 419, in recv
return_when=asyncio.FIRST_COMPLETED,
File "/usr/lib/python3.7/asyncio/tasks.py", line 361, in wait
fs = {ensure_future(f, loop=loop) for f in set(fs)}
File "/usr/lib/python3.7/asyncio/tasks.py", line 361, in <setcomp>
fs = {ensure_future(f, loop=loop) for f in set(fs)}
File "/usr/lib/python3.7/asyncio/tasks.py", line 592, in ensure_future
raise TypeError('An asyncio.Future, a coroutine or an awaitable is '
TypeError: An asyncio.Future, a coroutine or an awaitable is required

# Fixed code
async def asgi_receive(self):
    if not self.connect_sent:
        self.connect_sent = True
        return {"type": "websocket.connect"}

    await self.handshake_completed_event.wait()
    try:
        await self.ensure_open()
        data = await self.recv()
    except websockets.ConnectionClosed as exc:
        return {"type": "websocket.disconnect", "code": exc.code}

    msg = {"type": "websocket.receive"}

    if isinstance(data, str):
        msg["text"] = data
    else:
        msg["bytes"] = data

    return msg