Skip to content

fix(test): SPI ping-pong polling-path infinite loop + UBSan overflow - #2843

Merged
zackees merged 1 commit into
masterfrom
fix/spi-pingpong-polling-model
Jun 5, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/spi-pingpong-polling-model

Conversation

@zackees

@zackees zackees commented Jun 5, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes the `spi_pingpong_pipeline_logic` test failure that has been blocking unrelated PRs (e.g. #2840, and previously surfaced in #2838's local builds).

`stepStreaming`'s polling branch derived the just-drained buffer from `encodeIdx & 1`, which alternates A/B independently of which buffer was actually in flight. In polling mode `startFirstDma()` only queues A, so:

```
step 1: alternation says "drain A" → matches reality (transA=true)
step 2: alternation says "drain B" → transB was never queued, no-op,
but refill still encodes a B chunk and sets transB=true → now
BOTH A and B in flight, violating polling's "1-in-flight"
invariant.
```

After `ledBytesRemaining` hits 0, `encodeIdx` stops advancing (no refill happens), so the alternation gets stuck pointing at whichever buffer isn't in flight. `anyInFlight()` stays true forever and the test loop spins, incrementing `++completedCount` until UBSan trips on signed-int overflow at line 126. That's the "signed integer overflow: 2147483647 + 1" failure mode CI has been hitting.

Fix

Drop the `encodeIdx`-based polling branch entirely and share the async drain logic — drain whichever buffer is actually in flight (A first, mirroring the ESP-IDF FIFO). The polling path's distinguishing behavior is already captured in `startFirstDma()`'s pre-queue decision (line 92); `stepStreaming` doesn't need to know the mode.

Test plan

  • `bash test --cpp` — 227/227 pass (previously 226/227)
  • `bash test spi_pingpong_pipeline_logic --debug` — clean UBSan output

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Resolved a critical bug in the SPI driver pipeline buffer management that could cause infinite loops or integer overflow errors during streaming operations. The fix enhances reliability and prevents potential system crashes by ensuring deterministic and correct buffer selection during high-throughput SPI data transfers.

…verflow

stepStreaming's polling branch derived the just-drained buffer from
`encodeIdx & 1`, which alternates A/B independently of which buffer was
actually in flight. In polling mode startFirstDma() only queues A, so:

  step 1: alternation says "drain A" → matches reality (transA=true)
  step 2: alternation says "drain B" → transB was never queued, no-op,
          but refill still encodes a B chunk and sets transB=true → now
          BOTH A and B in flight, violating polling's "1-in-flight"
          invariant.

After ledBytesRemaining hits 0, encodeIdx stops advancing (no refill
happens), so the alternation gets stuck pointing at whichever buffer
isn't in flight. anyInFlight() stays true forever and the test loop
spins, incrementing `++completedCount` until UBSan trips on int
overflow at line 126. This was masking the underlying model bug as a
"signed integer overflow" runtime error in CI.

Fix: drop the encodeIdx-based polling branch entirely and share the
async drain logic — drain whichever buffer is actually in flight (A
first, mirroring the ESP-IDF FIFO). The polling path's distinguishing
behavior is already captured in startFirstDma()'s pre-queue decision
(line 92); stepStreaming doesn't need to know the mode.

All 227 unit tests pass after this change.
@coderabbitai

coderabbitai Bot commented Jun 5, 2026 •

Copy link
Copy Markdown

Ready to act? Review this PR in Change Stack to turn feedback into patch suggestions you can inspect and refine.

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 315788e6-0190-48dc-9cc6-f4303614e034

📥 Commits

Reviewing files that changed from the base of the PR and between ebd8791 and d810bd9.

📒 Files selected for processing (1)
  • tests/platforms/esp/32/drivers/spi/spi_pingpong_pipeline_logic.cpp

📝 Walkthrough

Walkthrough

The change refactors the PipelineModel::stepStreaming() drain behavior to select the drained buffer based on actual in-flight transaction state rather than encodeIdx parity. This fixes a case where polling could drain the wrong buffer and trigger an infinite loop or integer overflow in the completion counter.

Changes

PipelineModel streaming drain buffer selection fix

Layer / File(s) Summary
In-flight buffer drain logic
tests/platforms/esp/32/drivers/spi/spi_pingpong_pipeline_logic.cpp
stepStreaming() now deterministically drains the buffer actually in-flight (preferring A when both are in-flight) and stores the drained buffer index to use for refilling, replacing the prior encodeIdx parity-based selection that could drain the wrong buffer.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

  • FastLED/FastLED#2824: Both changes target the same PipelineModel::stepStreaming() drain logic in the same file to replace encodeIdx-derived polling with in-flight buffer selection.

Possibly related PRs

  • FastLED/FastLED#2810: Both PRs update the same PipelineModel::stepStreaming() drain/refill logic to ensure the buffer that actually completed is refilled instead of using encodeIdx/parity assumptions.

Poem

🐰 A buffer spins in endless night,
Poll-parity steering left and right—
But now we track what's really flying,
And drain what's in-flight, no more trying!
Integer underflow takes flight,
Pipeline loops spin true and bright. 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main fix: resolving an infinite loop and integer overflow in the SPI ping-pong polling path, which matches the core problem and solution in the changeset.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/spi-pingpong-polling-model

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 Infer (1.2.0)
tests/platforms/esp/32/drivers/spi/spi_pingpong_pipeline_logic.cpp

tests/platforms/esp/32/drivers/spi/spi_pingpong_pipeline_logic.cpp:25:10: fatal error: 'fl/stl/cstddef.h' file not found
25 | #include "fl/stl/cstddef.h"
| ^~~~~~~~~~~~~~~~~~
1 error generated.
Error: the following clang command did not run successfully:
/opt/infer-linux-x86_64-v1.2.0/lib/infer/facebook-clang-plugins/clang/install/bin/clang-18
@/tmp/coderabbit-infer/d810bd9e9bc00490bdf8a29e80510bff29c8ba5b-9d948ea0bf82c7ec/tmp/clang_command_.tmp.0acc14.txt
++Contents of '/tmp/coderabbit-infer/d810bd9e9bc00490bdf8a29e80510bff29c8ba5b-9d948ea0bf82c7ec/tmp/clang_command_.tmp.0acc14.txt':
"-cc1" "-load"
"/opt/infer-linux-x86_64-v1.2.0/lib/infer/infer/bin/../../facebook-clang-plugins/libtooling/build/FacebookClangPlugin.dylib"
"-add-plugin" "BiniouASTExporter" "-plugin-arg-BiniouASTExporter" "-"
"-plugin-arg-BiniouASTExporter" "PREPEND_CURRENT_DIR=1"
"-plugin-arg-BiniouASTExporter" "MAX_STRING_SIZE=65535" "-cc1" "-triple"
"x86_64-unknown-l

... [truncated 1178 characters] ...

l-isystem" "/usr/local/include" "-internal-isystem"
"/usr/lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/include"
"-internal-externc-isystem" "/usr/include/x86_64-linux-gnu"
"-internal-externc-isystem" "/include" "-internal-externc-isystem"
"/usr/include" "-Wno-ignored-optimization-argument" "-Wno-everything"
"-fdeprecated-macro" "-ferror-limit" "19" "-fgnuc-version=4.2.1"
"-fskip-odr-check-in-gmf" "-fcxx-exceptions" "-fexceptions"
"-D__GCC_HAVE_DWARF2_CFI_ASM=1" "-o"
"/tmp/coderabbit-infer/9d948ea0bf82c7ec/file.o" "-x" "c++"
"tests/platforms/esp/32/drivers/spi/spi_pingpong_pipeline_logic.cpp"
"-O0" "-fno-builtin" "-include"
"/opt/infer-linux-x86_64-v1.2.0/lib/infer/infer/bin/../lib/clang_wrappers/global_defines.h"
"-Wno-everything"


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@zackees
zackees merged commit 73ac19d into master Jun 5, 2026
3 checks passed
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Jun 5, 2026
pull Bot pushed a commit to manmuqingshan/FastLED that referenced this pull request Jun 6, 2026
Closes FastLED#2824.

PR FastLED#2843 already fixed the polling-drain logic bug (the encodeIdx-based
branch that produced the infinite loop). This closes out the issue's
"Additional defense" recommendation: add the same `safety` counter +
`stepStreaming()` return-value guard that's already in the queue-depth
test (line 226) to the two drain-all loops that don't yet have it.

Without the guard, a future regression of the same shape would manifest
as a 20 s runner-watchdog timeout (the original FastLED#2824 symptom). With it,
the loop bails after 1000 iterations with a clear `FL_CHECK_GT(safety,
0)` assertion in milliseconds.

Affected test cases:
- "Ping-pong - all bytes accounted for after drain" (line 248)
- "Ping-pong - polling path drains all bytes" (line 268, the original
  FastLED#2824 hang site)

The 1000-iteration cap leaves generous headroom — a 4 KB-capacity strip
of 12345 bytes drains in ~25 ping-pong steps; 8000 bytes polling drains
in ~16. Any regression that crosses 1000 is the same class of bug that
FastLED#2824 caught.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@zackees
zackees deleted the fix/spi-pingpong-polling-model branch August 21, 2026 19:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant