[New-bugs-announce] [issue32331] apply SOCK_TYPE_MASK to socket.type on Linux

Yury Selivanov report at bugs.python.org
Thu Dec 14 22:42:49 EST 2017

New submission from Yury Selivanov <yselivanov at gmail.com>:

On Linux, socket type is both a socket type and a bit mask (of SOCK_CLOEXEC and SOCK_NONBLOCK).  Therefore, anyone who write code like 'if sock.type == SOCK_STREAM' writes non-portable code, that occasionally breaks on Linux.  

This caused some hard to spot bugs in asyncio already: https://bugs.python.org/issue27456  -- this one was discovered only 1 year after the actual change.

On Linux, in 'include/linux/net.h' there's a SOCK_TYPE_MASK macro defined to 0xF.  The idea is that on Linux you should mask socket type before comparing it to SOCK_STREAM or other socket types.

Using socket.SOCK_NONBLOCK on Python was always error-prone even if one targets only Linux.  For instance, socket.type won't be updated to include SOCK_NONBLOCK if the socket was updated via ioctl call directly.

Therefore, I propose to fix socket.type getter to always (on Linux) apply that mask in Python.

components: Library (Lib)
messages: 308363
nosy: asvetlov, inada.naoki, pitrou, vstinner, yselivanov
priority: normal
severity: normal
status: open
title: apply SOCK_TYPE_MASK to socket.type on Linux
type: enhancement
versions: Python 3.7

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list