From ea3c593776a206d9eb2281f846c80a99063de5df Mon Sep 17 00:00:00 2001 From: Will Hedgecock Date: Wed, 16 Feb 2022 16:14:47 -0600 Subject: [PATCH] Ensure that event listener thread does not join on itself --- .../com/fazecast/jSerialComm/SerialPort.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/fazecast/jSerialComm/SerialPort.java b/src/main/java/com/fazecast/jSerialComm/SerialPort.java index 5216496..78a4813 100644 --- a/src/main/java/com/fazecast/jSerialComm/SerialPort.java +++ b/src/main/java/com/fazecast/jSerialComm/SerialPort.java @@ -1758,17 +1758,18 @@ public final class SerialPort eventListenerRunning = false; configTimeouts(portHandle, TIMEOUT_NONBLOCKING, 0, 0, 0); setEventListeningStatus(portHandle, false); - + + // Wait until the event-reading thread returns. This thread MUST return or the serial port will + // be in an unspecified, possibly unrecoverable state try { - // Wait until the event-reading thread returns. This thread MUST return or the serial port will - // be in an unspecified, possibly unrecoverable state - do - { - serialEventThread.join(500); - if (serialEventThread.isAlive()) - serialEventThread.interrupt(); - } while (serialEventThread.isAlive()); + if (!Thread.currentThread().equals(serialEventThread)) + do + { + serialEventThread.join(500); + if (serialEventThread.isAlive()) + serialEventThread.interrupt(); + } while (serialEventThread.isAlive()); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } serialEventThread = null;