Skip to content

Commit 17aad56

Browse files
committed
more guarding
1 parent cbb5a9f commit 17aad56

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/main/java/jssc/SerialPort.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,9 @@ public synchronized boolean removeEventListener() throws SerialPortException {
11571157
return false;
11581158
}
11591159
eventThread.terminateThread();
1160-
setEventsMask(0);
1160+
if (portOpened) {
1161+
setEventsMask(0);
1162+
}
11611163
//Guard against currentThread().join deadlock
11621164
if(Thread.currentThread().getId() != eventThread.getId()){
11631165
try {
@@ -1182,17 +1184,20 @@ public synchronized boolean removeEventListener() throws SerialPortException {
11821184
* @throws SerialPortException if exception occurred
11831185
*/
11841186
public synchronized boolean closePort() throws SerialPortException {
1185-
if (!portOpened) return false;
11861187
boolean returnValue;
11871188
//removeEventListener calls setEventsMask, and must occur before calling closePort
11881189
try {
11891190
removeEventListener();
11901191
}
11911192
finally {
1192-
returnValue = serialInterface.closePort(portHandle);
1193-
if (returnValue) {
1194-
maskAssigned = false;
1195-
portOpened = false;
1193+
if (portOpened) {
1194+
returnValue = serialInterface.closePort(portHandle);
1195+
if (returnValue) {
1196+
maskAssigned = false;
1197+
portOpened = false;
1198+
}
1199+
} else {
1200+
returnValue = false;
11961201
}
11971202
}
11981203
return returnValue;

0 commit comments

Comments
 (0)