New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patching socket in Python3 give wrong return values for socket.getaddrinfo #1310

Closed
theyosh opened this Issue Nov 10, 2018 · 0 comments

Comments

Projects
None yet
1 participant
@theyosh

theyosh commented Nov 10, 2018

  • gevent version: 1.3.7 (installed through pip)
  • Python version: 2.7.13 vs 3.5.3
  • Operating System: Raspbian GNU/Linux 9 (4.14.79-v7+ #1159 SMP Sun Nov 4 17:50:20 GMT 2018 armv7l GNU/Linux)

Description:

In python3 it looks like the socket variables are changed.
After running monkey.patch_socket() the function socket.getaddrinfo does return different/numeric values.

By default socket.getaddrinfo returns values of:
<AddressFamily.AF_INET6: 10> and <SocketKind.SOCK_STREAM: 1>
https://docs.python.org/3/library/socket.html#socket.getaddrinfo

After monky.patch_socket those are just integers 10 and 1

This will give an assert error when you check if the output Socket stream is the same value as socket.SOCK_STREAM

On python2 it is not an issue, because there it is all integers

Traceback (most recent call last):
  File "t.py", line 34, in <module>
    assert gai[0][1] is socket.SOCK_STREAM
AssertionError

What I've run:

# Error on https://github.com/miccoli/pyownet/blob/dev/src/pyownet/protocol.py#L724 when monkey patch is enabled....?
# https://docs.python.org/3/library/socket.html#socket.getaddrinfo

import socket
import sys
from gevent import monkey

print('Monkey patch socket bug in Python 3.5+ ?')
print('Currently running python version: {}'.format(sys.version_info))
print('BEFORE monkey.patch_socket()')
print('Test socket stream socket.SOCK_STREAM')
print(socket.SOCK_STREAM)
print('Stream socket nr: {}'.format(socket.SOCK_STREAM))

print('Get getaddrinfo data for example.com on port 80')
gai = socket.getaddrinfo('example.com', 80, 0, socket.SOCK_STREAM,socket.IPPROTO_TCP)
print(gai)
print('Check if input socket equals with output type value: socket.SOCK_STREAM is gai[0][1]: {}'.format(socket.SOCK_STREAM is gai[0][1]))
assert gai[0][1] is socket.SOCK_STREAM

monkey.patch_socket()

print('AFTER monkey.patch_socket()')
print('Test socket stream socket.SOCK_STREAM')
print(socket.SOCK_STREAM)
print('Stream socket nr: {}'.format(socket.SOCK_STREAM))

print('Get getaddrinfo data for example.com on port 80')
gai = socket.getaddrinfo('example.com', 80, 0, socket.SOCK_STREAM,socket.IPPROTO_TCP)
print(gai)
print('Check if input socket equals with output type value: socket.SOCK_STREAM is gai[0][1]: {}'.format(socket.SOCK_STREAM is gai[0][1]))

print('This will cause an error with the test: assert gai[0][1] is socket.SOCK_STREAM')
assert gai[0][1] is socket.SOCK_STREAM

Output python3:

Monkey patch socket bug in Python 3.5+ ?
Currently running python version: sys.version_info(major=3, minor=5, micro=3, releaselevel='final', serial=0)
BEFORE monkey.patch_socket()
Test socket stream socket.SOCK_STREAM
SocketKind.SOCK_STREAM
Stream socket nr: 1
Get getaddrinfo data for example.com on port 80
[(<AddressFamily.AF_INET6: 10>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('93.184.216.34', 80))]
Check if input socket equals with output type value: socket.SOCK_STREAM is gai[0][1]: True
AFTER monkey.patch_socket()
Test socket stream socket.SOCK_STREAM
SocketKind.SOCK_STREAM
Stream socket nr: 1
Get getaddrinfo data for example.com on port 80
[(10, 1, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (2, 1, 6, '', ('93.184.216.34', 80))]
Check if input socket equals with output type value: socket.SOCK_STREAM is gai[0][1]: False
This will cause an error with the test: assert gai[0][1] is socket.SOCK_STREAM
Traceback (most recent call last):
  File "t.py", line 34, in <module>
    assert gai[0][1] is socket.SOCK_STREAM
AssertionError

Output python2:

Monkey patch socket bug in Python 3.5+ ?
Currently running python version: sys.version_info(major=2, minor=7, micro=13, releaselevel='final', serial=0)
BEFORE monkey.patch_socket()
Test socket stream socket.SOCK_STREAM
1
Stream socket nr: 1
Get getaddrinfo data for example.com on port 80
[(10, 1, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (2, 1, 6, '', ('93.184.216.34', 80))]
Check if input socket equals with output type value: socket.SOCK_STREAM is gai[0][1]: True
AFTER monkey.patch_socket()
Test socket stream socket.SOCK_STREAM
1
Stream socket nr: 1
Get getaddrinfo data for example.com on port 80
[(10, 1, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (2, 1, 6, '', ('93.184.216.34', 80))]
Check if input socket equals with output type value: socket.SOCK_STREAM is gai[0][1]: True
This will cause an error with the test: assert gai[0][1] is socket.SOCK_STREAM

jamadden added a commit that referenced this issue Nov 12, 2018

ricardokirkner pushed a commit to ricardokirkner/gevent that referenced this issue Nov 30, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment