Skip to content

DEV-1306: a stop's reply no longer retires the wrong instruction - #337

Merged
JongChern merged 1 commit into
masterfrom
DEV-1306_stop_reply_retires_wrong_instruction
Oct 23, 2026
Merged

JongChern merged 1 commit into
masterfrom
DEV-1306_stop_reply_retires_wrong_instruction

Conversation

@JongChern

Copy link
Copy Markdown
Contributor

DEV-1306. Two defects in how the driver retires the command in flight around a stop. Found by the Java HIL's new samplingRates scenario in #336 (DEV-1305).

The problem

Every reply path retires the command in flight with removeInstruction(0), which assumes the head of the instruction list is the command being answered. Around a stop, two things break that assumption.

1. A configuration started straight after stopStreaming() loses its first SET_INFOMEM. That chunk holds the sampling rate.

  1. The STOP stays at the head of the list, in flight, until its stop handling retires it.
  2. That handling starts on the first byte back and sleeps 400 ms (IOThread.processNotStreamingWaitForAck).
  3. During that time, a configuration's operationPrepare() cleared the list with clearAllInstructions(), removing the STOP. The configuration then queued its chunks.
  4. When the stop handling woke, it removed chunk 0 in the STOP's place.

The unit kept its old rate, while chunks 1 and 2 and the sensors were written.

On the bench, this happened on both a Shimmer3 (LogAndStream 1.1.7) and a Shimmer3R (1.1.22):

  • 4 of 4 configurations started straight after a stop lost chunk 0. The driver log has no "Command Transmitted" for address 0x0000, and the operation starts at progress counter 2.
  • 4 of 4 configurations started on a quiet link sent it.

It isn't the firmware: a raw SET_SAMPLING_RATE sent 0, 50 or 2,000 ms after a STOP was taken 5 of 5 times.

2. A stop answered by its ACK first retires two instructions.

  1. The stop handling took the unit's ACK, then let the same byte go on into the ordinary ACK block.
  2. STOP is in the SET command map, so processAckFromSetCommand called removeInstruction(0) again.
  3. That removed the command queued behind the STOP, so it was never sent.

The firmware answers a stop with its ACK first whenever the stop lands between packets. But the driver sends queued commands while streaming only between blocking reads, so its own STOP goes out as a packet starts arriving, and data comes back first. On hardware, this defect needs the stream to be quiet for over 2 s before a stop. The simulator test below covers it.

Both have been there since 2017: the stop handling's removeInstruction(0) since 2017-05, and operationPrepare()'s clear since 2017-10.

The fix

  • operationPrepare() waits until a command already in flight has been answered and retired, then clears the list.
    • The wait is bounded at 3 s, past the 2 s ACK timeout. It returns at once on the IOThread.
    • mTransactionCompleted is now volatile, so the waiting thread sees it change.
    • Nothing calls operationPrepare() holding the device's lock, so the wait can't block the IOThread's removeInstruction().
  • The stop handling returns once it has handled the stop, so its byte can't retire a second instruction. It also logs when the stop was answered by its ACK.

Tests

API_00031_ConfigureStraightAfterStopTest (new, in CI) runs the IOThread as connect() starts it, over an in-memory radio that acknowledges every SET.

Test Without the fix With it
Configure while the stop is still being handled fails: chunks [0x80, 0x100] sent passes: [0x00, 0x80, 0x100]
A command queued after a stop answered by its ACK first fails: never sent passes
Configure after the stop has finished passes passes
  • Test suites: ShimmerDriver 177 tests and ShimmerDriverPC 88, with 0 failures.
  • On the bench: with this change, the Java HIL from DEV-1305: Java HIL rollover, idleStart and samplingRates scenarios #336 passes all seven of streamRest, midStreamSet, reconnectLoop, inStream, queueStress, idleStart and samplingRates. Every configuration made straight after a stop sent chunk 0.
  • Not run on a Shimmer3: the bench unit stopped answering before the fix was ready.

Merging

The other APIs and the wrappers

  • ShimmerAndroidAPI: has the same bug, because it uses this driver's IOThread and operationPrepare(). It gets the fix through a shimmerdriver pin bump, currently 0.11.7_beta, with no code port.
  • Shimmer-MATLAB-ID: needs its bundled jar re-vendored.
  • Not affected:
    • C#: it has no instruction queue, and its StopStreaming() blocks for about 1 s.
    • shimmer-logandstream: set_sampling_rate is refused until the library is READY.

🤖 Generated with Claude Code

Every reply path retires the command in flight with removeInstruction(0), taking the head of the
instruction list to be the command answered. Two things broke that around a stop:

- operationPrepare() cleared the list while the STOP was still in flight. The stop handling, which
  sleeps 400 ms on the first byte after the STOP, then removed the head in the STOP's place: the first
  SET_INFOMEM of a configuration started straight after stopStreaming(), the chunk that holds the
  sampling rate. The unit kept its old rate. operationPrepare() now waits, for up to 3 s, until a
  command already in flight has been answered and retired, then clears the list. mTransactionCompleted
  is volatile, so the waiting thread sees it.
- The stop handling then let the same byte go on to the ACK processing. When that byte was the unit's
  ACK, it retired the next instruction as well, a command queued after the stop that was never sent.
  The stop handling now returns once the stop is handled, and logs when it was answered by its ACK.

API_00031_ConfigureStraightAfterStopTest runs the IOThread over an in-memory radio. Without the fix
the stop-window configuration sends only chunks 0x0080 and 0x0100, and the command queued behind an
ACK-first stop is never sent; with it both pass, as does a configuration after the stop has finished.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JongChern
JongChern merged commit 3be3459 into master Oct 23, 2026
2 checks passed
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