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

Attribute error on sio.wait() #417

Closed
Galarzaa90 opened this issue Jan 24, 2020 · 10 comments
Closed

Attribute error on sio.wait() #417

Galarzaa90 opened this issue Jan 24, 2020 · 10 comments
Assignees
Labels

Comments

@Galarzaa90
Copy link

I have been struggling to keep my socket connected in an application. Since this program is already running it's own loop, I have a background task that attempts to connect to the socket server, until it does. Initially, it was working fine, but as load increased, it started dropping the connection to the socket and not reconnecting (not sending the disconnected event either).

So I proceeded to add a sio.wait() right after connect, hoping that this would keep my connection alive, but now it is raising exceptions after some time online.

  File "/home/galarzaa/NabBot/cogs/tracking.py", line 157, in socket_manager
    await self.sio.wait()
  File "/home/galarzaa/NabBot/venv/lib/python3.6/site-packages/socketio/asyncio_client.py", line 122, in wait
    await self.eio.wait()
  File "/home/galarzaa/NabBot/venv/lib/python3.6/site-packages/engineio/asyncio_client.py", line 87, in wait
    await self.read_loop_task
  File "/home/galarzaa/NabBot/venv/lib/python3.6/site-packages/engineio/asyncio_client.py", line 502, in _read_loop_websocket
    pkt = packet.Packet(encoded_packet=p)
  File "/home/galarzaa/NabBot/venv/lib/python3.6/site-packages/engineio/packet.py", line 30, in __init__
    self.decode(encoded_packet)
  File "/home/galarzaa/NabBot/venv/lib/python3.6/site-packages/engineio/packet.py", line 60, in decode
    encoded_packet = encoded_packet.encode('utf-8')
AttributeError: 'int' object has no attribute 'encode'
@miguelgrinberg
Copy link
Owner

What server are you connecting to? It seems this client is receiving unexpected data from it.

@Galarzaa90
Copy link
Author

I'm using a python socket-io server too. This is the only client application that has issues, and I think it only happens when there's too high load on the event loop.

@miguelgrinberg
Copy link
Owner

Can you provide a server and client I can use to reproduce this?

@thoquine
Copy link

thoquine commented Apr 2, 2020

Hi, I also have the exact same error. The error happens when the client is connected to the server and you close the server (simulating a disconnect). Looking at the wait method is asyncio_client.py, I can see the reconnect task should be started but it does not...

Expected result: When server disconnect, the client try to reconnect.
Observed result: The error @Galarzaa90 mentionned.

Here is the client code:

import asyncio
import time
import socketio

loop = asyncio.get_event_loop()
sio = socketio.AsyncClient()
start_timer = None


@sio.event
async def connect():
    print('connected to server')


async def start_server():
    try: 
        await sio.connect('http://localhost:8080')
        await sio.wait()
    except socketio.exceptions.ConnectionError:
        print("Cannot connect to server")


if __name__ == '__main__':
    loop.run_until_complete(start_server())

You can use any server example

@miguelgrinberg
Copy link
Owner

@thoquine I cannot reproduce using the server example from the Flask-SocketIO repository. When I Ctrl-C the server the client remains waiting, and as soon as I start a new server it reconnects.

@thoquine
Copy link

thoquine commented Apr 2, 2020

@miguelgrinberg I think I found the problem.

When I kill the server with ctrl+c I got the error on the client side. But when I simply kill the terminal running the server, the client try to reconnect as expected.

I'm using the aiohttp version of the server. Any idea why it's doing this?
I'm using python 3.7.4

Thanks for your help!

@miguelgrinberg
Copy link
Owner

@thoquine Thanks, what I was missing was that the server had to be aiohttp, the others do not reproduce this problem. The aiohttp web server is indeed sending a number right before ending the connection.

I still don't know if the aiohttp server has the problem, or the websocket client that I'm using. This number is a code that indicates that the connection is ending, but it is unclear why it is being sent or received as a normal message and not as connection metadata. Need to investigate this in more detail.

@miguelgrinberg
Copy link
Owner

@thoquine can I ask you to install the master branch for the python-engineio repository and retest? Thanks!

@thoquine
Copy link

thoquine commented Apr 11, 2020

@miguelgrinberg Yeah sure! I will do it this week and come back with the results.
Thanks!!

@bhakta0007
Copy link

bhakta0007 commented Apr 20, 2020

I hit this issue as well (can reproduce it reliably in my environment). I will try out the master branch of python-engineio repo..

Edit: Tested and works

In case it helps anyone else coming here. The problem in my case was due to asyncio client calling a "subprocess.popen / p.communicate()" that took about 60 seconds to complete. The client was disconnected after the subprocess was complete and resulted in this issue.

I had to move over to async version of subprocess.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants