pylessard / python-udsoncan

Python implementation of UDS (ISO-14229) standard.
MIT License
575 stars 199 forks source link

IsoTPSocketConnection can not be closed with tpsock in blocking mode #191

Closed bacarz closed 7 months ago

bacarz commented 9 months ago

Closing IsoTPSocketConnection stucks on self.rxthread.join() when self.tpsock is created in blocking mode:

    tpsock = isotp.socket(timeout=0)
    connector = IsoTPSocketConnection(interface=interface, rxid=endpoint.tx, txid=endpoint.rx, tpsock=tpsock)

rxthread_task use directly socket.recv by self.tpsock.recv() . Is it possible to use selector?

    def rxthread_task(self):
        sel = selectors.DefaultSelector()
        sel.register(self.tpsock, selectors.EVENT_READ, data=None)
        while not self.exit_requested:
            events = sel.select(timeout=1)
            for _, _ in events:
                try:
                    data = self.tpsock.recv()
                    if data is not None:
                        self.rxqueue.put(data)
                except socket.timeout:
                    pass
                except Exception:
                    self.exit_requested = True

Or it would be better to make some changes to isotp.socket.recv directly?

pylessard commented 9 months ago

Good point. It can certainly be improved to use a selector. I am actually reworking that section of code, will keep that in mind. thanks

pylessard commented 7 months ago

Want to try this branch? https://github.com/pylessard/python-udsoncan/tree/use-selector-in-socket-connection

bacarz commented 7 months ago

Tested. Looks good.

nada-ben-ali commented 4 months ago

@pylessard After adding the selectors in IsoTPSocketConnection, I think it won't work when using custom sockets that don't have a file descriptor, so events will always be None, and so self.rxqueue will always remain empty, which is the case for me.

pylessard commented 4 months ago

Why would a socket have no file descriptor? Are you able to provide a reproducing code?