[BUG] Closed StreamReader can restore block state after close
Author: BackendArchitectXCreated Sep 21, 2026Updated Sep 21, 2026
AutoMQ Version
main at 7d4f733348 (3.9.0-SNAPSHOT)
Operating System
Windows local source build. The issue is in StreamReader lifecycle/event-loop logic and is not expected to be OS-specific.
Installation Method
source
Other Relevant Software
- JDK 17
- Gradle 8.8
- Scala 2.13.14
What Went Wrong?
StreamReader.close() clears the reader's block state, but asynchronous block-index/readahead work can cross the close lifecycle boundary.
There are two independently reproducible cases:
- A block-index load starts before
close()and completes afterward. The asynchronous continuation can restore blocks after the reader has already been closed. - Readahead starts after
close(). Without an explicit closed-state guard, it can start a newObjectManager#getObjects(...)metadata load.
This violates the expected lifecycle invariant that a closed StreamReader must not restore or start loading block-index state.
What Should Have Happened Instead?
After StreamReader.close():
- work that started before close must not restore block state;
- queued or later readahead must not start another block-index metadata load;
blocksMapmust remain empty.
Steps to Reproduce
In-flight load
- Start readahead while delaying
ObjectManager#getObjects. - Close the reader before the metadata future completes.
- Confirm that
blocksMapis empty immediately after close. - Complete the delayed metadata load.
- Wait for the readahead continuation.
- Observe that
blocksMapbecomes non-empty again.
Post-close load
- Close the
StreamReader. - Trigger readahead.
- Observe that
ObjectManager#getObjects(...)is invoked after close.
Both cases are covered by deterministic regression tests.
Additional Information
On current main (7d4f733348):
testClosePreventsInflightReadaheadFromRestoringBlocksfails because block state is restored after close.testClosedReaderDoesNotStartNewReadaheadLoadfails becauseObjectManager#getObjects(...)is invoked after close.
Applying the fix proposed in #3555 makes both regressions pass.
Proposed fix: #3555
Source: AutoMQ/automq