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

client.connect has inconsistent behavior #109

Open
IonCaza opened this issue Jun 29, 2024 · 3 comments
Open

client.connect has inconsistent behavior #109

IonCaza opened this issue Jun 29, 2024 · 3 comments

Comments

@IonCaza
Copy link

IonCaza commented Jun 29, 2024

Hey there, I'm working on a little software KVM for my 3x C3 42". Here's my retry logic when trying to connect to my TVs:

def connectTVs(config):
    #TODO : Make sure TVs are on
    TVClients.clear()
    for tv in config['TVs']:
        client = WebOSClient(tv['ip'], secure=True)
        client.name = tv['name']
        keyString = {"client_key": tv['client_key']}
        
        retry_count = 0
        while retry_count < 3:
            try:
                client.connect()
                break
            except Exception as e:
                    retry_count += 1
                    logging.info(f"Connection attempt in connectTVs on count {retry_count} for {tv['name']} failed: {str(e)}")

        retry_count = 0
        while retry_count < 3:
            try:
                for status in client.register(keyString):
                    logging.info(f"{tv['name']} registered and logged in | status {status}")
                break
            except Exception as e:
                    retry_count += 1
                    logging.info(f"Connection attempt in connectTVs on count {retry_count} for {tv['name']} failed: {str(e)}")
  
        if retry_count == 3: sys.exit()

        TVClients.append(client)
    logging.info(TVClients)

This is my registration logic:

def registerTVs(config):
    tvCreds = {}

    for tv in config['TVs']:
        retry_count = 0 
                       
        if "client_key" not in tv or not tv['client_key']:
            while retry_count < 3:
                try:
                    client = WebOSClient(tv['ip'], secure=True)
                    client.connect()
                    for status in client.register(tvCreds):
                        if status == WebOSClient.PROMPTED:
                            logging.info("Please accept the connect on the TV!")
                        elif status == WebOSClient.REGISTERED:
                            logging.info("Registration successful!")
                    tv.update(tvCreds)
                    break
                except ConnectionResetError as e:
                    retry_count += 1
                    logging.info(f"Connection attempt in registerTVs on count {retry_count} failed: {str(e)}")
    return config

Every once in a while one of them fails to connect even though I try multiple times. Here's what that looks like:

INFO:root:Connection attempt in connectTVs on count 1 for RightMonitor failed: [Errno 54] Connection reset by peer
INFO:root:Connection attempt in connectTVs on count 2 for RightMonitor failed: [Errno 22] Invalid argument
INFO:root:Connection attempt in connectTVs on count 3 for RightMonitor failed: [Errno 9] Bad file descriptor
INFO:root:Connection attempt in connectTVs on count 1 for RightMonitor failed: [Errno 9] Bad file descriptor
INFO:root:Connection attempt in connectTVs on count 2 for RightMonitor failed: [Errno 9] Bad file descriptor
INFO:root:Connection attempt in connectTVs on count 3 for RightMonitor failed: [Errno 9] Bad file descriptor

Any ideas why this is happening? Most of the times, hitting that logic again works without issues.

@supersaiyanmode
Copy link
Owner

  1. connect(..) is meant to be a one-time-use API. Please create a new object via WebOSClient(..) when attempting to reconnect.

  2. Connection reset -- looks like your TV is closing the socket?

@IonCaza
Copy link
Author

IonCaza commented Jul 4, 2024

Copy that, I'll move the object creation inside the try block. Is register also meant to be performed once? If it fails, do I have to recreate the client object?

@supersaiyanmode
Copy link
Owner

Yes, even register(..) is meant to be performed only one per connection (WebOSClient is just a connection client instance from ws4py)

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

No branches or pull requests

2 participants