Skip to content

fix(deque): free orphaned chunks in grow_map to stop chunked-deque leak (closes #3286) - #3289

Merged
zackees merged 1 commit into
masterfrom
fix/audio-detector-deque-contiguity
Jun 19, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/audio-detector-deque-contiguity

Conversation

@zackees

@zackees zackees commented Jun 19, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes the macOS-arm64 unit-test regression that's been red on master since #3282 (commit a8b5cac). LeakSanitizer reported ~5KB leaked in 20 allocations rooted in fl::deque<float>::ensure_back_room() from TempoAnalyzer::updateCurrentTempo / update.

Closes #3286.

Root cause

The chunked-deque refactor in #3282 has a chunk-orphaning bug in the pop / grow interaction:

  • pop_front() / pop_back() advance mFrontMapIdx / mSize past an emptied chunk without freeing or nulling the chunk pointer. The chunk is left orphaned in mMap[].
  • The destructor sweeps the entire map, so orphans are harmless as long as the map itself survives.
  • BUT grow_map() only copies pointers in [mFrontMapIdx, mFrontMapIdx+used) forward into the new map, then deallocates the old map. Every orphaned chunk pointer outside that range is silently lost, leaking its backing storage.

The audio TempoAnalyzer hits this directly: mOnsetTimes (50-entry sliding window) and mBPMHistory (20-entry sliding window) do push_back + pop_front each frame. After enough frames, an old chunk gets orphaned, then a later push_back forces grow_map, then the orphan leaks. Linux x86-64 CI doesn't run leak detection so the regression only tripped the macos job.

Fix

In grow_map(), after a successful new-map allocation but before deallocating the old map, free any chunk pointers in the old map that fall outside the live [mFrontMapIdx, mFrontMapIdx+used) range. The live range still copies forward unchanged, so chunk addresses stay stable for surviving elements (preserves the #3270 pointer / iterator stability guarantee).

Two-file diff:

  • src/fl/stl/deque.h — the orphan-cleanup loop inside grow_map
  • tests/fl/stl/deque.cpp — regression test using SmallChunkTag (chunk_size=4) to reproduce the leak pattern in a few dozen ops

Test plan

  • bash test fl_stl_deque --debug (clang + ASAN) → pass
  • bash test fl_audio_detectors --debug (clang + ASAN) → pass
  • bash lint --cpp → clean
  • macOS-arm64 CI confirms the original failing test now passes

References

Summary by CodeRabbit

  • Bug Fixes

    • Fixed memory leak in deque container where orphaned chunk pointers were not properly deallocated during internal map resizing cycles.
  • Tests

    • Added regression test case validating deque behavior during repeated pop and push operations that trigger internal map growth.

…ak (closes #3286)

Regression introduced by #3282 (chunked map-of-fixed-size-blocks layout,
commit a8b5cac). Surfaced as macOS-arm64 unit test failure
fl_audio_detectors with ASAN LeakSanitizer reporting ~5KB leaked in 20
allocations, all rooted in fl::deque<float>::ensure_back_room() →
DefaultMemoryResource::do_allocate(). Linux x86-64 jobs don't run
leak-detection, so the regression only tripped CI on the macos workflow.

Root cause
----------
`pop_front()` / `pop_back()` advance mFrontMapIdx / mSize past an
emptied chunk without freeing or nulling the chunk pointer. The chunk
is left orphaned in mMap[]. The destructor sweeps the entire map so
orphans are harmless as long as the map itself survives.

But `grow_map()` only copies pointers in [mFrontMapIdx,
mFrontMapIdx+used) forward into the new map; it then deallocates the
old map. Every orphaned chunk pointer outside that range is silently
lost, leaking its backing storage.

The audio TempoAnalyzer hits this directly: mOnsetTimes (50-entry
sliding window) and mBPMHistory (20-entry sliding window) do
push_back + pop_front each frame. After enough frames, an old chunk
gets orphaned, then a later push_back forces grow_map, then the orphan
leaks.

Fix
---
In `grow_map()`, after a successful new-map allocation but before
deallocating the old map, free any chunk pointers in the old map that
fall outside the live [mFrontMapIdx, mFrontMapIdx+used) range. The
live range still copies forward unchanged, so chunk addresses remain
stable for elements that survive a grow (preserving the #3270 design
intent of pointer / iterator stability).

Test
----
tests/fl/stl/deque.cpp gains `fl::deque - grow_map releases orphaned
chunks (#3286)`. It uses SmallChunkTag (chunk_size=4) to reproduce the
push/pop/push-grow leak pattern in a few dozen ops instead of the
thousands needed at the default chunk_size=64; ASAN under macOS-arm64
flags the leak immediately on the broken code.

Verification
- bash test fl_stl_deque --debug → pass (Windows clang+ASAN)
- bash test fl_audio_detectors --debug → pass (Windows clang+ASAN)
- bash lint --cpp → clean

References
- Breaking PR: #3282 (commit a8b5cac)
- Regression issue: #3286

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 19, 2026 •

Copy link
Copy Markdown

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: 5331f751-899f-4c17-bf30-af2ce01a12b6

📥 Commits

Reviewing files that changed from the base of the PR and between a427694 and c78104e.

📒 Files selected for processing (2)
  • src/fl/stl/deque.h
  • tests/fl/stl/deque.cpp

📝 Walkthrough

Walkthrough

In fl::deque<T>::grow_map, a new loop iterates over the old map before it is freed, deallocating any chunk pointers that fall outside the live range [mFrontMapIdx, mFrontMapIdx + used) and nulling those slots. A regression test using deque<SmallChunkTag> reproduces the leak scenario via pop_front/push_back sequences that trigger grow_map.

Changes

grow_map orphaned chunk cleanup

Layer / File(s) Summary
grow_map: free orphaned chunks before map reallocation
src/fl/stl/deque.h
Updates the grow_map doc comment to describe the orphaned-chunk freeing behavior, then adds a loop that computes the live-range end, deallocates chunk pointers outside [mFrontMapIdx, mFrontMapIdx + used), and nulls those slots before the old map is released.
Regression test: grow_map releases orphaned chunks
tests/fl/stl/deque.cpp
Adds FL_TEST_CASE reproducing issue #3286: uses deque<SmallChunkTag> to force grow_map after pop_front orphans chunks, asserts size and element values after the growth event, then runs repeated pop_front/push_back rounds across many grow cycles checking final deque state.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Possibly related PRs

  • FastLED/FastLED#3282: Introduced the chunked fl::deque<T> layout whose grow_map path is the direct source of the orphaned chunk pointers fixed here.

Poem

🐇 Hop, hop, the map grew wide,
Old chunks were left to hide inside,
But now we sweep each orphaned block,
Free them clean before we unlock,
No leaking pointers, no crash-and-burn —
The deque is tidy at every turn! 🌿

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% 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 clearly and specifically describes the main change: fixing a memory leak in deque's grow_map by freeing orphaned chunks, which directly addresses the core issue.
Linked Issues check ✅ Passed The PR directly addresses all coding objectives from #3286: identifies the root cause of the deque memory leak, implements the fix in grow_map to free orphaned chunks, and includes a regression test to prevent recurrence.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the identified memory leak: the grow_map implementation fix, inline documentation update, and focused regression test with SmallChunkTag.

✏️ 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/audio-detector-deque-contiguity

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 5c9662a into master Jun 19, 2026
11 checks passed
@zackees
zackees deleted the fix/audio-detector-deque-contiguity branch June 19, 2026 20:44
@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Jun 19, 2026
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Jun 19, 2026
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.

fl_audio_detectors macOS-arm64 unit test failing on master since PR #3282 (deque refactor)

1 participant