Skip to content

Commit 026355f

Browse files
committed
terminate SerialInputOutputManager write thread if read thread terminates (e.g. port closed)
1 parent 1501745 commit 026355f

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

usbSerialForAndroid/src/androidTest/java/com/hoho/android/usbserial/DeviceTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,17 @@ public void IoManager() throws Exception {
14851485
assertFalse("iomanager threads", usb.hasIoManagerThreads());
14861486
assertNull(usb.ioManager);
14871487
assertEquals(SerialInputOutputManager.State.STOPPED, ioManager.getState());
1488+
1489+
usb.open();
1490+
ioManager = usb.ioManager;
1491+
assertEquals(SerialInputOutputManager.State.RUNNING, usb.ioManager.getState());
1492+
usb.serialPort.close(); // stop before ioManager
1493+
for (int i = 0; i < 100 && usb.hasIoManagerThreads(); i++) {
1494+
Thread.sleep(1);
1495+
}
1496+
assertFalse("iomanager threads", usb.hasIoManagerThreads());
1497+
assertEquals(SerialInputOutputManager.State.STOPPED, usb.ioManager.getState());
1498+
14881499
SerialInputOutputManager.DEBUG = false;
14891500
}
14901501

usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/util/SerialInputOutputManager.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public void start() {
183183
public void stop() {
184184
if(mState.compareAndSet(State.RUNNING, State.STOPPING)) {
185185
synchronized (mWriteBufferLock) {
186-
mWriteBufferLock.notifyAll(); // Wake up any waiting thread to check the stop condition
186+
mWriteBufferLock.notifyAll(); // wake up write thread to check the stop condition
187187
}
188188
Log.i(TAG, "Stop requested");
189189
}
@@ -250,10 +250,12 @@ void runRead() {
250250
}
251251
notifyErrorListener(e);
252252
} finally {
253-
if (!mState.compareAndSet(State.RUNNING, State.STOPPING)) {
254-
if (mState.compareAndSet(State.STOPPING, State.STOPPED)) {
255-
Log.i(TAG, "runRead: Stopped mState=" + getState());
253+
if (mState.compareAndSet(State.RUNNING, State.STOPPING)) {
254+
synchronized (mWriteBufferLock) {
255+
mWriteBufferLock.notifyAll(); // wake up write thread to check the stop condition
256256
}
257+
} else if (mState.compareAndSet(State.STOPPING, State.STOPPED)) {
258+
Log.i(TAG, "runRead: Stopped mState=" + getState());
257259
}
258260
}
259261
}

0 commit comments

Comments
 (0)