sjlongland / aioax25

Asynchronous AX.25 library using asyncio
GNU General Public License v2.0
21 stars 8 forks source link

Exception handling help? #19

Open cidrblock opened 7 months ago

cidrblock commented 7 months ago

Hey there,

Thanks for making this project. I'm trying to write a little chat app over AX.25 and was trying to catch to errors.

The first I figured out, if direwolf isn't running the device never transitons to OPEN, so I can catch that:

while self.device.state != KISSDeviceState.OPEN:
            msg = f"Waiting for direwolf connection... attempt {i}/{max_attempts}"
            print(msg, end="\r")  # noqa: T201
            if i > max_attempts:
                msg = "Cannot connect to direwolf. Is it running?"
                LOGGER.critical(msg)
                sys.exit(1)

            await asyncio.sleep(1)
            i += 1

The second, which I cannot figure out, is if direwolf disappears after the device was open.

raw_frame = AX25RawFrame(
                destination=dest,
                source=self.callsign,
                control=0,
                payload=payload,
            )
interface.transmit(raw_frame)

In this case if direwolf is no longer running I get the following:

Unhandled exception in event loop:
  File "/usr/lib64/python3.12/asyncio/events.py", line 84, in _run
    self._context.run(self._callback, *self._args)
  File "/home/bthornto/github/ax25/venv/lib64/python3.12/site-packages/aioax25/kiss.py", line 321, in _send_data
    self._send_raw_data(data)
  File "/home/bthornto/github/ax25/venv/lib64/python3.12/site-packages/aioax25/kiss.py", line 448, in _send_raw_data
    self._transport.write(data)
    ^^^^^^^^^^^^^^^^^^^^^

Exception 'NoneType' object has no attribute 'write'

Any suggestions or pointers on how to catch this one? I would like to gracefully exit the app when it happens.

Thanks for any info/suggestions,

Brad

sjlongland commented 7 months ago

Ahh good question, see the connection is not meant to disappear, so there's been no real thought into that sort of error handling.

Most of the effort has gone towards actually getting the AX.25 protocol stack working.

cidrblock commented 7 months ago

Thanks for the response. If I get a chance to dig in a little deeper I'll see if I can help here.

I'm still chipping away at my project, hope to have something shareable soon.

-Brad

sjlongland commented 4 months ago

Sorry for the long delay… could you have a look at https://github.com/sjlongland/aioax25/tree/bugfix/issue19-missing-device and see if that helps catch the error and deal with it?