Conversation
…rBase BufferAndLoad registers DoReadPage as a deferred drain callback via BumpCurrentEpoch. This callback captures instance state including loadCompletionEvents. When the callback is for a read-ahead page and SafeToReclaimEpoch hasn't advanced yet, the callback remains in the drain list. If the scan completes and the iterator is disposed before the callback executes, loadCompletionEvents is set to null. When the callback later runs (during another thread's Drain), it accesses the null array, throwing NullReferenceException. This exception from a drain callback leaves the epoch in a held state (SuspendDrain's Resume/Release is not exception-safe), causing the cascading 'Trying to acquire protected epoch' assertion failure seen in ObjectIterationPushLockTest(4,4,Iterate,False). Fix: capture loadCompletionEvents into a local before accessing it in DoReadPage, and return early if the iterator has been disposed. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a concurrency issue in Tsavorite scan iterators where a deferred epoch drain callback could run after the iterator is disposed, leading to a NullReferenceException and potentially leaving the epoch in a held state.
Changes:
- In
ScanIteratorBase.BufferAndLoad, updates the deferredDoReadPagecallback to snapshotloadCompletionEventsinto a local variable and return early if it has been nulled byDispose(). - Adds explanatory comments documenting why the deferred callback can outlive the iterator instance state.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fix deferred DoReadPage drain callback accessing disposed ScanIteratorBase
BufferAndLoad registers DoReadPage as a deferred drain callback via BumpCurrentEpoch. This callback captures instance state including loadCompletionEvents. When the callback is for a read-ahead page and SafeToReclaimEpoch hasn't advanced yet, the callback remains in the drain list. If the scan completes and the iterator is disposed before the callback executes, loadCompletionEvents is set to null. When the callback later runs (during another thread's Drain), it accesses the null array, throwing NullReferenceException.
This exception from a drain callback leaves the epoch in a held state (SuspendDrain's Resume/Release is not exception-safe), causing the cascading 'Trying to acquire protected epoch' assertion failure seen in ObjectIterationPushLockTest(4,4,Iterate,False).
Fix: capture loadCompletionEvents into a local before accessing it in DoReadPage, and return early if the iterator has been disposed.