Skip to content

DEV-1266: Take the instruction list's two locks in one order - #334

Merged
JongChern merged 1 commit into
masterfrom
DEV-1266_instruction_list_lock_order
Oct 22, 2026
Merged

JongChern merged 1 commit into
masterfrom
DEV-1266_instruction_list_lock_order

Conversation

@JongChern

Copy link
Copy Markdown
Contributor

The bug

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: writeInstruction, clearAllInstructions, removeInstruction, getListofInstructions and the rest.

getInstruction(), which the IO thread calls for the next instruction, took them the other way round. It locked the list, then called the synchronized getListofInstructions(), 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:

  • the ACK/response timeout, which clears the queue while streaming;
  • the read-status and battery timers;
  • the app's own calls.

Both threads then wait for good, and so does every later API call, stopStreaming() and disconnect() 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 a synchronized method.

Tests

  • API_00033_InstructionListLockOrderTest.theIoThreadAndATimerTakeTheTwoLocksInOneOrder reproduces the deadlock deterministically. A "timer" thread holds the device, as 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() while holding the list, and the timer blocked in writeInstruction() while holding the device. These are the same stacks as the hardware hang.
    • With the fix, both finish.
  • connectsWithTheStatusTimerOn connects 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 queueStress scenario. While a unit streams, four app threads queue harmless reads (readBattery() and readStatusLogAndStream()) and check the queue length, in tight loops, for 60 s, under a hang watchdog. No SD logging is involved.

Shimmer3R, LogAndStream v1.1.21, over Classic Without this fix With this fix
Run 1 deadlocked after 0.3 s, 8 calls 60 s, 679 calls, 674 replies decoded, 0 timeouts
Run 2 deadlocked after 0.3 s, 8 calls 60 s, 665 calls, 661 replies decoded, 0 timeouts

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.java inherits getInstruction(), 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

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>
@JongChern

Copy link
Copy Markdown
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.

  • Default 1-byte CRC: all 10 scenario runs passed: streamRest, midStreamSet, reconnectLoop, inStream and queueStress, on both units. There were 0 parser errors and 0 command timeouts.
  • Unit tests on the same merge: ShimmerDriverPC 85 of 85, ShimmerDriver 177 of 177.
  • This fix (DEV-1266): queueStress ran 60 s clean on both units with the 1-byte CRC, and on the Shimmer3R with the 2-byte CRC: 565–665 calls per run, with no hang. Without this fix it deadlocked within 0.3 s, in 2 of 2 runs.

@JongChern
JongChern merged commit 927f57f into master Oct 22, 2026
2 checks passed
@JongChern
JongChern deleted the DEV-1266_instruction_list_lock_order branch October 22, 2026 07:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant