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

Edge case uncaught exception in fjagepy send #321

Open
impala454 opened this issue Jul 29, 2024 · 2 comments
Open

Edge case uncaught exception in fjagepy send #321

impala454 opened this issue Jul 29, 2024 · 2 comments
Assignees

Comments

@impala454
Copy link

We encountered an issue with fjagepy where, when sending a message via transport. We think this happened when the modem was disconnected during a send operation and the offending line is here. Trace looked like this:

[action_agent-1]     self.transport.send(datagram_req)
[action_agent-1]   File "/usr/local/lib/python3.10/dist-packages/fjagepy/__init__.py", line 147, in send
[action_agent-1]     self.owner.send(msg)
[action_agent-1]   File "/usr/local/lib/python3.10/dist-packages/fjagepy/__init__.py", line 760, in send
[action_agent-1]     self.socket.sendall((rq + '\n').encode())
[action_agent-1] BrokenPipeError: [Errno 32] Broken pipe

Looking at that code it appears it's using the standard Python socket.sendall() which says it will raise an Exception on error and return None on success. So perhaps a check here would be good before returning True so the user doesn't need to catch the underlying socket exceptions.

@notthetup
Copy link
Collaborator

Thanks @impala454. Let me take a look at this.

@impala454
Copy link
Author

Here's a sample where I was able to reproduce this most of the time. Steps:

  • change your IP/port to whatever your modem is
  • power modem on until it's booted
  • run the script
  • power modem off
  • script will eventually indefinitely hang at sending xxx (the transport.send() call)
  • power modem back on
  • once modem is booted back up the script will resume sending the same number XXX indicated in the hung call but fail with an exception which is caught by the script
  • the script will automatically reconnect but on some runs an exception is printed to the screen but not re-raised or returned to the transport.send() call: Exception: [Errno 107] Transport endpoint is not connected .

The script:

#!/usr/bin/env python3

from time import sleep
from fjagepy import Gateway
from unetpy import TxFrameReq

modem_ip_address = "192.168.10.38"
modem_port = 1100
remote_modem_addr = 58
gateway = None
transport = None
reconnect = True
counter = 0

while True:
    if not gateway:
        reconnect = True
    elif not gateway.isConnected():
        print('disconnected')
        reconnect = True

    if reconnect:
        try:
            print('connecting')
            gateway = Gateway(hostname=modem_ip_address, port=modem_port)
        except Exception as ex:
            print(f'connect failed: {ex}')
            sleep(0.1)
            continue
        else:
            transport = gateway.agent('transport')
            reconnect = False
            print('connected')

    req = TxFrameReq()
    req.type = 2
    req.to = remote_modem_addr
    req.data = list(bytearray('\x00\x23\x04\x54\x65\x13\x05\x36\x76\x31\x12\x13\x07', encoding='utf-8'))

    print(f'sending {counter}')
    try:
        transport.send(req)
    except Exception as ex:
        print(f'lockup.py exception {counter}: {ex}')
        gateway.close()
        counter += 1
    else:
        print(f'sent {counter}')
        counter += 1

    sleep(0.1)

The main concerns here are two things:

  • The gateway continues to report as connected and also the send call returns None and doesn't re-raise any exceptions, so the user has no way of knowing the modem is disconnected.
  • The case where the Transport endpoint is not connected Exception occurs and is printed doesn't make it back to the user in any way except via the console print. So user software thinks the send() succeeded.

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