Skip to content

Commit 7778e71

Browse files
committed
Better socket type check for Linux
1 parent d60be44 commit 7778e71

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

uvloop/includes/stdlib.pxi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ cdef inspect_isgenerator = inspect.isgenerator
6464
cdef int has_SO_REUSEPORT = hasattr(socket, 'SO_REUSEPORT')
6565
cdef int SO_REUSEPORT = getattr(socket, 'SO_REUSEPORT', 0)
6666
cdef int SO_BROADCAST = getattr(socket, 'SO_BROADCAST')
67+
cdef int SOCK_NONBLOCK = getattr(socket, 'SOCK_NONBLOCK', -1)
6768

6869
cdef socket_gaierror = socket.gaierror
6970
cdef socket_error = socket.error

uvloop/loop.pyx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,23 @@ include "errors.pyx"
3939

4040

4141
cdef _is_sock_stream(sock_type):
42-
# Linux's socket.type is a bitmask that can include extra info
43-
# about socket, therefore we can't do simple
44-
# `sock_type == socket.SOCK_STREAM`.
45-
return (sock_type & uv.SOCK_STREAM) == uv.SOCK_STREAM
42+
if SOCK_NONBLOCK == -1:
43+
return sock_type == uv.SOCK_STREAM
44+
else:
45+
# Linux's socket.type is a bitmask that can include extra info
46+
# about socket (like SOCK_NONBLOCK bit), therefore we can't do simple
47+
# `sock_type == socket.SOCK_STREAM`, see
48+
# https://github.com/torvalds/linux/blob/v4.13/include/linux/net.h#L77
49+
# for more details.
50+
return (sock_type & 0xF) == uv.SOCK_STREAM
4651

4752

4853
cdef _is_sock_dgram(sock_type):
49-
# Linux's socket.type is a bitmask that can include extra info
50-
# about socket, therefore we can't do simple
51-
# `sock_type == socket.SOCK_DGRAM`.
52-
return (sock_type & uv.SOCK_DGRAM) == uv.SOCK_DGRAM
54+
if SOCK_NONBLOCK == -1:
55+
return sock_type == uv.SOCK_DGRAM
56+
else:
57+
# Read the comment in `_is_sock_stream`.
58+
return (sock_type & 0xF) == uv.SOCK_DGRAM
5359

5460

5561
cdef isfuture(obj):

0 commit comments

Comments
 (0)