Skip to content

fix(streaming): preserve pooled cache buffer ownership - #11152

Merged
ReubenBond merged 1 commit into
dotnet:mainfrom
ReubenBond:rb-fix-streaming-pooled-buffer-ownership
Sep 9, 2026
Merged

ReubenBond merged 1 commit into
dotnet:mainfrom
ReubenBond:rb-fix-streaming-pooled-buffer-ownership

Conversation

@ReubenBond

@ReubenBond ReubenBond commented Sep 5, 2026 •

Copy link
Copy Markdown
Member

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

  • Release every tracked buffer when chronological eviction empties the cache.
  • Clear the Generator, Memory, and Event Hubs caches' currentBuffer after an emptying purge so the next append reacquires a buffer from the pool and registers its ownership.
  • Clear removed and reset cached-message slots so backing-buffer references follow the cached message lifetime.
  • Cover Generator and Memory cache ownership directly and align the existing Event Hubs purge expectations with full buffer release.

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 High severity

New issues introduced by this change (1)
Severity Finding
High severity test/​Orleans.Streaming.Tests/​OrleansRuntime/​Streams/​PooledCacheBufferOwnershipTests.cs — IMemoryMessageBodySerializer.Serialize returns ArraySegment&lt;byte&gt;, 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 currentBuffer after 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.

@ReubenBond
ReubenBond merged commit baf91fa into dotnet:main Sep 9, 2026
71 checks passed
@ReubenBond
ReubenBond deleted the rb-fix-streaming-pooled-buffer-ownership branch September 9, 2026 23:16
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 10, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants