Skip to content
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

socket recv returning multiple data types #245

Closed
jerzyk opened this issue Sep 5, 2015 · 7 comments
Closed

socket recv returning multiple data types #245

jerzyk opened this issue Sep 5, 2015 · 7 comments

Comments

@jerzyk
Copy link

jerzyk commented Sep 5, 2015

python 3.4 - when using green sockets, most of the time socket.recv is returning bytes, but when
communication is broken, it returns string

sock = eventlet.connect((SERVER_ADDRESS, port))

while True:
    _d = sock.recv(1024)
    print('recv: %s %s' % (_d, type(_d)))

returns:

Starting processes..
Connecting to port 9800
Connecting to port 9800
recv: b'{Ping}\r' <class 'bytes'>
record (port: 9800): b'{Ping}'
recv: b'{Ping}\r' <class 'bytes'>
record (port: 9800): b'{Ping}'
recv: b'' <class 'bytes'>
recv:  <class 'str'>
@temoto
Copy link
Member

temoto commented Sep 5, 2015

I tried the following code to reproduce your issue and it consistently returns bytes. What kind of "communication is broken" exactly are you experiencing?

def test_recv_types():
    server_sock = eventlet.listen(('127.0.0.1', 0))

    def server():
        conn, _ = server_sock.accept()
        conn.send(b'a')
        eventlet.sleep(0.1)
        conn.close()
    server_thread = eventlet.spawn(server)

    client = eventlet.connect(server_sock.getsockname())
    s1 = client.recv(1024)
    assert type(s1) == type(b'')
    # tried few combinations of server and client failures
    # server_thread.kill()
    # client.shutdown(socket.SHUT_RDWR)
    # client.close()
    s2 = client.recv(1)
    assert type(s2) == type(b'')

@jerzyk
Copy link
Author

jerzyk commented Sep 5, 2015

as you see, in the second part of the ticket, recv returns empty string after server closed a socket.
my example code is from a client

@temoto
Copy link
Member

temoto commented Sep 5, 2015

Can you run the code I pasted? It does exactly that: server closes connection after 0.1s timeout, then s2 is empty b''. What version of eventlet do you use?

@jerzyk
Copy link
Author

jerzyk commented Sep 5, 2015

eventlet 0.17.4, did not get errors on your code, here is mine (I was trying to put it into one file, but then there were no errors...)

run server, then client, stop server, assertion error shows

server:

#!/usr/bin/env python
import eventlet


def server():
    server_sock = eventlet.listen(('127.0.0.1', 9801))
    conn, _ = server_sock.accept()
    while True:
        conn.sendall(b'{Ping}')
        eventlet.sleep(2)
    conn.close()


if __name__ == "__main__":
    server_thread = eventlet.spawn(server)
    server_thread.wait()

client:

#!/usr/bin/env python
import eventlet


def handler():
    socket = eventlet.connect(('127.0.0.1', 9801))
    data = b''
    while True:
        timeout = eventlet.Timeout(5)
        try:
            data = socket.recv(1024)
            assert isinstance(data, bytes)
        except eventlet.Timeout:
            socket.sendall(b'pong')
        finally:
            timeout.cancel()

        if not data:
            eventlet.sleep(2)
            socket.sendall(b'ping')

    socket.close()


if __name__ == "__main__":
    pool = eventlet.GreenPool(2)
    pool.spawn_n(handler)
    pool.waitall()

@temoto
Copy link
Member

temoto commented Sep 6, 2015

Thank you. Indeed, it deceptively passes when client and server are just two green threads.

Error was in eventlet/green/base.py:318: return ''.

temoto added a commit that referenced this issue Sep 6, 2015
@jerzyk
Copy link
Author

jerzyk commented Sep 6, 2015

@temoto what's an oldest supported python version?(I could not find it in the docs) - with 2.5 supported there should be six.b('')

@temoto
Copy link
Member

temoto commented Sep 6, 2015

That's not a problem, we use few 2.6+ syntax features already. Fix is merged in master 001f31f.

@temoto temoto closed this as completed Sep 6, 2015
@temoto temoto added this to the v0.18 milestone Sep 6, 2015
openstack-gerrit pushed a commit to openstack-archive/deb-python-eventlet that referenced this issue Sep 2, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants