1
0
mirror of https://github.com/mik3y/usb-serial-for-android synced 2025-06-06 23:46:33 +00:00

terminate SerialInputOutputManager write thread if read thread terminates (e.g. port closed)

This commit is contained in:
Kai Morich 2025-02-15 18:02:15 +01:00
parent 150174573c
commit 026355f61e
2 changed files with 17 additions and 4 deletions

View File

@ -1485,6 +1485,17 @@ public class DeviceTest {
assertFalse("iomanager threads", usb.hasIoManagerThreads());
assertNull(usb.ioManager);
assertEquals(SerialInputOutputManager.State.STOPPED, ioManager.getState());
usb.open();
ioManager = usb.ioManager;
assertEquals(SerialInputOutputManager.State.RUNNING, usb.ioManager.getState());
usb.serialPort.close(); // stop before ioManager
for (int i = 0; i < 100 && usb.hasIoManagerThreads(); i++) {
Thread.sleep(1);
}
assertFalse("iomanager threads", usb.hasIoManagerThreads());
assertEquals(SerialInputOutputManager.State.STOPPED, usb.ioManager.getState());
SerialInputOutputManager.DEBUG = false;
}

View File

@ -183,7 +183,7 @@ public class SerialInputOutputManager {
public void stop() {
if(mState.compareAndSet(State.RUNNING, State.STOPPING)) {
synchronized (mWriteBufferLock) {
mWriteBufferLock.notifyAll(); // Wake up any waiting thread to check the stop condition
mWriteBufferLock.notifyAll(); // wake up write thread to check the stop condition
}
Log.i(TAG, "Stop requested");
}
@ -250,10 +250,12 @@ public class SerialInputOutputManager {
}
notifyErrorListener(e);
} finally {
if (!mState.compareAndSet(State.RUNNING, State.STOPPING)) {
if (mState.compareAndSet(State.STOPPING, State.STOPPED)) {
Log.i(TAG, "runRead: Stopped mState=" + getState());
if (mState.compareAndSet(State.RUNNING, State.STOPPING)) {
synchronized (mWriteBufferLock) {
mWriteBufferLock.notifyAll(); // wake up write thread to check the stop condition
}
} else if (mState.compareAndSet(State.STOPPING, State.STOPPED)) {
Log.i(TAG, "runRead: Stopped mState=" + getState());
}
}
}