[New-bugs-announce] [issue38285] Asyncio BaseEventLoop can support socket types other than SOCK_STREAM

Malversán report at bugs.python.org
Thu Sep 26 08:05:11 EDT 2019

New submission from Malversán <malverspam at gmail.com>:

Currently the BaseEventLoop class in asyncio has explicit checks to raise ValueError when creating a connection if the socket argument has a type other than SOCK_STREAM:

This is also applicable for class _UnixSelectorEventLoop:

But the fact is that it actually supports other socket types, like SOCK_SEQPACKET for example.

Currently you can test this by dirty-hacking the socket class "type" property to momentarily trick the event loop into thinking that any socket is of SOCK_STREAM type.

# First create an AF_UNIX, SOCK_SEQPACKET socket.
sock = socket.socket(socket.AddressFamily.AF_UNIX, socket.SocketKind.SOCK_SEQ_PACKET)

params = { "sock" : sock, "protocol_factory" : lambda: protocol }

# Now do the trick.
hack = (params["sock"].type != socket.SocketKind.SOCK_STREAM)

if hack:
    # Substitute class property getter with fixed value getter.
    socket_property = socket.socket.type
    socket.socket.type = property(lambda self: socket.SocketKind.SOCK_STREAM, None, None,)

# Use the socket normally to create connection and run the event loop.
loop = asyncio.new_event_loop()
coroutine = loop.create_unix_connection(**params)    # It also works with .create_connection()
transport, protocol = loop.run_until_complete(coroutine)

# Revert the trick.
if hack:
    # Restore class property getter.
    socket.socket.type = socket_property

As dirty as it looks, this works flawlessy. It just tricks the event loop .create_connection() call to bypass the explicit check of using a SOCK_STREAM socket. This done, THE EVENT LOOP SUPPORTS SOCK_SEQPACKET PERFECTLY.

This is the solution I'm currently using to communicate an application with a local daemon, but I would really prefer to have the SOCK_SEQPACKET support allowed into the event loop itself. Having in mind that it simply works with other socket types, I find that limiting the use of the event loop with an explicit SOCK_STREAM-only check is somehow artificial and unrealistic.

Thanks in advance for your attention.

components: asyncio
messages: 353296
nosy: asvetlov, malversan, yselivanov
priority: normal
severity: normal
status: open
title: Asyncio BaseEventLoop can support socket types other than SOCK_STREAM
type: enhancement
versions: Python 3.5, Python 3.6, Python 3.7, Python 3.8, Python 3.9

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list