Pi4J / pi4j-v1

DEPRECATED Java I/O library for Raspberry Pi (GPIO, I2C, SPI, UART)
http://www.pi4j.com
Apache License 2.0
1.31k stars 447 forks source link

Bad exception thrown when interrupted in I2C read #407

Open RenWal opened 6 years ago

RenWal commented 6 years ago

Hello!

There is an issue with the Pi4J I2C implementation that keeps crashing my application on shutdown: Whenever there is an I2C read waiting to obtain the bus access lock and the thread is interrupted, the I2CBusImpl class catches the InterruptedException thrown by the tryLock() method and wraps it in a RuntimeException. Unexpecting callers will have no idea what has just happened, probably not have a good handler for it or even just crash the thread entirely with an uncaught runtime exception. Instead, after logging, the InterruptedException should just be re-thrown, so that callers clearly see that the method has failed because of an interrupt and can act accordingly.

https://github.com/Pi4J/pi4j/blob/f4a3f790a3be9d419d89525cedadffbad602a9c4/pi4j-core/src/main/java/com/pi4j/io/i2c/impl/I2CBusImpl.java#L267

Workaround is to put a catch block for RuntimeException on the caller's side and then check if getCause() returns an object of type InterruptedException.

savageautomate commented 3 years ago

Reviewing this issue for v1.4 release. While I agree the exception probably should not be caught internally and masked as a runtime exception, changing this behavior would be a breaking API change for most of the I2CDevice methods.

Postponing fix to version 2.0 where breaking API changes are planned.