Python Forum

Full Version: Asyncio StreamReader read method doesn't respect timeout when using SSL
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?