mik3y / usb-serial-for-android

Android USB host serial driver library for CDC, FTDI, Arduino and other devices.
MIT License
4.9k stars 1.59k forks source link

SerialInputOutputManager potential deadlock with no read timout set #441

Open rocketraman opened 2 years ago

rocketraman commented 2 years ago

The SerialInputOutputManager currently potentially does an no-timeout read (depending on the timeout settings) before write in each step. If the read data depends on something being written, this results in a deadlock.

kai-morich commented 2 years ago

this is already documented as limitation in the code: https://github.com/mik3y/usb-serial-for-android/blob/5bb8db02d5a6f2db32522ce602a6dd7d791de083/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/util/SerialInputOutputManager.java#L147 I recommend to do writing directly in your worker thread

rocketraman commented 2 years ago

I recommend to do writing directly in your worker thread

That's totally understood -- in fact I ended up dumping the SerialInputOutputManager and doing both writes and reads myself to keep things consistent and clean, and I prefer doing the reads and writes in separate threads, and asynchronously via coroutines. Now I am just using usb-serial-for-android to do the serial-specific stuff like line initialization, for which it has been invaluable!

However, the point of this issue was for future users of this library to not have to take this step by changing SerialInputOutputManager to avoid the potential read/write deadlock -- why not use separate reader and writer threads in SerialInputOutputManager?