NeuronRobotics / nrjavaserial

A Java Serial Port system. This is a fork of the RXTX project that uses in jar loading of the native code.
Other
345 stars 143 forks source link

Fix IllegalMonitorStateException when thread closing port is interrupted #145

Closed wborn closed 5 years ago

wborn commented 5 years ago

When a thread is closing the port it may not have obtained the writeLock when it is interrupted. So it should only unlock the writeLock in the finally clause when the thread is holding that lock. This fixes an IllegalMonitorStateException that would be thrown if the thread does not hold the lock.

An example stacktrace is:

java.lang.IllegalMonitorStateException: null
    at java.util.concurrent.locks.ReentrantReadWriteLock$Sync.tryRelease(ReentrantReadWriteLock.java:371) ~[?:1.8.0_111]
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1261) ~[?:1.8.0_111]
    at java.util.concurrent.locks.ReentrantReadWriteLock$WriteLock.unlock(ReentrantReadWriteLock.java:1131) ~[?:1.8.0_111]
    at gnu.io.RXTXPort.close(RXTXPort.java:1101) ~[nrjavaserial-3.15.0.jar:3.15.0]
wborn commented 5 years ago

To fix the Travis CI build I've created PR #146.

madhephaestus commented 5 years ago

Thank you!