Repository navigation
DEV-1372: a disconnect while a queued command is about to be sent no longer crashes the app - #143
Merged
Merged
Conversation
The driver's IO thread takes a queued command, sleeps 10-110 ms, then sends it through writeBytes(), which is Shimmer.write() here. disconnect() calls stop() on the caller's thread, and stop() nulls mConnectedThread without waiting for the IO thread. So a disconnect inside that sleep made write() dereference null. Nothing on the IO thread catches that, and Android killed the app. The Android HIL hit it over Bluetooth Classic on drivers 0.11.9 to 0.11.12_beta. The bug has been there since 2024, when the guarded write() was commented out. write() now reads the connection once, and drops and logs a write with none, as a write that fails on a closed socket already is. The field is volatile so the IO thread sees stop()'s null. AAPI_00007 covers a write after stop(), after disconnect() and before any connection. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s check dock and USB configureAccelOnly() counted the configuration done on a "Ready" plus the requested rate. Straight after a stopStreaming(), the stop's own "Ready" and the rate from the first config chunk both arrived with commands still queued. samplingRates then disconnected under the IO thread, which is how the run hit the crash fixed in the previous commit. The wait now also needs an empty instruction queue. The configure queues the whole operation before it returns, so an empty queue means it has finished. Docked, or with USB in a Shimmer3R, the firmware ACKs a start of SD logging and then ignores it. The driver marks the unit logging on that ACK, so midStreamSet and inStream passed without logging anything, and connectReady failed later, on the reconnect. All three now fail at once when the unit reports itself docked or on USB, and say why. The README lists it under preparing each unit. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The crash
If
disconnect()lands while the driver's IO thread is about to send a queued command over Bluetooth Classic, the app process dies:How it happens:
IOThread.processNextInstruction()takes a command off the queue, sleeps 10-110 ms, then callswriteBytes(), which isShimmer.write()here.disconnect()callsstop()on the caller's thread.stop()nullsmConnectedThreadwithout waiting for the IO thread.write()dereference null. Nothing on the IO thread catches the exception, so Android kills the app.This has been possible since 2024, when the guarded
write()was commented out (first shipped in AndroidAPI_v3.0.84beta). It does not come from a driver change.The Android HIL has hit it on drivers 0.11.9 to 0.11.12_beta. An app that connects and then disconnects straight away can hit it too: the connect sequence leaves SET_RWC_COMMAND queued after the unit is reported ready.
Not affected:
Shimmer3BLEAndroidwrites throughBleManager, which reports a failed write through a callback.ShimmerPC.writeBytes()null-checks the port and catchesNullPointerException.The fix
write()reads the connection once. If there is none, it drops the write and logs "Command NOT transmitted, not connected", the same way a write that fails on a closed socket is already handled.mConnectedThreadis nowvolatile, so the IO thread sees the null thatstop()sets.AAPI_00007_WriteAfterDisconnectTestwrites afterstop(), afterdisconnect(), and with no connection. All three tests fail with the NullPointerException on master and pass with the fix.HIL harness (second commit)
The harness is what was disconnecting inside that window. Two changes:
Configure wait.
configureAccelOnly()treated the configuration as done once it saw a "Ready" and the requested rate. Straight after astopStreaming(), both arrive while commands are still queued: the "Ready" comes from the stop itself, and the rate is set by the first config chunk.samplingRatesthen disconnected under the IO thread. The wait now also needs the instruction queue to be empty. The configure queues the whole operation before it returns, so an empty queue means it has finished.SD-logging scenarios check dock and USB.
midStreamSetandinStreamwere passing without logging anything, andconnectReadyfailed later, on the reconnect.Testing
:ShimmerAndroidInstrumentDriver:testDebugUnitTest, 29 tests, all pass.:shimmerHilTest:compileDebugAndroidTestJavaWithJavacbuilds.connectReadyover Classic, the test disconnected 15 ms before the IO thread sent the queued SET_RWC_COMMAND.write()logged the dropped write and the app kept running. No logcat in the run has a FATAL EXCEPTION.samplingRatesconfigure now finishes with 0 instructions queued, andsamplingRatespasses on both units.midStreamSet,inStreamandconnectReadystopped at the new check, with the new message.🤖 Generated with Claude Code