# Buggy code
async def send_message_receive_stream(
    server_url: Text, auth_token: Text, sender_id: Text, message: Text
):
    payload = {"sender": sender_id, "message": message}

    url = f"{server_url}/webhooks/rest/webhook?stream=true&token={auth_token}"

    # Define timeout to not keep reading in case the server crashed in between
    timeout = ClientTimeout(DEFAULT_STREAM_READING_TIMEOUT_IN_SECONDS)

    async with aiohttp.ClientSession(timeout=timeout) as session:
        async with session.post(url, json=payload, raise_for_status=True) as resp:
            async for line in resp.content:
                if line:
                    yield json.loads(line.decode(DEFAULT_ENCODING))


# Traceback (TimeoutError)
2019-10-14 10:18:37 ERROR    asyncio  - Task exception was never retrieved
future: <Task finished coro=<configure_app.<locals>.run_cmdline_io() done, defined at /Users/tabergma/.virtualenv/rasa/lib/python3.6/site-packages/rasa/core/run.py:123> exception=TimeoutError()>
Traceback (most recent call last):
File "/Users/tabergma/.virtualenv/rasa/lib/python3.6/site-packages/rasa/core/run.py", line 127, in run_cmdline_io
server_url=constants.DEFAULT_SERVER_FORMAT.format("http", port)
File "/Users/tabergma/.virtualenv/rasa/lib/python3.6/site-packages/rasa/core/channels/console.py", line 138, in record_messages
async for response in bot_responses:
File "/Users/tabergma/.virtualenv/rasa/lib/python3.6/site-packages/async_generator/_impl.py", line 366, in step
return await ANextIter(self._it, start_fn, *args)
File "/Users/tabergma/.virtualenv/rasa/lib/python3.6/site-packages/async_generator/_impl.py", line 205, in throw
return self._invoke(self._it.throw, type, value, traceback)
File "/Users/tabergma/.virtualenv/rasa/lib/python3.6/site-packages/async_generator/_impl.py", line 209, in _invoke
result = fn(*args)
File "/Users/tabergma/.virtualenv/rasa/lib/python3.6/site-packages/rasa/core/channels/console.py", line 103, in send_message_receive_stream
async for line in resp.content:
File "/Users/tabergma/.virtualenv/rasa/lib/python3.6/site-packages/aiohttp/streams.py", line 40, in __anext__
rv = await self.read_func()
File "/Users/tabergma/.virtualenv/rasa/lib/python3.6/site-packages/aiohttp/streams.py", line 329, in readline
await self._wait('readline')
File "/Users/tabergma/.virtualenv/rasa/lib/python3.6/site-packages/aiohttp/streams.py", line 297, in _wait
await waiter
File "/Users/tabergma/.virtualenv/rasa/lib/python3.6/site-packages/aiohttp/helpers.py", line 585, in __exit__
raise asyncio.TimeoutError from None
concurrent.futures._base.TimeoutError

# Fixed code
async def send_message_receive_stream(
    server_url: Text, auth_token: Text, sender_id: Text, message: Text
):
    payload = {"sender": sender_id, "message": message}

    url = f"{server_url}/webhooks/rest/webhook?stream=true&token={auth_token}"

    # Define timeout to not keep reading in case the server crashed in between
    timeout = _get_stream_reading_timeout()

    async with aiohttp.ClientSession(timeout=timeout) as session:
        async with session.post(url, json=payload, raise_for_status=True) as resp:
            async for line in resp.content:
                if line:
                    yield json.loads(line.decode(DEFAULT_ENCODING))