Python Forum
Asyncio StreamReader read method doesn't respect timeout when using SSL - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Asyncio StreamReader read method doesn't respect timeout when using SSL (/thread-20024.html)



Asyncio StreamReader read method doesn't respect timeout when using SSL - dukadahake - Jul-24-2019

I'm making requests to SSL servers using asyncio's streams.
Here is an example method.

async def make_request():
    async with asyncio.connect('127.0.0.1', 8888, ssl=ssl.SSLContext()) as stream:
        await stream.write(REQUEST)
        print(await stream.read(RESPONSE_LENGTH))
Some of the server complete the SSl handshake, receive the request but don't send any response. This makes the method stuck on the stream.read call as expected.

In such cases I want to define a timeout for the read call. I have tried to achieve this by adding a wait_for.
print(await asyncio.wait_for(stream.read(RESPONSE_LENGTH),timeout=2))
However, this method doesn't work and continues to block indefinitely.
Additionally, if we use a plain TCP connection the fix I proposed works fine.

Whats wrong with my solution? Is there an alternative way to achieve it?