Repository navigation
fix(streaming): preserve pooled cache buffer ownership - #11152
Merged
ReubenBond merged 1 commit intoSep 9, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The new test introduces a compile-time type mismatch by returning byte[] where ArraySegment<byte> is required in IMemoryMessageBodySerializer.Serialize.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review tier: Lite
Findings: 1
New issues introduced by this change (1)
| Severity | Finding |
|---|---|
test/Orleans.Streaming.Tests/OrleansRuntime/Streams/PooledCacheBufferOwnershipTests.cs — IMemoryMessageBodySerializer.Serialize returns ArraySegment<byte>, but this implementation… |
What changed in this PR
This PR fixes a pooled-buffer lifetime hazard in Orleans streaming caches where a purge can return buffers to the pool while some cache adapters still retain a writable currentBuffer, allowing subsequent writes through a buffer that has already been freed.
Changes:
- Ensure chronological eviction returns all tracked buffers when a purge empties the cache.
- Clear each adapter’s
currentBufferafter an emptying purge so the next add allocates/re-registers a fresh pooled buffer (Memory, Generator, Event Hubs). - Release stale backing-buffer references by clearing removed/reset cached-message slots, and add/update tests to cover these ownership semantics.
| File | Description |
|---|---|
| test/Orleans.Streaming.Tests/OrleansRuntime/Streams/PooledCacheBufferOwnershipTests.cs | Adds direct regression tests for Memory + Generator caches allocating a fresh buffer after an emptying purge. |
| test/Extensions/Orleans.Streaming.EventHubs.Tests/EvictionStrategyTests/EHPurgeLogicTests.cs | Updates Event Hubs purge expectations to require full buffer release when caches are emptied. |
| src/Orleans.Streaming/MemoryStreams/MemoryPooledCache.cs | Clears currentBuffer when a purge leaves the cache empty. |
| src/Orleans.Streaming/Generator/GeneratorPooledCache.cs | Clears currentBuffer when empty and introduces an internal ctor to inject a purge predicate for testing. |
| src/Orleans.Streaming/Common/PooledCache/ChronologicalEvictionStrategy.cs | Fixes buffer release logic to free all tracked buffers when purge empties the cache. |
| src/Orleans.Streaming/Common/PooledCache/CachedMessageBlock.cs | Clears removed/reset cached-message slots to drop stale segment/buffer references sooner. |
| src/Azure/Orleans.Streaming.EventHubs/Providers/Streams/EventHub/EventHubQueueCache.cs | Makes currentBuffer nullable and clears it when purge empties the cache. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.

Problem
Pooled cache eviction retains the final buffer when a purge empties the cache because cache adapters keep that buffer as their current append target. Cached-message blocks also retain removed entries and their backing-buffer references until those slots are overwritten.
Solution
currentBufferafter an emptying purge so the next append reacquires a buffer from the pool and registers its ownership.Rationale
This is an independently mergeable extraction of the pooled-buffer lifetime fixes from #11076 and #10588. The coordinated eviction and adapter updates allow an empty cache to release all buffers while preserving correct ownership for the next append, and contribute toward #756 without bringing in retained-history replay, checkpointing, or public extensibility changes.
Microsoft Reviewers: Open in CodeFlow