Repository navigation
DEV-1266: Take the instruction list's two locks in one order - #334
Merged
Merged
Conversation
ShimmerBluetooth guards its instruction list with two locks: the device (its synchronized methods) and the list itself. Every accessor takes them device first, list second. getInstruction(), which the IO thread calls for the next instruction, took the list and then called the synchronized getListofInstructions(), so it took them the other way round. So the IO thread could hold the list while waiting for the device, just as a timer held the device while waiting for the list: - the ACK/response timeout, calling clearAllInstructions() while streaming; - the read-status timer, queuing GET_STATUS_COMMAND. Both then waited for good, and so did every later API call. The Android HIL caught it on a Shimmer3, where stopSDLogging() never returned. It was also the Linux CI hang in DEV-1198, which #325 worked around in the tests only. getInstruction() now reads the head of the list directly, so the IO thread never needs the device while it holds the list. This is Mas's suggestion from her review of #326. API_00033 reproduces the deadlock deterministically. A "timer" thread holds the device the way every synchronized accessor does while the IO thread's real getInstruction() runs; the timer then queues an instruction, which needs the list. Without the fix, the JVM's deadlock detector finds the IO thread blocked in getListofInstructions() and the timer blocked in writeInstruction(), the same stacks as the hardware hang. A second test connects five times with the read-status timer left on, as the product runs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
JongChern
added a commit
that referenced
this pull request
Oct 22, 2026
While a unit streams, four app threads call readBattery() and readStatusLogAndStream() and check the queue length, in tight loops for 60 s, keeping the queue under 8. Each of those calls locks the device and then the instruction list, so one is nearly always under way while the IO thread, holding the list, fetches the next instruction. That is the window DEV-1266's lock inversion needs. No SD logging is involved. The scenario fails if the calls and replies stop for 10 s, or if the JVM finds a deadlock. Either way the events get every thread's stack, and the unit is left alone. On a Shimmer3R (v1.1.21) without the DEV-1266 fix (#334), it deadlocked within 0.3 s in both runs. With the fix, two 60-second runs were clean, with about 670 calls and 0 timeouts each. It runs by default, so the release gate includes it. Until #334 merges, the gate fails on a deadlock. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Oct 22, 2026
Contributor
Author
|
Combined hardware verification. This used the Java HIL from #331 on a local merge of #331, #332, #333 and #334. The units were a Shimmer3 (LogAndStream v1.1.7) and a Shimmer3R (v1.1.21) over Bluetooth Classic, with nothing else on the bench.
|
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 bug
ShimmerBluetoothguards its instruction list with two locks: the device (itssynchronizedmethods) and the list itself. Every accessor takes them device first, list second:writeInstruction,clearAllInstructions,removeInstruction,getListofInstructionsand the rest.getInstruction(), which the IO thread calls for the next instruction, took them the other way round. It locked the list, then called thesynchronizedgetListofInstructions(), which needs the device.So the IO thread could hold the list while waiting for the device, at the same moment as another thread held the device while waiting for the list. Any thread that touches the queue can do that:
Both threads then wait for good, and so does every later API call,
stopStreaming()anddisconnect()included. Only restarting the app recovers.The Android HIL caught it on a Shimmer3 (
stopSDLogging()never returned), and it was the Linux CI hang in DEV-1198. #325 only worked around it in the tests, by starting simulator devices without the status timer.The fix
getInstruction()now reads the head of the list directly, so the IO thread never needs the device while it holds the list. Every path now takes device, then list. This is Mas's suggestion from her review of #326. Every other place that locks the list is already inside asynchronizedmethod.Tests
API_00033_InstructionListLockOrderTest.theIoThreadAndATimerTakeTheTwoLocksInOneOrderreproduces the deadlock deterministically. A "timer" thread holds the device, as everysynchronizedaccessor does, while the IO thread's realgetInstruction()runs. The timer then queues an instruction, which needs the list.getListofInstructions()while holding the list, and the timer blocked inwriteInstruction()while holding the device. These are the same stacks as the hardware hang.connectsWithTheStatusTimerOnconnects to the simulator five times with the read-status timer left on, as the product runs.ShimmerDriverPC: 68 of 68 pass.ShimmerDriver: 177 of 177 pass.On hardware
The Java HIL (#331) has a new
queueStressscenario. While a unit streams, four app threads queue harmless reads (readBattery()andreadStatusLogAndStream()) and check the queue length, in tight loops, for 60 s, under a hang watchdog. No SD logging is involved.Both builds had #331's HIL and the DEV-1275 (#333) and DEV-1291 (#332) fixes. The only difference was this commit.
In the failing runs, the JVM's deadlock detector found the IO thread holding the list in
getInstruction()and waiting for the device, while a stress thread held the device and waited for the list. That is the same lock pair as the Android hang.A full HIL run with all three fixes passed 7 of 8 scenarios. The miss was a single Shimmer3 sample around a battery read: a clean timestamp gap with no parser error, which happened at the same rate without this fix.
Other APIs
The two-lock structure is specific to the Java driver. Android's
Shimmer.javainheritsgetInstruction(), so it gets the fix once its driver pin is bumped, and nothing has to be ported. The C#, Python, web, Swift and Rust APIs lock differently.🤖 Generated with Claude Code