Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

SSL hangs if connection is closed before handshake completed #472

@tvoinarovskyi

Description

@tvoinarovskyi

Here's the snippet, that replicates the issue. I stomped upon this in normal TCP sockets, so sockpair is not the issue.

import asyncio
import socket
import ssl
import sys
import threading

context = ssl.create_default_context()
rsock, wsock = socket.socketpair()


def reader(sock):
    data = sock.recv(4)
    print("RCV", data)
    sock.close()

t = threading.Thread(target=reader, args=(rsock, ))
t.start()
try:
    if sys.argv[1] == 'sync':
        # The blocking example works OK. Exits right away
        sock = context.wrap_socket(
            wsock,
            server_hostname="localhost",
        )
        print("Connected")

    elif sys.argv[1] == "async":
        # Non blocking call example
        async def connect(loop):
            reader, writer = await asyncio.open_connection(
                sock=wsock, ssl=context, loop=loop,
                server_hostname="localhost")
            print("Connected Async")

        loop = asyncio.get_event_loop()
        loop.run_until_complete(loop.create_task(connect(loop)))

finally:
    wsock.close()
    t.join()

The idea is, that the other end is not an SSL socket, but rather some plain protocol, that reads size (lets say 4 bytes) and closes the socket, as size is inappropriate.
Blocking (test.py sync) socket works as expected raising ConnectionResetError
Async (test.py async) example blocks forever

Version:
Python 3.5.2 (default, Jul 17 2016, 00:00:00)
[GCC 4.8.4] on linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions