From 3340f9bd5b00ec1d1f7e49448e1368d6b45d1b34 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 16 Jul 2026 10:33:18 +0000 Subject: [PATCH 1/7] DEV-895 Stop per-device reader threads from busy-spinning when idle ShimmerBluetooth.IOThread and LiteProtocol.IOThread ran tight while(!stop) loops with no sleep, calling processNextInstruction()/bytesAvailableToBeRead() as fast as possible, pegging a CPU core per connected device even when idle. Track whether an iteration did useful work (instruction processed, actively streaming, or bytes read) and only sleep briefly (2ms) when it didn't - latency of the active/streaming paths is unchanged since sleep is skipped whenever there's real work to do. ShimmerBluetooth.ProcessingThread spun on mABQPacketByeArray.isEmpty(); since the queue is an ArrayBlockingQueue, swap to a blocking poll(100ms) instead so the thread parks while idle rather than busy-checking emptiness. Also replace two teardown busy-waits with bounded Thread.join() calls so closing a connection doesn't spin a core while waiting for the reader threads to exit: ShimmerPC.closeConnection() (mIOThread + mPThread) and LiteProtocol.stopIoThread() (mIOThread). Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01JjgNkS6wtPkQUdK3KdHVdN --- .../bluetooth/ShimmerBluetooth.java | 60 +++++++++++++------ .../comms/radioProtocol/LiteProtocol.java | 51 ++++++++++++---- .../shimmerresearch/pcDriver/ShimmerPC.java | 16 ++++- 3 files changed, 94 insertions(+), 33 deletions(-) diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java b/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java index db2beb4d1..b3e1ae8f9 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java @@ -581,19 +581,26 @@ public class ProcessingThread extends Thread { int count=0; public void run() { while (!stop) { - if(!mABQPacketByeArray.isEmpty()){ + RawBytePacketWithPCTimeStamp rbp = null; + try { + // Blocks (with a timeout) instead of busy-spinning on isEmpty() while idle. + // The timeout ensures the stop flag is still checked periodically. + rbp = mABQPacketByeArray.poll(100, TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + if(rbp!=null){ count++; if(count%1000==0){ consolePrintLn("Queue Size: " + mABQPacketByeArray.size()); printLogDataForDebugging("Queue Size: " + mABQPacketByeArray.size() + "\n"); } - RawBytePacketWithPCTimeStamp rbp = mABQPacketByeArray.remove(); // buildAndSendMsg(rbp.mDataArray, FW_TYPE_BT, false, rbp.mSystemTimeStamp); buildAndSendMsg(rbp.mDataArray, COMMUNICATION_TYPE.BLUETOOTH, false, rbp.mSystemTimeStamp); } - } - } - } + } + } + } //region --------- BLUETOOH STACK --------- @@ -603,9 +610,11 @@ public class IOThread extends Thread { public void run() { while(!stop) { + boolean didWork = false; //In-Shimmer Test here if(InShimmerTest) { if (bytesAvailableToBeRead()) { + didWork = true; byte[] data = readBytes(availableBytes()); if (data!=null) { if (data.length>0) { @@ -619,29 +628,43 @@ public void run() { else { if (mDummyRead) { performDummyRead(); + didWork = true; } - + // Process Instruction on stack. is an instruction running? if not proceed if(!isInstructionStackLock()){ - processNextInstruction(); + if(processNextInstruction()){ + didWork = true; + } } - + if(mIsStreaming){ processWhileStreaming(); + didWork = true; } else if(bytesAvailableToBeRead()){ + didWork = true; if(mWaitForAck) { processNotStreamingWaitForAck(); - } + } else if(mWaitForResponse) { processNotStreamingWaitForResp(); - } - + } + processBytesAvailableAndInstreamSupported(); - + + } + } + + if(!didWork){ + // Nothing useful happened this iteration (idle, not streaming, no pending + // instructions/bytes) - avoid busy-spinning and pegging a CPU core. + try { + Thread.sleep(2); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); } } - } } @@ -674,10 +697,11 @@ private void performDummyRead() { mDummyReadStarted = false; } - private void processNextInstruction() { + /** @return true if an instruction was found and processed (i.e. useful work was done) */ + private boolean processNextInstruction() { // check instruction stack, are there any other instructions left to be executed? //checkAndRemoveFirstInstructionIfNull(); - + byte[] insBytes = getInstruction(); if (insBytes!=null){ mCurrentCommand=insBytes[0]; @@ -754,14 +778,16 @@ else if((mCurrentCommand==GET_FW_VERSION_COMMAND) mTransactionCompleted=false; + return true; } else { if (!mIsStreaming && !bytesAvailableToBeRead()){ threadSleep(50); } + return false; } } - - /** Process ACK from a GET or SET command while not streaming */ + + /** Process ACK from a GET or SET command while not streaming */ private void processNotStreamingWaitForAck() { //JC TEST:: IMPORTANT TO REMOVE // This is to simulate packet loss /* diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java b/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java index dc1634651..04ec411e7 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java @@ -255,6 +255,13 @@ private void startIoThread() { private void stopIoThread() { if(mIOThread!=null){ mIOThread.stop=true; + // Bounded wait for the thread to actually exit its loop before dropping the + // reference, without risking blocking indefinitely. + try { + mIOThread.join(2000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } mIOThread = null; } } @@ -266,36 +273,52 @@ public class IOThread extends Thread { public synchronized void run() { while (!stop) { + boolean didWork = false; try { // Process Instruction on stack. is an instruction running? if not proceed if(!isInstructionStackLock()){ - processNextInstruction(); + if(processNextInstruction()){ + didWork = true; + } } - + if(isStreaming()){ processWhileStreaming(); + didWork = true; } else if(bytesAvailableToBeRead()){ + didWork = true; if(mWaitForAck) { processNotStreamingWaitForAck(); - } + } else if(mWaitForResponse) { processNotStreamingWaitForResp(); - } - + } + processBytesAvailableAndInstreamSupported(); } } catch (ShimmerException dE) { // stop=true; - + killConnection(dE); // e.printStackTrace(); //TODO send event up the ladder } - } - } - - private void processNextInstruction() throws ShimmerException { + + if(!didWork){ + // Nothing useful happened this iteration (idle, not streaming, no pending + // instructions/bytes) - avoid busy-spinning and pegging a CPU core. + try { + Thread.sleep(2); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + } + } + + /** @return true if an instruction was found and processed (i.e. useful work was done) */ + private boolean processNextInstruction() throws ShimmerException { // check instruction stack, are there any other instructions left to be executed? if(!getListofInstructions().isEmpty()) { if(getListofInstructions().get(0)==null) { @@ -387,16 +410,18 @@ else if((mCurrentCommand==InstructionsGet.GET_FW_VERSION_COMMAND_VALUE) } }*/ } - - + + mTransactionCompleted=false; + return true; } - } + } else { if (!isStreaming() && !bytesAvailableToBeRead()){ threadSleep(50); } } + return false; } private void processWhileStreaming() throws ShimmerException { diff --git a/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java b/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java index eda410989..d6e0170fa 100644 --- a/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java +++ b/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java @@ -608,13 +608,23 @@ private void closeConnection(){ if (mIOThread != null) { mIOThread.stop = true; - // Closing serial port before before thread is finished stopping throws an error so waiting here - while(mIOThread != null && mIOThread.isAlive()); + // Closing serial port before before thread is finished stopping throws an error so waiting here. + // Bounded join instead of an empty-body busy-wait, so this can't block indefinitely. + try { + mIOThread.join(2000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } mIOThread = null; - + if(mUseProcessingThread){ mPThread.stop = true; + try { + mPThread.join(2000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } mPThread = null; } } From 050410c4f9b21875f9db51f5e7c0e385c3a2297e Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 16 Jul 2026 11:28:42 +0000 Subject: [PATCH 2/7] DEV-895 review fixes: self-join guards, un-synchronize LiteProtocol run(), interrupt-exit loops Review findings on the idle-sleep changeset: - LiteProtocol.IOThread.run() was declared synchronized; Thread.join() locks the same Thread instance, so external stopIoThread() callers blocked on monitor entry with no timeout until the loop exited, defeating the bounded join. run() is no longer synchronized. - stopIoThread()/closeConnection() can be reached synchronously from the reader thread itself (killConnection from IOThread's catch; connectionLost on serial errors), where join() can never succeed and just burns the full 2s timeout. Skip the join when running on that thread. - Reader loops now also exit on thread interrupt: previously an interrupt without stop=true would make poll()/sleep() throw immediately every iteration - a latent busy-spin. Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01JjgNkS6wtPkQUdK3KdHVdN --- .../bluetooth/ShimmerBluetooth.java | 4 ++-- .../comms/radioProtocol/LiteProtocol.java | 23 +++++++++++++------ .../shimmerresearch/pcDriver/ShimmerPC.java | 13 +++++++---- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java b/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java index b3e1ae8f9..6df67e805 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java @@ -580,7 +580,7 @@ public class ProcessingThread extends Thread { public boolean stop = false; int count=0; public void run() { - while (!stop) { + while (!stop && !Thread.currentThread().isInterrupted()) { RawBytePacketWithPCTimeStamp rbp = null; try { // Blocks (with a timeout) instead of busy-spinning on isEmpty() while idle. @@ -609,7 +609,7 @@ public class IOThread extends Thread { public boolean stop = false; public void run() { - while(!stop) { + while(!stop && !Thread.currentThread().isInterrupted()) { boolean didWork = false; //In-Shimmer Test here if(InShimmerTest) { diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java b/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java index 04ec411e7..5103754ec 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java @@ -256,11 +256,15 @@ private void stopIoThread() { if(mIOThread!=null){ mIOThread.stop=true; // Bounded wait for the thread to actually exit its loop before dropping the - // reference, without risking blocking indefinitely. - try { - mIOThread.join(2000); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); + // reference. Guard against the self-join case: killConnection() can reach here + // synchronously from within IOThread.run()'s catch block (error-driven teardown), + // and a thread joining itself would just burn the full timeout. + if(Thread.currentThread() != mIOThread){ + try { + mIOThread.join(2000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } } mIOThread = null; } @@ -271,8 +275,13 @@ public class IOThread extends Thread { byte[] byteBuffer = {0}; public boolean stop = false; - public synchronized void run() { - while (!stop) { + /* + * Deliberately NOT synchronized: Thread.join() locks this same Thread + * instance, so a synchronized run() would block external stopIoThread() + * callers on monitor entry (with no timeout) until the loop exits. + */ + public void run() { + while (!stop && !Thread.currentThread().isInterrupted()) { boolean didWork = false; try { // Process Instruction on stack. is an instruction running? if not proceed diff --git a/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java b/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java index d6e0170fa..194a33ba6 100644 --- a/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java +++ b/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java @@ -610,10 +610,15 @@ private void closeConnection(){ // Closing serial port before before thread is finished stopping throws an error so waiting here. // Bounded join instead of an empty-body busy-wait, so this can't block indefinitely. - try { - mIOThread.join(2000); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); + // Skip the join when closeConnection() is reached from the IOThread itself + // (connectionLost() fires on it when the serial port errors mid-read/write) - + // a self-join can never succeed and would just burn the full timeout. + if(Thread.currentThread() != mIOThread){ + try { + mIOThread.join(2000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } } mIOThread = null; From d3054979e641664c2cd93db8b5252bffa6208671 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 4 Sep 2026 06:46:46 +0000 Subject: [PATCH 3/7] DEV-895 Avoid stacking idle sleep on processNextInstruction backoff processNextInstruction()'s no-instruction branch already waits 50ms (threadSleep(50)) when idle (no instruction pending, not streaming, no bytes available). The new outer idle-sleep in IOThread.run() added a further Thread.sleep(2) whenever didWork stayed false, so the fully idle case waited 52ms instead of 50ms. Fix: have that branch report true (i.e. "waited") instead of false, so run()'s outer idle-sleep is skipped for the iteration that already paced itself. This is the minimal change - it keeps the pre-existing 50ms idle cadence exactly as before this PR, rather than replacing it with the new 2ms cadence (a bigger behavioural change) or leaving the two sleeps stacked. The 50ms sleep only fires when there is no instruction on the stack at all, so it has no bearing on pacing retries for an in-flight instruction - removing it in favour of the 2ms sleep was rejected because that would make idle polling 25x more frequent than before. Applied identically to ShimmerBluetooth.java and LiteProtocol.java, with matching javadoc updates on processNextInstruction(). Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_015q29AEJZbwa7aVYeAj61Py --- .../shimmerresearch/bluetooth/ShimmerBluetooth.java | 11 ++++++++++- .../comms/radioProtocol/LiteProtocol.java | 11 ++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java b/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java index 044183e85..0dea4087c 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java @@ -697,7 +697,12 @@ private void performDummyRead() { mDummyReadStarted = false; } - /** @return true if an instruction was found and processed (i.e. useful work was done) */ + /** + * @return true if an instruction was found and processed (i.e. useful work was done), + * or if this call already idle-paced the loop with its own threadSleep(50) below - + * in that case reporting true here stops run()'s outer idle-sleep from stacking an + * additional wait on top of this one. + */ private boolean processNextInstruction() { // check instruction stack, are there any other instructions left to be executed? //checkAndRemoveFirstInstructionIfNull(); @@ -781,7 +786,11 @@ else if((mCurrentCommand==GET_FW_VERSION_COMMAND) return true; } else { if (!mIsStreaming && !bytesAvailableToBeRead()){ + // No instruction pending and nothing else to do - wait here rather than + // busy-spinning. Report "did work" (true) so the caller's own idle sleep + // in run() doesn't stack an extra wait on top of this one. threadSleep(50); + return true; } return false; } diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java b/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java index 5103754ec..54b6af0ca 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java @@ -326,7 +326,12 @@ else if(mWaitForResponse) { } } - /** @return true if an instruction was found and processed (i.e. useful work was done) */ + /** + * @return true if an instruction was found and processed (i.e. useful work was done), + * or if this call already idle-paced the loop with its own threadSleep(50) below - + * in that case reporting true here stops run()'s outer idle-sleep from stacking an + * additional wait on top of this one. + */ private boolean processNextInstruction() throws ShimmerException { // check instruction stack, are there any other instructions left to be executed? if(!getListofInstructions().isEmpty()) { @@ -427,7 +432,11 @@ else if((mCurrentCommand==InstructionsGet.GET_FW_VERSION_COMMAND_VALUE) } else { if (!isStreaming() && !bytesAvailableToBeRead()){ + // No instruction pending and nothing else to do - wait here rather than + // busy-spinning. Report "did work" (true) so the caller's own idle sleep + // in run() doesn't stack an extra wait on top of this one. threadSleep(50); + return true; } } return false; From 30462c83dc9c2f51f2b235d090c7efe0be46165e Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 4 Sep 2026 06:59:07 +0000 Subject: [PATCH 4/7] DEV-895 Harden bounded teardown joins: null-safe snapshots, guard ProcessingThread self-join, volatile stop Round-2 review findings on the bounded-join teardown from the previous commit: - ShimmerPC.closeConnection() and ShimmerGRPC.closeConnection() are unsynchronized and can run concurrently (connectionLost() on the IOThread itself vs. a user-triggered disconnect() on another thread). Both dereferenced mIOThread/mPThread after only a non-atomic null check, so a concurrent caller nulling the field out mid-method could turn the passed check into an NPE - swallowed by the outer catch(Exception), skipping purgePort/closePort and leaking the COM port. Fixed by snapshotting mIOThread/mPThread into locals once and operating on the snapshot throughout. - Added the same self-join guard used for mIOThread to mPThread in both classes: buildAndSendMsg() runs on the ProcessingThread and dispatches to app listeners, so a listener calling disconnect() would otherwise self-join and burn the full 2s timeout. - After each bounded join(2000), if the thread is still alive, log a warning and call interrupt() so the thread's own interrupt checks can finish tearing it down, instead of silently closing the port under a still-live reader. Applied to mIOThread/mPThread in ShimmerPC.closeConnection() and ShimmerGRPC.closeConnection(), and to mIOThread in LiteProtocol.stopIoThread(). - ShimmerGRPC.closeConnection() previously did an unbounded join() with no self-join guard; converted to the same bounded snapshot+guard+alive-check pattern, keeping its existing interrupt() calls. - Made the `stop` flags volatile (ShimmerBluetooth.IOThread, ShimmerBluetooth.ProcessingThread, LiteProtocol.IOThread) so a writer on one thread is visibly seen by the reader thread without relying on incidental synchronization elsewhere. - Softened the processNextInstruction() idle-branch comment in ShimmerBluetooth.java: getInstruction() returning null doesn't only mean "no instruction pending" - it also happens right after removing a leading null placeholder, with a real instruction possibly next in the list. No behavioural change, comment only. Re-verified: streaming path in both IOThread.run() loops still never sleeps, every idle path still sleeps (no spin), and closeConnection() still reaches purgePort/closePort on every path. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_015q29AEJZbwa7aVYeAj61Py --- .../bluetooth/ShimmerBluetooth.java | 15 ++++-- .../comms/radioProtocol/LiteProtocol.java | 9 +++- .../shimmerresearch/pcDriver/ShimmerGRPC.java | 53 +++++++++++++++---- .../shimmerresearch/pcDriver/ShimmerPC.java | 47 ++++++++++++---- 4 files changed, 99 insertions(+), 25 deletions(-) diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java b/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java index 0dea4087c..d52c1bfad 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java @@ -577,7 +577,7 @@ public ShimmerBluetooth(String userAssignedName, double samplingRate, int enable } public class ProcessingThread extends Thread { - public boolean stop = false; + public volatile boolean stop = false; int count=0; public void run() { while (!stop && !Thread.currentThread().isInterrupted()) { @@ -606,7 +606,7 @@ public void run() { public class IOThread extends Thread { protected byte[] byteBuffer = {0}; - public boolean stop = false; + public volatile boolean stop = false; public void run() { while(!stop && !Thread.currentThread().isInterrupted()) { @@ -785,10 +785,15 @@ else if((mCurrentCommand==GET_FW_VERSION_COMMAND) mTransactionCompleted=false; return true; } else { + // getInstruction() returned null here - either the instruction stack is + // genuinely empty, or its leading entry was a null placeholder that + // getInstruction() just removed (a real instruction may still be next in + // the list and will be picked up on the following iteration). Either way, + // if there's also nothing else to do this iteration, wait here rather than + // busy-spinning. if (!mIsStreaming && !bytesAvailableToBeRead()){ - // No instruction pending and nothing else to do - wait here rather than - // busy-spinning. Report "did work" (true) so the caller's own idle sleep - // in run() doesn't stack an extra wait on top of this one. + // Report "did work" (true) so the caller's own idle sleep in run() + // doesn't stack an extra wait on top of this one. threadSleep(50); return true; } diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java b/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java index 54b6af0ca..112f2c97f 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java @@ -265,6 +265,13 @@ private void stopIoThread() { } catch (InterruptedException e) { Thread.currentThread().interrupt(); } + if (mIOThread.isAlive()) { + // Didn't terminate within the bounded join - interrupt it so the + // thread's own interrupt checks can finish tearing it down, rather + // than silently proceeding under a live reader. + printLogDataForDebugging("Warning: IOThread did not terminate within join timeout; interrupting"); + mIOThread.interrupt(); + } } mIOThread = null; } @@ -273,7 +280,7 @@ private void stopIoThread() { public class IOThread extends Thread { byte[] byteBuffer = {0}; - public boolean stop = false; + public volatile boolean stop = false; /* * Deliberately NOT synchronized: Thread.join() locks this same Thread diff --git a/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerGRPC.java b/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerGRPC.java index f9dc48001..9542f4ee7 100644 --- a/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerGRPC.java +++ b/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerGRPC.java @@ -541,16 +541,51 @@ public void disconnect() throws ShimmerException { private void closeConnection() { try { - if (mIOThread != null) { - mIOThread.stop = true; - mIOThread.interrupt(); - mIOThread.join(); // Wait until the thread terminates + // closeConnection() is unsynchronized and can be entered concurrently - once from + // connectionLost() running on the IOThread itself, and once from a user-triggered + // disconnect() on another thread. Snapshot the fields into locals before touching + // them so a concurrent caller nulling the field out mid-method can't turn a passed + // null-check into an NPE further down (see ShimmerPC.closeConnection() for the + // same fix). + IOThread ioThread = mIOThread; + if (ioThread != null) { + ioThread.stop = true; + ioThread.interrupt(); + // Skip the join when closeConnection() is reached from the IOThread itself - + // a self-join can never succeed and would just burn the full timeout. + if (Thread.currentThread() != ioThread) { + try { + ioThread.join(2000); // Bounded wait so this can't block indefinitely + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + if (ioThread.isAlive()) { + // Didn't terminate within the bounded join - interrupt again so the + // thread's own interrupt checks can finish tearing it down, rather + // than silently proceeding to close the port under a live reader. + consolePrintLn("Warning: IOThread did not terminate within join timeout; interrupting"); + ioThread.interrupt(); + } + } mIOThread = null; - + if (mUseProcessingThread) { - mPThread.stop = true; - mPThread.interrupt(); - mPThread.join(); + ProcessingThread pThread = mPThread; + if (pThread != null) { + pThread.stop = true; + pThread.interrupt(); + if (Thread.currentThread() != pThread) { + try { + pThread.join(2000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + if (pThread.isAlive()) { + consolePrintLn("Warning: ProcessingThread did not terminate within join timeout; interrupting"); + pThread.interrupt(); + } + } + } mPThread = null; } } @@ -561,7 +596,7 @@ private void closeConnection() { } catch (Exception ex) { consolePrintException(ex.getMessage(), ex.getStackTrace()); setBluetoothRadioState(BT_STATE.DISCONNECTED); - } + } } //Need to override here because ShimmerDevice class uses a different map diff --git a/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java b/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java index 194a33ba6..04a040030 100644 --- a/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java +++ b/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java @@ -605,30 +605,57 @@ protected void connectionLost() { private void closeConnection(){ try { - if (mIOThread != null) { - mIOThread.stop = true; - + // closeConnection() is unsynchronized and can be entered concurrently - + // once from connectionLost() running on the IOThread itself, and once from + // a user-triggered disconnect() on another thread. Snapshot the fields into + // locals before touching them so a concurrent caller nulling the field out + // mid-method can't turn a passed null-check into an NPE further down (which + // would otherwise be swallowed by the catch below and skip purgePort/closePort, + // leaking the COM port). + IOThread ioThread = mIOThread; + if (ioThread != null) { + ioThread.stop = true; + // Closing serial port before before thread is finished stopping throws an error so waiting here. // Bounded join instead of an empty-body busy-wait, so this can't block indefinitely. // Skip the join when closeConnection() is reached from the IOThread itself // (connectionLost() fires on it when the serial port errors mid-read/write) - // a self-join can never succeed and would just burn the full timeout. - if(Thread.currentThread() != mIOThread){ + if(Thread.currentThread() != ioThread){ try { - mIOThread.join(2000); + ioThread.join(2000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } + if (ioThread.isAlive()) { + // Didn't terminate within the bounded join - interrupt it so the + // thread's own interrupt checks can finish tearing it down, rather + // than silently proceeding to close the port under a live reader. + consolePrintLn("Warning: IOThread did not terminate within join timeout; interrupting"); + ioThread.interrupt(); + } } mIOThread = null; if(mUseProcessingThread){ - mPThread.stop = true; - try { - mPThread.join(2000); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); + ProcessingThread pThread = mPThread; + if (pThread != null) { + pThread.stop = true; + // buildAndSendMsg() runs on the ProcessingThread and dispatches to app + // listeners, so a listener calling disconnect() would self-join here - + // guard the same way as the IOThread above. + if (Thread.currentThread() != pThread) { + try { + pThread.join(2000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + if (pThread.isAlive()) { + consolePrintLn("Warning: ProcessingThread did not terminate within join timeout; interrupting"); + pThread.interrupt(); + } + } } mPThread = null; } From 254f490697cbf26f7e0be10345f6bf752e74ec22 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 4 Sep 2026 07:28:19 +0000 Subject: [PATCH 5/7] DEV-895 Address review comments: interrupt-preserving idle sleep, interrupt before join, tidy-ups Copilot review findings on PR #279: - ShimmerBluetooth.java and LiteProtocol.java: the idle-branch sleep in processNextInstruction() used threadSleep(50), whose shared implementation swallows InterruptedException without restoring the interrupt flag - defeating run()'s !Thread.currentThread().isInterrupted() exit check. Replaced with a local try/Thread.sleep(50)/catch that re-sets the flag via Thread.currentThread().interrupt(). Left every other threadSleep() call in both files untouched. - ShimmerBluetooth.java IOThread.run(): fixed inconsistent indentation in the InShimmerTest branch (didWork assignment and the readBytes block were not nested under the bytesAvailableToBeRead() guard). Indentation only - control flow verified unchanged. - ShimmerPC.java: fixed a duplicated "before before" in a comment. - ShimmerPC.closeConnection(), ShimmerGRPC.closeConnection(), and LiteProtocol.stopIoThread(): interrupt() is now called before the bounded join(2000) (still only in the non-self-join branch) instead of only after a failed join. Since run() now exits on Thread.currentThread().isInterrupted() and any idle Thread.sleep()/ queue poll() wakes immediately on interrupt, this makes shutdown faster and more deterministic. The post-join "still alive" branch now just logs a warning instead of interrupting a second time. - ShimmerGRPC.closeConnection(): moved the pre-existing interrupt() calls (for both mIOThread and mPThread) inside the Thread.currentThread() != t guard - previously they ran unconditionally and would mark the *current* thread interrupted when closeConnection() runs on the IOThread/ProcessingThread itself (the self-close case), which is neither needed nor correct. Safety check for interrupting a thread blocked in a serial read (requested before making the interrupt-before-join change): ShimmerPC reads via ByteCommunicationJSSC, which calls JSSC's SerialPort.readBytes(count, timeout) - a native, timeout-bounded JNI call that is not interruptible and never throws InterruptedException; Thread.interrupt() on a thread blocked there only sets the interrupt flag, observed once the call returns (by timeout or data) at the top of run() or in a subsequent Thread.sleep()/queue.poll(). bytesAvailableToBeRead()/readBytes()/ writeBytes() only catch SerialPortException/SerialPortTimeoutException/ NullPointerException, none of which interrupt() can trigger, so no new connectionLost() -> closeConnection() recursion is introduced. ShimmerGRPC reads via a ThreadSafeByteFifoBuffer whose read() does declare InterruptedException and is already caught locally (printed and treated as "no data"), so it is safe by construction. No path was found where the interrupt flag causes a new failure mode; the change was made in all three teardown sites as requested. Re-verified: both IOThread.run() loops still never sleep on the streaming path, no path can spin without sleeping, and closeConnection() still reaches purgePort/closePort on every path. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_015q29AEJZbwa7aVYeAj61Py --- .../bluetooth/ShimmerBluetooth.java | 25 ++++++++++++------- .../comms/radioProtocol/LiteProtocol.java | 23 ++++++++++++----- .../shimmerresearch/pcDriver/ShimmerGRPC.java | 23 +++++++++-------- .../shimmerresearch/pcDriver/ShimmerPC.java | 21 ++++++++++------ 4 files changed, 59 insertions(+), 33 deletions(-) diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java b/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java index d52c1bfad..1bb68ec4f 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java @@ -614,16 +614,16 @@ public void run() { //In-Shimmer Test here if(InShimmerTest) { if (bytesAvailableToBeRead()) { - didWork = true; - byte[] data = readBytes(availableBytes()); - if (data!=null) { - if (data.length>0) { - if (mTestByteListener != null) { - mTestByteListener.eventNewBytesReceived(data); - } + didWork = true; + byte[] data = readBytes(availableBytes()); + if (data!=null) { + if (data.length>0) { + if (mTestByteListener != null) { + mTestByteListener.eventNewBytesReceived(data); + } + } } } - } } else { if (mDummyRead) { @@ -794,7 +794,14 @@ else if((mCurrentCommand==GET_FW_VERSION_COMMAND) if (!mIsStreaming && !bytesAvailableToBeRead()){ // Report "did work" (true) so the caller's own idle sleep in run() // doesn't stack an extra wait on top of this one. - threadSleep(50); + // Not threadSleep(): that helper swallows InterruptedException without + // restoring the interrupt flag, which would defeat run()'s + // !Thread.currentThread().isInterrupted() loop-exit check. + try { + Thread.sleep(50); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } return true; } return false; diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java b/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java index 112f2c97f..5b33cbe5c 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java @@ -260,17 +260,21 @@ private void stopIoThread() { // synchronously from within IOThread.run()'s catch block (error-driven teardown), // and a thread joining itself would just burn the full timeout. if(Thread.currentThread() != mIOThread){ + // Interrupt before joining: run() now exits promptly on + // Thread.currentThread().isInterrupted(), and any idle Thread.sleep()/ + // queue poll() it's waiting in will wake immediately - making shutdown + // faster and more deterministic than waiting out the bounded join first. + mIOThread.interrupt(); try { mIOThread.join(2000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } if (mIOThread.isAlive()) { - // Didn't terminate within the bounded join - interrupt it so the - // thread's own interrupt checks can finish tearing it down, rather - // than silently proceeding under a live reader. - printLogDataForDebugging("Warning: IOThread did not terminate within join timeout; interrupting"); - mIOThread.interrupt(); + // Still not terminated after the interrupt + bounded join - most likely + // blocked in a non-interruptible native serial read. Just warn; a second + // interrupt() here wouldn't unblock it either. + printLogDataForDebugging("Warning: IOThread did not terminate within join timeout; it may be blocked in a non-interruptible read"); } } mIOThread = null; @@ -442,7 +446,14 @@ else if((mCurrentCommand==InstructionsGet.GET_FW_VERSION_COMMAND_VALUE) // No instruction pending and nothing else to do - wait here rather than // busy-spinning. Report "did work" (true) so the caller's own idle sleep // in run() doesn't stack an extra wait on top of this one. - threadSleep(50); + // Not threadSleep(): that helper swallows InterruptedException without + // restoring the interrupt flag, which would defeat run()'s + // !Thread.currentThread().isInterrupted() loop-exit check. + try { + Thread.sleep(50); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } return true; } } diff --git a/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerGRPC.java b/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerGRPC.java index 9542f4ee7..9c3bced9f 100644 --- a/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerGRPC.java +++ b/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerGRPC.java @@ -550,21 +550,24 @@ private void closeConnection() { IOThread ioThread = mIOThread; if (ioThread != null) { ioThread.stop = true; - ioThread.interrupt(); // Skip the join when closeConnection() is reached from the IOThread itself - - // a self-join can never succeed and would just burn the full timeout. + // a self-join can never succeed and would just burn the full timeout. The + // interrupt() call moves inside this guard too: calling it unconditionally + // would mark the *current* thread interrupted in the self-close case. if (Thread.currentThread() != ioThread) { + // Interrupt before joining: run() now exits promptly on + // Thread.currentThread().isInterrupted(), and any idle Thread.sleep() + // it's waiting in will wake immediately - making shutdown faster and + // more deterministic than waiting out the bounded join first. + ioThread.interrupt(); try { ioThread.join(2000); // Bounded wait so this can't block indefinitely } catch (InterruptedException e) { Thread.currentThread().interrupt(); } if (ioThread.isAlive()) { - // Didn't terminate within the bounded join - interrupt again so the - // thread's own interrupt checks can finish tearing it down, rather - // than silently proceeding to close the port under a live reader. - consolePrintLn("Warning: IOThread did not terminate within join timeout; interrupting"); - ioThread.interrupt(); + // Still not terminated after the interrupt + bounded join. + consolePrintLn("Warning: IOThread did not terminate within join timeout"); } } mIOThread = null; @@ -573,16 +576,16 @@ private void closeConnection() { ProcessingThread pThread = mPThread; if (pThread != null) { pThread.stop = true; - pThread.interrupt(); if (Thread.currentThread() != pThread) { + // Interrupt before joining - see the IOThread teardown above. + pThread.interrupt(); try { pThread.join(2000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } if (pThread.isAlive()) { - consolePrintLn("Warning: ProcessingThread did not terminate within join timeout; interrupting"); - pThread.interrupt(); + consolePrintLn("Warning: ProcessingThread did not terminate within join timeout"); } } } diff --git a/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java b/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java index 04a040030..da872e9fd 100644 --- a/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java +++ b/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java @@ -616,23 +616,27 @@ private void closeConnection(){ if (ioThread != null) { ioThread.stop = true; - // Closing serial port before before thread is finished stopping throws an error so waiting here. + // Closing serial port before thread is finished stopping throws an error so waiting here. // Bounded join instead of an empty-body busy-wait, so this can't block indefinitely. // Skip the join when closeConnection() is reached from the IOThread itself // (connectionLost() fires on it when the serial port errors mid-read/write) - // a self-join can never succeed and would just burn the full timeout. if(Thread.currentThread() != ioThread){ + // Interrupt before joining: run() now exits promptly on + // Thread.currentThread().isInterrupted(), and any idle Thread.sleep() + // it's waiting in will wake immediately - making shutdown faster and + // more deterministic than waiting out the bounded join first. + ioThread.interrupt(); try { ioThread.join(2000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } if (ioThread.isAlive()) { - // Didn't terminate within the bounded join - interrupt it so the - // thread's own interrupt checks can finish tearing it down, rather - // than silently proceeding to close the port under a live reader. - consolePrintLn("Warning: IOThread did not terminate within join timeout; interrupting"); - ioThread.interrupt(); + // Still not terminated after the interrupt + bounded join - most + // likely blocked in a non-interruptible native serial read. Just + // warn; a second interrupt() here wouldn't unblock it either. + consolePrintLn("Warning: IOThread did not terminate within join timeout; it may be blocked in a non-interruptible read"); } } @@ -646,14 +650,15 @@ private void closeConnection(){ // listeners, so a listener calling disconnect() would self-join here - // guard the same way as the IOThread above. if (Thread.currentThread() != pThread) { + // Interrupt before joining - see the IOThread teardown above. + pThread.interrupt(); try { pThread.join(2000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } if (pThread.isAlive()) { - consolePrintLn("Warning: ProcessingThread did not terminate within join timeout; interrupting"); - pThread.interrupt(); + consolePrintLn("Warning: ProcessingThread did not terminate within join timeout"); } } } From e36c8919cef1e5867939b9e0713727a1ab5227f4 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 5 Oct 2026 08:30:27 +0000 Subject: [PATCH 6/7] DEV-895: harden IOThread/ProcessingThread teardown per PR #279 review - LiteProtocol.stopIoThread(): snapshot mIOThread into a local and use it for stop/interrupt/join/isAlive, so a concurrent teardown that nulls the field after the null check can no longer cause an NPE. Only clear the field if it still points at the thread that was stopped, so a newly started IOThread is not clobbered. - ShimmerPC/ShimmerGRPC.closeConnection(): move the ProcessingThread (mPThread) teardown out of the `if (ioThread != null)` block into its own sibling block, so it still runs when another caller has already nulled mIOThread while the ProcessingThread is alive. The logic of each block is otherwise unchanged. The merge of origin/master is in the preceding merge commit. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01FkxGgMBTsQEZ36RNU3khML --- .../comms/radioProtocol/LiteProtocol.java | 21 ++++++---- .../shimmerresearch/pcDriver/ShimmerGRPC.java | 35 ++++++++-------- .../shimmerresearch/pcDriver/ShimmerPC.java | 41 ++++++++++--------- 3 files changed, 53 insertions(+), 44 deletions(-) diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java b/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java index 5b33cbe5c..228934875 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java @@ -253,31 +253,38 @@ private void startIoThread() { } private void stopIoThread() { - if(mIOThread!=null){ - mIOThread.stop=true; + // Snapshot the field: a concurrent teardown may null mIOThread between the + // null check and the interrupt/join/isAlive calls below. + IOThread ioThread = mIOThread; + if(ioThread!=null){ + ioThread.stop=true; // Bounded wait for the thread to actually exit its loop before dropping the // reference. Guard against the self-join case: killConnection() can reach here // synchronously from within IOThread.run()'s catch block (error-driven teardown), // and a thread joining itself would just burn the full timeout. - if(Thread.currentThread() != mIOThread){ + if(Thread.currentThread() != ioThread){ // Interrupt before joining: run() now exits promptly on // Thread.currentThread().isInterrupted(), and any idle Thread.sleep()/ // queue poll() it's waiting in will wake immediately - making shutdown // faster and more deterministic than waiting out the bounded join first. - mIOThread.interrupt(); + ioThread.interrupt(); try { - mIOThread.join(2000); + ioThread.join(2000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } - if (mIOThread.isAlive()) { + if (ioThread.isAlive()) { // Still not terminated after the interrupt + bounded join - most likely // blocked in a non-interruptible native serial read. Just warn; a second // interrupt() here wouldn't unblock it either. printLogDataForDebugging("Warning: IOThread did not terminate within join timeout; it may be blocked in a non-interruptible read"); } } - mIOThread = null; + // Only clear the field if it still refers to the thread we stopped, so a + // newly started IOThread is not clobbered. + if (mIOThread == ioThread) { + mIOThread = null; + } } } diff --git a/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerGRPC.java b/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerGRPC.java index 9c3bced9f..c146588b2 100644 --- a/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerGRPC.java +++ b/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerGRPC.java @@ -571,26 +571,27 @@ private void closeConnection() { } } mIOThread = null; - - if (mUseProcessingThread) { - ProcessingThread pThread = mPThread; - if (pThread != null) { - pThread.stop = true; - if (Thread.currentThread() != pThread) { - // Interrupt before joining - see the IOThread teardown above. - pThread.interrupt(); - try { - pThread.join(2000); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - if (pThread.isAlive()) { - consolePrintLn("Warning: ProcessingThread did not terminate within join timeout"); - } + } + // Not nested under the IOThread block: a concurrent caller may already have + // nulled mIOThread while the ProcessingThread is still running. + if (mUseProcessingThread) { + ProcessingThread pThread = mPThread; + if (pThread != null) { + pThread.stop = true; + if (Thread.currentThread() != pThread) { + // Interrupt before joining - see the IOThread teardown above. + pThread.interrupt(); + try { + pThread.join(2000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + if (pThread.isAlive()) { + consolePrintLn("Warning: ProcessingThread did not terminate within join timeout"); } } - mPThread = null; } + mPThread = null; } mIsStreaming = false; mIsInitialised = false; diff --git a/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java b/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java index da872e9fd..353262f0c 100644 --- a/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java +++ b/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java @@ -641,29 +641,30 @@ private void closeConnection(){ } mIOThread = null; - - if(mUseProcessingThread){ - ProcessingThread pThread = mPThread; - if (pThread != null) { - pThread.stop = true; - // buildAndSendMsg() runs on the ProcessingThread and dispatches to app - // listeners, so a listener calling disconnect() would self-join here - - // guard the same way as the IOThread above. - if (Thread.currentThread() != pThread) { - // Interrupt before joining - see the IOThread teardown above. - pThread.interrupt(); - try { - pThread.join(2000); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - if (pThread.isAlive()) { - consolePrintLn("Warning: ProcessingThread did not terminate within join timeout"); - } + } + // Not nested under the IOThread block: a concurrent caller may already have + // nulled mIOThread while the ProcessingThread is still running. + if(mUseProcessingThread){ + ProcessingThread pThread = mPThread; + if (pThread != null) { + pThread.stop = true; + // buildAndSendMsg() runs on the ProcessingThread and dispatches to app + // listeners, so a listener calling disconnect() would self-join here - + // guard the same way as the IOThread above. + if (Thread.currentThread() != pThread) { + // Interrupt before joining - see the IOThread teardown above. + pThread.interrupt(); + try { + pThread.join(2000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + if (pThread.isAlive()) { + consolePrintLn("Warning: ProcessingThread did not terminate within join timeout"); } } - mPThread = null; } + mPThread = null; } mIsStreaming = false; mIsInitialised = false; From 8e9e63f9cac937450baad5cc98220a76bc0abe2b Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 5 Oct 2026 09:00:10 +0000 Subject: [PATCH 7/7] DEV-895: keep a user disconnect from turning into CONNECTION_LOST F1: closeConnection() closes the serial port after a bounded 2 s join even if the IOThread is still blocked in a non-interruptible jssc call. When that call then failed on the closed port (or NPE'd on the nulled mByteCommunication), the ShimmerPC I/O helpers called connectionLost(), setting CONNECTION_LOST after a deliberate disconnect, which AbstractConsensysApi answers with an auto-reconnect in NeuroLynQ. The catch sites in bytesAvailableToBeRead/availableBytes/writeBytes/readBytes, and ShimmerGRPC.writeBytes, now skip connectionLost() when running on an IOThread whose stop flag is set. Both closeConnection()s also clear mIOThread/mPThread only if they still hold the thread that was stopped, as LiteProtocol.stopIoThread() already does, so a late closeConnection() cannot clobber a reconnect's new threads. F2: LiteProtocol.IOThread.run() no longer calls killConnection() when stop is already set: the teardown interrupt surfacing from txBytes as ERR_WRITING_DATA caused a spurious error event and a re-entrant disconnect. Test: API_00028_IoThreadTeardownTest (ShimmerDriverPC) pins that idle IOThread/ProcessingThread use <300 ms CPU per second, that disconnect() stops both within 1500 ms, and that a write blocked past the join and then failing on the closed port leaves the device DISCONNECTED (fails without F1). Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01FkxGgMBTsQEZ36RNU3khML --- .../comms/radioProtocol/LiteProtocol.java | 6 +- .../shimmerresearch/pcDriver/ShimmerGRPC.java | 23 ++- .../shimmerresearch/pcDriver/ShimmerPC.java | 39 +++- .../API_00028_IoThreadTeardownTest.java | 193 ++++++++++++++++++ 4 files changed, 250 insertions(+), 11 deletions(-) create mode 100644 ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00028_IoThreadTeardownTest.java diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java b/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java index 228934875..8ab6f3a79 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/comms/radioProtocol/LiteProtocol.java @@ -327,7 +327,11 @@ else if(mWaitForResponse) { } catch (ShimmerException dE) { // stop=true; - killConnection(dE); + // DEV-895: once stop is set, an error here is the teardown interrupt surfacing + // (e.g. as ERR_WRITING_DATA from txBytes), not a real link failure. + if(!stop){ + killConnection(dE); + } // e.printStackTrace(); //TODO send event up the ladder } diff --git a/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerGRPC.java b/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerGRPC.java index c146588b2..0f251cc73 100644 --- a/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerGRPC.java +++ b/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerGRPC.java @@ -290,10 +290,21 @@ protected void writeBytes(byte[] data) { // Remote gRPC service is not available, so disconnect Shimmer device sre.printStackTrace(); System.out.println("ERROR: Lost connection to GRPC Server"); - connectionLost(); + if(!isOnStoppedIoThread()){ + connectionLost(); + } } } + /** DEV-895: true when running on an IOThread whose stop flag is set, i.e. the failure is the + * expected consequence of closeConnection() tearing down under an IOThread that outlived the + * bounded join. Reporting CONNECTION_LOST then would turn a user disconnect into an + * auto-reconnect downstream. */ + private boolean isOnStoppedIoThread(){ + Thread t = Thread.currentThread(); + return (t instanceof IOThread) && ((IOThread)t).stop; + } + @Override protected void stop() { // TODO Auto-generated method stub @@ -570,7 +581,11 @@ private void closeConnection() { consolePrintLn("Warning: IOThread did not terminate within join timeout"); } } - mIOThread = null; + // Only clear the field if it still refers to the thread we stopped, so a + // late closeConnection() cannot clobber a reconnect's new IOThread. + if (mIOThread == ioThread) { + mIOThread = null; + } } // Not nested under the IOThread block: a concurrent caller may already have // nulled mIOThread while the ProcessingThread is still running. @@ -590,8 +605,10 @@ private void closeConnection() { consolePrintLn("Warning: ProcessingThread did not terminate within join timeout"); } } + if (mPThread == pThread) { + mPThread = null; + } } - mPThread = null; } mIsStreaming = false; mIsInitialised = false; diff --git a/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java b/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java index 353262f0c..7c5ee004d 100644 --- a/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java +++ b/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java @@ -434,7 +434,9 @@ public boolean bytesAvailableToBeRead() { } catch (SerialPortException | NullPointerException ex) { consolePrintException(ex.getMessage(), ex.getStackTrace()); - connectionLost(); + if(!isOnStoppedIoThread()){ + connectionLost(); + } // e.printStackTrace(); } return false; @@ -450,7 +452,9 @@ public int availableBytes(){ } } catch (SerialPortException | NullPointerException ex) { consolePrintException(ex.getMessage(), ex.getStackTrace()); - connectionLost(); + if(!isOnStoppedIoThread()){ + connectionLost(); + } return 0; } } @@ -467,7 +471,9 @@ public void writeBytes(byte[] data) { } catch (SerialPortException | NullPointerException ex) { consolePrintLn("Tried to writeBytes but port is closed"); consolePrintException(ex.getMessage(), ex.getStackTrace()); - connectionLost(); + if(!isOnStoppedIoThread()){ + connectionLost(); + } } } @@ -491,7 +497,9 @@ protected byte[] readBytes(int numberOfBytes) { } } } catch (SerialPortException | NullPointerException e) { - connectionLost(); + if(!isOnStoppedIoThread()){ + connectionLost(); + } consolePrintLn("Tried to readBytes but serial port error"); consolePrintException(e.getMessage(), e.getStackTrace()); // e.printStackTrace(); @@ -505,7 +513,9 @@ protected byte[] readBytes(int numberOfBytes) { // ShimmerBluetooth if(mBluetoothRadioState==BT_STATE.CONNECTING || mBluetoothRadioState==BT_STATE.CONFIGURING){ - connectionLost(); + if(!isOnStoppedIoThread()){ + connectionLost(); + } } } return null; @@ -595,6 +605,15 @@ public void disconnectNoException() { } } + /** DEV-895: true when running on an IOThread whose stop flag is set, i.e. the failure is the + * expected consequence of closeConnection() closing the port under a reader that outlived the + * bounded join. Reporting CONNECTION_LOST then would turn a user disconnect into an + * auto-reconnect downstream. */ + private boolean isOnStoppedIoThread(){ + Thread t = Thread.currentThread(); + return (t instanceof IOThread) && ((IOThread)t).stop; + } + @Override protected void connectionLost() { disconnectNoException(); @@ -640,7 +659,11 @@ private void closeConnection(){ } } - mIOThread = null; + // Only clear the field if it still refers to the thread we stopped, so a + // late closeConnection() cannot clobber a reconnect's new IOThread. + if (mIOThread == ioThread) { + mIOThread = null; + } } // Not nested under the IOThread block: a concurrent caller may already have // nulled mIOThread while the ProcessingThread is still running. @@ -663,8 +686,10 @@ private void closeConnection(){ consolePrintLn("Warning: ProcessingThread did not terminate within join timeout"); } } + if (mPThread == pThread) { + mPThread = null; + } } - mPThread = null; } mIsStreaming = false; mIsInitialised = false; diff --git a/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00028_IoThreadTeardownTest.java b/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00028_IoThreadTeardownTest.java new file mode 100644 index 000000000..18f44c653 --- /dev/null +++ b/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00028_IoThreadTeardownTest.java @@ -0,0 +1,193 @@ +package com.shimmerresearch.bluetooth; + +import static org.junit.Assert.*; + +import java.lang.management.ManagementFactory; +import java.lang.management.ThreadMXBean; +import java.util.concurrent.ConcurrentLinkedDeque; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.junit.After; +import org.junit.Assume; +import org.junit.Test; + +import com.shimmerresearch.bluetooth.ShimmerBluetooth.BT_STATE; +import com.shimmerresearch.pcDriver.ShimmerPC; +import com.shimmerresearch.shimmer3.communication.ByteCommunication; +import com.shimmerresearch.verisense.communication.ByteCommunicationListener; + +import jssc.SerialPortException; +import jssc.SerialPortTimeoutException; + +/** + * DEV-895: the IOThread and ProcessingThread must sleep rather than spin when + * idle, must stop promptly on disconnect(), and a disconnect() must stay a + * disconnect. closeConnection() only waits a bounded 2 s for the IOThread, so + * one blocked in a non-interruptible serial call outlives it, and the port is + * closed under it. When that call then fails, the I/O helpers used to report + * CONNECTION_LOST, which NeuroLynQ answers with an auto-reconnect. + *

+ * No hardware: the threads are started as connect() starts them, over an + * in-memory radio. It lives in this package because mIOThread and mPThread + * are protected members of ShimmerBluetooth. + */ +public class API_00028_IoThreadTeardownTest { + + /** Thread-safe in-memory radio. Its writes can block, ignoring interrupts, as a native jssc call does. */ + private static class BlockingRadio implements ByteCommunication { + final ConcurrentLinkedDeque rx = new ConcurrentLinkedDeque(); + final long writeBlockMs; + final CountDownLatch writeStarted = new CountDownLatch(1); + volatile boolean open = true; + + BlockingRadio(long writeBlockMs) { + this.writeBlockMs = writeBlockMs; + } + + @Override + public boolean writeBytes(byte[] buffer) throws SerialPortException { + writeStarted.countDown(); + if(writeBlockMs>0){ + long end = System.currentTimeMillis()+writeBlockMs; + boolean interrupted = false; + long left; + while((left = end-System.currentTimeMillis())>0){ + try { + Thread.sleep(left); + } catch (InterruptedException e) { + interrupted = true; + } + } + if(interrupted){ + Thread.currentThread().interrupt(); + } + if(!open){ + throw new SerialPortException("TEST", "writeBytes", "Port closed during write"); + } + } + return true; + } + + @Override + public byte[] readBytes(int byteCount, int timeout) throws SerialPortTimeoutException, SerialPortException { + if(!open){ + throw new SerialPortException("TEST", "readBytes", "Port not opened"); + } + if(rx.size()0){ + Thread.sleep(left); + } + assertEquals("still disconnected, not CONNECTION_LOST", BT_STATE.DISCONNECTED, device.getBluetoothRadioState()); + } +}